0

我有这样的json解析

姓名

  • 价值推荐

    • 值 b
  • MPN

  • 价值推荐
    • 值 b

我可以在列表视图中显示这个值(品牌和 mpn)。我想如果我点击品牌,它将在列表中显示价值推荐(价值 a 和 b)。如果我单击 mpn,它也会显示列表中名称 2 的值 recom(值 a 和 b)。任何人都可以给我建议??谢谢

这是我的 json 解析

array (
    'features' => 
    array (
     'Recommendations' => 
      array (
        'id' => 
        'NameRecommendation' => 
        array (
          0 => 
          array (
            'Name' => 'Brand',
            ),      
            'ValueRecommendation' => 
            array (
              0 => 
              array (
                'Value' => 'Unbranded/Generic',
                ),

              1 => 
              array (
                'Value' => 'Canon',
                ),

              2 => 
              array (
                'Value' => 'Nikon',
                ),

              ),
  1 => 
          array (
            'Name' => 'MPN',
            ),
              'ValueRecommendation' => 
            array (
              0 => 
              array (
                'Value' => 'mpn1',
                ),

              1 => 
              array (
                'Value' => 'aas',
                ),

              2 => 
              array (
                'Value' => 'aab',
                ),
4

3 回答 3

0

您可以在 JSONArray 对象中阅读此内容

这是示例

JSONArray jsonArr = new JSONArray(jsonStr); // this is you string as formatted in json array if not parse by this construct it will throw the jsonparseexception

现在通过迭代器遍历这个 json 的每个对象

for(int i=0;i<jsonArr.length();i++){
    JSONObject jsonObj = jsonArr.get(i).getJsonObject("value recom");
    String a = jsonObj.getString("a"); // here is your data for a object
    String b = jsonObj.getString("b"); // here is your data for b object
}
于 2013-01-10T10:35:06.813 回答
0

使用谷歌 gson 时。

var getRowData =
[{
    "dayOfWeek": "Sun",
    "date": "11-Mar-2012",
    "los": "1",
    "specialEvent": "",
    "lrv": "0"
},
{
    "dayOfWeek": "Mon",
    "date": "",
    "los": "2",
    "specialEvent": "",
    "lrv": "0.16"
}];

    JsonElement root = new JsonParser().parse(request.getParameter("getRowData"));
     JsonArray  jsonArray = root.getAsJsonArray();
     JsonObject  jsonObject1 = jsonArray.get(0).getAsJsonObject();
     String dayOfWeek = jsonObject1.get("dayOfWeek").toString();

// 当使用杰克逊时

    JsonFactory f = new JsonFactory();
              ObjectMapper mapper = new ObjectMapper();
          JsonParser jp = f.createJsonParser(getRowData);
          // advance stream to START_ARRAY first:
          jp.nextToken();
          // and then each time, advance to opening START_OBJECT
         while (jp.nextToken() == JsonToken.START_OBJECT) {
            Map<String,Object> userData = mapper.readValue(jp, Map.class);
            userData.get("dayOfWeek");
            // process
           // after binding, stream points to closing END_OBJECT
        }
于 2014-04-22T09:40:00.840 回答
0

检查此urljsonobject并使用of -name 读取总数据

于 2013-01-10T10:34:33.823 回答