我无法解析在解析时有点混乱的JSON数据。我试过很多方面,但我无法这样做。我是JSON解析的新手。任何人都可以帮助我。
{
"pulse_updates_id": 203,
"poster": {
"id": 0,
"name": "test"
},
"postermodel": {
"id": 0,
"name": "modeltest"
},
"STM1": [
{
"provider": "MM",
"options": [
{
"itValue": 11.3,
"imValue": 16.3,
"sqValue": 24.4,
"description": "test description",
"isAvailable": false
},
{
"itValue": 14.3,
"imValue": 11.3,
"sqValue": 54.4,
"description": "test description2",
"isAvailable": true
}
],
"status": false,
"name": "testname",
"id": "984793353",
"testValue": {
"id": 0,
"name": "TestName"
},
"testIssue": {
"id": 0,
"name": "Issue"
}
}
],
"DTVG": null,
"RIP": null,
"HTSD": null,
"STM5": null,
"IdentificationNumber": null,
"Value": null
}
我将此作为JSON的回复。
感谢。
答案 0 :(得分:1)
private void parseJson(JSONObject data) {
if (data != null) {
Iterator<String> it = data.keys();
while (it.hasNext()) {
String key = it.next();
try {
if (data.get(key) instanceof JSONArray) {
JSONArray arry = data.getJSONArray(key);
int size = arry.length();
for (int i = 0; i < size; i++) {
parseJson(arry.getJSONObject(i));
}
} else if (data.get(key) instanceof JSONObject) {
parseJson(data.getJSONObject(key));
} else {
System.out.println("Key :" + key);
System.out.println("Value :" + data.getString(key));
}
} catch (Throwable e) {
try {
System.out.println("Key :" + key);
System.out.println("Value :" + data.getString(key));
} catch (Exception ee) {
}
e.printStackTrace();
}
}
}
}
答案 1 :(得分:0)
试试这个递归函数
调用此函数并查看logcat
This will parse any json object.
private void parseJson(JSONObject data) {
if (data != null) {
Iterator<String> it = data.keys();
while (it.hasNext()) {
String key = it.next();
try {
if (data.get(key) instanceof JSONArray) {
JSONArray arry = data.getJSONArray(key);
int size = arry.length();
for (int i = 0; i < size; i++) {
parseJson(arry.getJSONObject(i));
}
} else if (data.get(key) instanceof JSONObject) {
parseJson(data.getJSONObject(key));
} else {
System.out.println("Key : " + key);
System.out.println("Value : " + data.getString(key));
}
} catch (Throwable e) {
try {
System.out.println("Key : " + key);
System.out.println("Value : " + data.getString(key));
} catch (Exception ee) {
}
e.printStackTrace();
}
}
}
}