2014-04-25 73 views
-3

我想解析这个json.i有困难,当我试图解析下面的json.suggest我解析正确的方式下面的json.i编辑我的代码,noe这是我想解析正确的代码。如何解析此json字符串?

{ 
"tag":{ 
    "tag1":1, 
    "tag2": "colour", 
    "tag3":"value", 
    "tag4":"value", 
    "tag5":1, 
    "tag6":1, 
    "tag7":true, 
    "tag8": [ 
      { 
      "t1":"Red", 
      "t2":"int", 
      "t3":1, 
      "t4":true 
      }, 
      { 
      "t1":"Green", 
      "t2":"int", 
      "t3":2, 
      "t4":true 
      }, 
      { 
      "t1":"Blue", 
      "t2":"int", 
      "t3":3, 
      "t4":true 
      } 
      ], 
    "tag9":null, 
    "tag10":1 
}, 
"tag":{ 
    "tag1":2, 
    "tag2": "value", 
    "tag3":"value", 
    "tag4":"value", 
    "tag5":1, 
    "tag6":1, 
    "tag7":true, 
    "tag8": [ 
      { 
      "t1":"value", 
      "t2":"value", 
      "t3":true, 
      "t4":true, 

      }, 
      { 
      "t1":"value", 
      "t2":"value", 
      "t3":false, 
      "t4":true, 

      } 
      ], 
    "tag9":1, 
    "tag10":3 
}, 
"tag":{ 
    "tag1":5, 
    "tag2": "value", 
    "tag3":"value", 
    "tag4":"value", 
    "tag5":1, 
    "tag6":1, 
    "tag7":false, 
    "tag8": null, 
    "tag9":null, 
    "tag10":null 
} 

} 

i had used this code   
    try { 
     JSONObject json= (JSONObject) new JSONTokener(loadJSONFromAsset()).nextValue(); 
     JSONObject json2 = json.getJSONObject("tag"); 
     for (int i = 0; i < json.length(); i++) { 
      int tag1 = (Integer) json2.get("tag1"); 
      String tag2 = (String) json2.get("tag2"); 
      String tag3 = (String) json2.get("tag3"); 
      String tag4 = (String) json2.get("tag4"); 
      int tag5 = (Integer) json2.get("tag5"); 
      int tag6 = (Integer) json2.get("tag6"); 
      boolean tag7 = json2.getBoolean("tag7"); 
      if (!json2.isNull("tag8")) { 
       JSONArray jArray = json2.getJSONArray("tag8"); 
       JSONObject json_data = null; 
       for (int i1 = 0; i1 < jArray.length(); i1++) { 
        json_data = jArray.getJSONObject(i1); 
        String Label = json_data.getString("t1"); 
        String ValueType = json_data.getString("t2"); 
        int Value = json_data.getInt("t3"); 
        boolean isNextEnabled = json_data.getBoolean("t4"); 
       } 
      } 
      int previous = 0; 
      if (!json2.isNull("tag9")) { 

       previous= (Integer) json2.get("tag9"); 
      } 
      int next = 0; 
      if (!json2.isNull("tag10")) { 

       next = (Integer) json2.get("tag10"); 
      } 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 


public String loadJSONFromAsset() { 
    String json = null; 
    try { 

     InputStream is = getAssets().open("myjson"); 

     int size = is.available(); 

     byte[] buffer = new byte[size]; 

     is.read(buffer); 

     is.close(); 

     json = new String(buffer, "UTF-8"); 


    } catch (IOException ex) { 
     ex.printStackTrace(); 
     return null; 
    } 
    return json; 

} 
} 

我只得到最后的记录,我如何得到所有数据.ANY帮助将不胜感激感谢...

+0

所有这些都是动态的还是固定的? - >“tag”和tag1,tag2,t1,t2,“respond” – user2450263

+1

有多余的'tag'键,你确定这是你的JSON,因为即使'JsonValidator'也返回只有最后一个'标签'键。 –

回答

0

的JSONOject具有接受JSON数据字符串的构造函数。您可以使用构造函数来解析JSON字符串。如果字符串是有效的JSON数据,构造函数将返回一个JSONObject。如果字符串不是有效的JSON,那么构造函数会抛出一个错误。将构造函数封装在try/catch块内是一个很好的习惯。

String json = "{name:\"value\"}"; 
JSONObject item = null; 
try{ 
    item = new JSONObject(json); 
}catch(Exception exc){ 
    Log.d(TAG, exc.toString()); 
} 
if(item != null){ 
    Log.d(TAG, String.format("name:%s", item.getString("name"))); 
} 
+0

考虑添加解释。从长远来看,普通代码很少有帮助。 – Joffrey

+0

好点。我会更新答案。 – itsben

0

你可以尝试解析您的字符串@http://json.parser.online.fr/它的工作原理很好我。

从JSON字符串的静态回顾中看来,您在输入中有3次“标记”元素,并且由于该解析器可能会感到困惑。尝试删除重复的条目,然后尝试。