java - Parsing a complex JSON file -


i need parse json file , put data html table. using gwt application, data shall read file on server side , passed client on page load.

the format jsonobjects in file follows:

{   "object 1": [ { "value1": [ "subkey1", "subvalue2" ], "value2": "val", "value3": { "key1": val1, "key2": val2, "key3": val3} }, { "value2": [ "subkey1", "subvalue2" ], "value2": "val", "value3": { "key1": val1, "key2": val2, "key3": val3} }, .... .... ],  "object 2": [ { "value1": [ "subkey1", "subvalue2" ], "value2": "val", "value3": { "key1": val1, "key2": val2, "key3": val3} }, { "value2": [ "subkey1", "subvalue2" ], "value2": "val", "value3": { "key1": val1, "key2": val2, "key3": val3} }, .... .... ],  ....  } 

up until now, have done simple json parsing. problem having here data working has unique name each object cannot seem parse them array of jsonobjects.

i have attempted parse them (using json simple) way throwing error.

    try {         jsonparser parser = new jsonparser();         jsonobject obj;             obj = (jsonobject) parser.parse(new filereader("file.json"));          jsonarray array = new jsonarray();         array.add(obj.get("object1"));         array.add(obj.get("object2"));         array.add(obj.get("object3"));         array.add(obj.get("object4"));          jsonobject jo;          (object o : array) {             jo = (jsonobject) o;         }          } catch (filenotfoundexception e) {             e.printstacktrace();         } catch (ioexception e) {             e.printstacktrace();         } catch (parseexception e) {             e.printstacktrace();         }  

but throws error:

    org.json.simple.jsonarray cannot cast org.json.simple.jsonobject 

another method understanding create pojo class objects since each jsonobject has different identifier, mean each object must have own unique class? json2java methods have used create new class each of them.

you can check instance of object before casting it:

for (object obj : array) {     if (obj instanceof jsonarray) {        // it's array        yourjsonarray = (jsonarray)obj;     } else if (obj instanceof jsonobject) {        // it's object        yourjsonobject = (jsonobject)obj;     } else {        // it's string, number...     }  } 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -