Friday, 4 May 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

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...