-1

我无法解析解析时有点混乱的 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 的响应。

谢谢。

4

2 回答 2

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();

                }
            }
        }
    }
于 2013-11-11T10:10:55.557 回答
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();

                }
            }
        }
    }
于 2013-11-11T10:11:31.127 回答