我想解析這個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幫助將不勝感激感謝...
所有這些都是動態的還是固定的? - >「tag」和tag1,tag2,t1,t2,「respond」 – user2450263
有多餘的'tag'鍵,你確定這是你的JSON,因爲即使'JsonValidator'也返回只有最後一個'標籤'鍵。 –