I have Json string, which contain “key” : “value” pairs and i need to know keys at this string using Java and importing org.json. I tried to use iterator, but first pair key prints in the end.
for example string:
{
"firstName": "sam",
"lastName": "Smith",
"address": {
"streetAddress": "somestreet",
"city": "somecity",
"postalCode": 101101
},
"phoneNumbers": [
"812 123-1234",
"916 123-4567"
]
}
I want to print:
”
firstName
lastName
address
phoneNumbers
“.
But i have:
“lastName
address
phoneNumbers
firstName”
JSONObject JO = new JSONObject(JsonString);
Iterator<String> It = JO.keys();
while (It.hasNext()){
System.out.println(It.next());
}
From the JavaDoc:
If you want to order the keys you must 1) place them in a list and 2) order the list according your needs. Keep in mind that this is a key/value representation of data – the sequence of elements should not matter.