2

I have a json string as shown below

{
"college": {
    "id": "RPD4007",
    "address": "#302, 1 st cross"
},
"deparment": {
    "department1": {
        "name": {
            "maths": {
                "chapter": 1,
                "name": "algebra",
                "module_id": "01"
            },
            "electronics": {
                "chapter": 1,
                "name": "ec",
                "module_id": "01"
            }
        }
    },
    "department2": {
        "name": {
            "english": {
                "chapter": 2,
                "name": "algebra",
                "module_id": "02"
            },
            "electrical": {
                "chapter": 2,
                "name": "algebra",
                "module_id": "02"
            }
        }
    }
}
}

I have tried to convert this json sring to json object ,

string json_string = EntityUtils.toString(response.getEntity());
jObj = new JSONObject(json_string);//json object
JSONObject object = jobj.getJSONObject("college");

But the jobj output I got is in reverse order of json string .Like below,

{
"college": {
    "id": "RPD4007",
    "address": "#302, 1 st cross"
},
"deparment": {
    "department2": {
        "name": {
            "electrical": {
                "chapter": 2,
                "name": "algebra",
                "module_id": "02"
            },
            "english": {
                "chapter": 2,
                "name": "algebra",
                "module_id": "02"
            }
        }
    },
    "department1": {
        "name": {
            "electronics": {
                "chapter": 1,
                "name": "ec",
                "module_id": "01"
            },
            "maths": {
                "chapter": 1,
                "name": "algebra",
                "module_id": "01"
            }
        }
    }
}
}

How to get it in same order as it is?

Dhanya Santhosh
  • 101
  • 1
  • 17

2 Answers2

0

I think you are making jsonObject for two times. Just do something like this you have String that contains JSON DATA Make an object for that which you did

string json_string = EntityUtils.toString(response.getEntity());
jObj = new JSONObject(json_string);//json object

then make a loop to get the object inside that object using

 String college = jobj.getString("college");
Harsh Patel
  • 1,056
  • 2
  • 10
  • 20
-1

The answers here: JSON order mixed up

As a consequence, JSON libraries are free to rearrange the order of the elements as they see fit. This is not a bug.

Community
  • 1
  • 1
AndroidLTG
  • 91
  • 1
  • 11