如何从android中的json获取值

时间:2014-09-19 10:53:08

标签: json

您好我得到以下json pentaho服务器,我如何从这个json获取日期

"queryInfo":{
    "totalRows":"1"
},
"resultset":[
    [
        "09-09-2014"
    ]
],
"metadata":[
    {
        "colIndex":0,
        "colType":"String",
        "colName":"dt"
    }
]

}

1 个答案:

答案 0 :(得分:0)

最好的方法是使用gson来对字符串进行deserilize: 在单独的文件中创建一个类。

import java.util.List;

import com.google.gson.annotations.SerializedName;

public class pentaho {

    public QueryInfo queryInfo;

    public static class QueryInfo {
        public List<Result> metadata;

        public static class Result {
            @SerializedName("colIndex")
            public String colIndexStr;

            @SerializedName("colType")
            public String colTypeStr;

            @SerializedName("colName")
            public String colNameStr;
        }

    }
 }

在您的活动中

      public pentaho pentahoResult;

在你的职能中。

private void getJson(String jsonStr){
  Gson gson = new Gson();
  pentahoResult = gson.fromJson(jsonStr, pentaho.class);

现在你可以通过用一个像int i这样的循环变量替换0来查看每个结果。

String MycolIndex = pentahoResult.d.results.get(0).colIndexStr;
String MycolType = pentahoResult.d.results.get(0).colTypeStr;
String MycolName = pentahoResult.d.results.get(0).colNameStr;

玩得开心。