如何使用json对象和jsonarray

时间:2018-11-29 17:54:27

标签: java android json

我有这个JSON响应:

"objects":{
  "5": [
    [
      {
        "id_lot_espace": 0,
        "id_lot_objet": "0",
        "id_objet_piece": 0,
        "params": {
          "auto": "1",
          "objLink": "0",
          "setpointM": "7",
          "setpoint0": "21",
          "setpoint1": "19",
          "setpointA": "16",
          "tempSocialJ": "19",
          "tempSocialN": "17",
          "tempMin": "7",
          "tempMax": "30",
          "tempFrom": "2018-10-15",
          "tempTo": "2019-04-15"
        },
        "label": "migo",
        "pieceLabel": "Pièce principale",
        "objLabel": "Tête thermostatique",
        "code": "zwave_device_fab36177_node6_thermostat_setpoint_heating",
        "renommable": "0",
        "id_famille": 5,
        "reveil_possible": "1",
        "id_type_espace": "25",
        "principal": "1",
        "rights": 1
      }
    ]
  ],

    "17": {
      "381": {
        "19": {
          "id_lot_espace": "381",
          "id_lot_objet": "0",
          "id_objet_piece": "19",
          "params": "",
          "label": "Pièce principale - Tête thermostatique",
          "pieceLabel": "Pièce principale",
          "objLabel": "Tête thermostatique",
          "code": "",
          "renommable": "0",
          "id_famille": "17",
          "reveil_possible": "1",
          "id_type_espace": "25",
          "principal": "1",
          "rights": 1
        }
      }
    }
  }
}

我想访问每个元素中的pieceLabel。这是我到目前为止尝试过的:

job = new JSONObject(responseContent);
JSONObject object = job.getJSONObject("objects");

Iterator<String> it = object.keys();
while (it.hasNext()) {
  String key = it.next();
  JSONObject obj1 = object.getJSONObject(key);

  Iterator<String> it1 = obj1.keys();
  while (it1.hasNext()) {
    String key1 = it1.next();
    JSONObject obj2 = obj1.getJSONObject(key1);

    Iterator<String> it2 = obj2.keys();
    while (it2.hasNext()) {
      String key2 = it2.next();
      final JSONObject obj3 = obj2.getJSONObject(key2);
      String pieceLabel = String.valueOf(obj3.get("pieceLabel"));
    }
  }
}

4 个答案:

答案 0 :(得分:0)

您需要递归地迭代非结构化json并检查每个密钥。

以下是一个有效的示例,向您展示如何实现自己想要做的事情:

Pièce principale
Pièce principale

test.json文件包含您的示例json。

输出为:

{{1}}

更改键的值和顺序并查看结果。

答案 1 :(得分:0)

请检查json是否有问题。您可以在线使用许多工具来检查json是否正确。 我通常使用https://codebeautify.org/jsonviewer


更正的杰森

 [
      [
        {
      "id_lot_espace": 0,
      "id_lot_objet": "0",
      "id_objet_piece": 0,
      "params": {
        "auto": "1",
        "objLink": "0",
        "setpointM": "7",
        "setpoint0": "21",
        "setpoint1": "19",
        "setpointA": "16",
        "tempSocialJ": "19",
        "tempSocialN": "17",
        "tempMin": "7",
        "tempMax": "30",
        "tempFrom": "2018-10-15",
        "tempTo": "2019-04-15"
        },
      "label": "migo",
      "pieceLabel": "Pièce principale",
      "objLabel": "Tête thermostatique",
      "code": "zwave_device_fab36177_node6_thermostat_setpoint_heating",
      "renommable": "0",
      "id_famille": 5,
      "reveil_possible": "1",
      "id_type_espace": "25",
      "principal": "1",
      "rights": 1
     }
    ]
    ]


    {
  "17": {
    "381": {
      "19": {
        "id_lot_espace": "381",
        "id_lot_objet": "0",
        "id_objet_piece": "19",
        "params": "",
        "label": "Pièce principale - Tête thermostatique",
        "pieceLabel": "Pièce principale",
        "objLabel": "Tête thermostatique",
        "code": "",
        "renommable": "0",
        "id_famille": "17",
        "reveil_possible": "1",
        "id_type_espace": "25",
        "principal": "1",
        "rights": 1
      }
     }
    }
   }

最终密码

 try {
        JSONObject jObject = new JSONObject(sss.trim());
        Iterator<?> keys = jObject.keys();

        while (keys.hasNext()) {
            String key = (String) keys.next();
            Log.d("vt","output1 "+key);
            JSONObject obj1 = jObject.getJSONObject(key);
            Iterator<String> it1 = obj1.keys();

            while (it1.hasNext()) {
                String key1 = it1.next();
                Log.d("vt","output2 "+key1);
                JSONObject obj2 = obj1.getJSONObject(key1);

                Iterator<String> it2 = obj2.keys();
                while (it2.hasNext()) {
                    String key2 = it2.next();
                    Log.d("vt","output3 "+key2);
                    final JSONObject obj3 = obj2.getJSONObject(key2);
                    String pieceLabel =          String.valueOf(obj3.get("pieceLabel"));
                    Log.d("vt","final "+pieceLabel);
                }
            }
        }

        }catch (Exception e){
            Log.d("vt","error  "+e.getMessage());
       }

答案 2 :(得分:0)

我通过https://jsoneditoronline.org传递了您的json,但未发现任何错误。

我使用了json-simple,可以在这里找到: https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple/1.1.1

我的示例在下面,没有循环。

public static void main( String[] args ) {

    JSONParser parser = new JSONParser();
    JSONObject jobj = null;
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(".json"),"UTF-8"));
        jobj = (JSONObject) parser.parse(reader);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }

    JSONObject jobj2 = (JSONObject) jobj.get("object"); // outer object.
    JSONObject jobj3 = (JSONObject) jobj2.get("17"); // nested object.
    JSONObject jobj4 = (JSONObject) jobj3.get("381"); // nested object.
    JSONObject jobj5 = (JSONObject) jobj4.get("19"); // nested object.
    System.out.println(jobj5.get("pieceLabel")); // Returns the value to where the specified key is mapped.

    JSONArray jsarry = (JSONArray) jobj.get("5"); // Json Array.
    JSONArray jsarry2 = (JSONArray) jsarry.get(0); // nested Json Array.
    JSONObject nestedjsobj = (JSONObject) jsarry2.get(0); // nested object.
    System.out.println(nestedjsobj.get("pieceLabel"));  // Returns the value to where the specified key is mapped.
}

输出:

Pièce principale
Pièce principale

答案 3 :(得分:0)

您的Json代码格式不正确... 请使用下面提供的插件检查代码

http://jsonviewer.stack.hu/