【问题标题】:How to Loop JSON Data & Parse without Array in JSON file如何在 JSON 文件中循环 JSON 数据并解析没有数组
【发布时间】:2015-10-19 04:53:03
【问题描述】:

我正在尝试解析没有数组的 JSON 数据,目前我的代码循环通过 JSONArray 并且工作正常。

问题/错误

我不确定如何对没有数组的 JSON 执行相同的操作。当我在没有数组的 JSON 中没有要处理的数组长度时如何循环。

代码:- 目前适用于带有数组的 JSON 数据

JSONArray inbox = null;

List<NameValuePair> params = new ArrayList<NameValuePair>();

 // getting JSON string from URL
  JSONObject json = jsonParser.makeHttpRequest(INBOX_URL, "GET",
                params);

try {

        inbox = json.getJSONArray(TAG_ARRAY);
    inbox.toString();

 // looping through All messages
 for (int i = 0; i < inbox.length(); i++) {
 JSONObject c = inbox.getJSONObject(i);

    // Storing each json item in variable
String id = c.getString(TAG_ID);
String person = c.getString(TAG_PERSON);
….. 
…..
…..
// creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                map.put(TAG_ID, id);
                map.put(TAG_FROM, person);
….. 
…..
…..

没有数组的 JSON

[
    {
        "id": "1",
        "person": "David",
        "thur": "",
        "grade": "15",
        "round": "",
        "tour": ""
    },
    {
        "id": "T2",
        "person": "Mary",
        "thur": "",
        "grade": "13",
        "round": "",
        "tour": ""
    },


]

JSON WITH ARRAY - 上面的代码可以正常工作

{
    "id": [
        {
            "id": "1",
            "person": "Jason",
            "thur": "F",
            "grade": "17",
            "round": "2"
        },
        {
            "id": "2",
            "person": "Joe",
            "grade": "F",
            "score": "16",
           "round": "3"
        }

]

【问题讨论】:

  • 您需要更改 makeHttpRequest 以便它可以返回 JSONArray。

标签: java android arrays json loops


【解决方案1】:

不知道你的解析器方法 (makeHttpRequest()) 是如何工作的 但是更改 makeHttpRequest() 方法的定义..所以它返回 jsonarray.. 因为你的 json 是 jsonarray..它在每个索引处进一步包含 jsonobject

JSONArray inbox = null;
List<NameValuePair> params = new ArrayList<NameValuePair>();

// getting JSON string from URL
JSONArray json = jsonParser.makeHttpRequest(INBOX_URL, "GET",params);

try {
    //inbox = json.getJSONArray(TAG_ARRAY);
    //inbox.toString();

    //comment the above 2 line and the rest is same
    // looping through All messages
    for (int i = 0; i < json.length(); i++) {
    JSONObject c = json.getJSONObject(i);

    // Storing each json item in variable
    String id = c.getString(TAG_ID);
    String person = c.getString(TAG_PERSON);
    // creating new HashMap
    HashMap<String, String> map = new HashMap<String, String>();

    // adding each child node to HashMap key => value
    map.put(TAG_ID, id);
    map.put(TAG_FROM, person);

【讨论】:

    【解决方案2】:

    你试过了吗?

    JSONObject jsonObject = new JSONObject(); try { JSONArray jsonArray = new JSONArray(jsonObject.toString()); } catch (JSONException e) { e.printStackTrace(); }

    但我建议你使用 Google Library GSON (https://github.com/google/gson) 从 rest 客户端获取 JSON 值

    【讨论】:

      猜你喜欢
      • 2018-06-02
      • 2019-03-17
      • 1970-01-01
      • 2021-10-17
      • 1970-01-01
      • 2016-08-17
      • 1970-01-01
      • 2021-12-20
      • 1970-01-01
      相关资源
      最近更新 更多