Friday, May 4, 2018

json - Retrieve name of fields of a JSONObject - Java

I have to retrieve all name of fields of JSONObjectin Java.

For example, this is a JSON:



{
"name" : {
"first" : "John",
"last" : "McCarthy"
},
"birth" : ISODate("1927-09-04T04:00:00.000Z"),
"death" : ISODate("2011-12-24T05:00:00.000Z"),
"contribs" : [

"Lisp",
"Artificial Intelligence",
"ALGOL"
]
}


This JSON object is just an example, I can't know what a JSONObject contains since I import it. I don't have any information about the name of fields to use Gson/Jackson to deserialization.



Suppose I have a JSONObject myDocJson that contains the previous JSON Object, I use an iterator:




Iterator keys = myDocJson.keySet().iterator();
while(keys.hasNext()) {
System.out.println(keys.next());
}


And as a result I get:



name

birth
death
contribs


But for example I don't have name.first and name.last. So this iterator does not work with innested Object.

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...