I have this JSON response:
"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
}
}
}
}
}
I want to access pieceLabel in each element. Here’s what I’ve tried so far:
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"));
}
}
}
-
1This JSON is not valid, man...nyarian– nyarian2018年11月29日 17:57:09 +00:00Commented Nov 29, 2018 at 17:57
-
Yeah, it's not validDoctiger– Doctiger2018年11月29日 18:00:02 +00:00Commented Nov 29, 2018 at 18:00
-
@AndreyIlyunin Why not?OneCricketeer– OneCricketeer2018年11月29日 18:20:37 +00:00Commented Nov 29, 2018 at 18:20
-
@cricket_007 , now it's valid, watch edit historynyarian– nyarian2018年11月29日 18:23:15 +00:00Commented Nov 29, 2018 at 18:23
-
I can't even understand the structure if this JSON. I suppose that pattern matching is the only option here.nyarian– nyarian2018年11月29日 18:26:32 +00:00Commented Nov 29, 2018 at 18:26
3 Answers 3
Please check your json its having issue. You can use many tools online to check your json if its proper or not. I usually use https://codebeautify.org/jsonviewer
Corrected Json
[
[
{
"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
}
}
}
}
Final Code
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());
}
Comments
You need to iterate your unstructured json recursively and check every key.
Here is a working example to show you how to achieve what you want to do:
public class Main {
private static List<String> values = new ArrayList();
public static void main(String[] args) {
try {
JSONObject jsonObject = new JSONObject(new String(Files.readAllBytes(Paths.get("test.json"))));
findValues(jsonObject);
values.forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
}
private static void findValues(Object root) {
if (root instanceof JSONObject) {
Iterator<String> keys = ((JSONObject)root).keys();
while (keys.hasNext()) {
String key = keys.next();
if ("pieceLabel".equals(key)) {
values.add(((JSONObject)root).getString(key));
}
else {
Object o = ((JSONObject)root).get(key);
findValues(o);
}
}
}
else if (root instanceof JSONArray) {
for (Object o : (JSONArray)root) {
findValues(o);
}
}
}
}
test.json file contains your example json.
Output is :
Pièce principale
Pièce principale
Change the values and order of keys and see the result.
Comments
I passed your json through https://jsoneditoronline.org and did not find any error.
I used json-simple which can be found here: https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple/1.1.1
My example is below with no loops.
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.
}
output:
Pièce principale
Pièce principale