java - org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject -


i'm trying parse below json file:

{"units":[{"id":42,                   "title":"hello world",               "position":1,     "v_id":9,     "sites":[[{"id":316,       "article":42,       "clip":133904         }],        {"length":5}]     }, ..]} 

this have tried:

object obj = null; jsonparser parser = new jsonparser(); object unitsobj = parser.parse(new filereader("file.json"); jsonobject unitsjson = (jsonobject) unitsobj;  jsonarray units = (jsonarray) unitsjson.get("units"); iterator<string> unitsiterator = units.iterator(); while(unitsiterator.hasnext()){              object ujson = unitsiterator.next();     jsonobject uj = (jsonobject) ujson;     obj =  parser.parse(uj.get("sites").tostring());     jsonarray jsonsites = (jsonarray)  obj;      for(int i=0;i<jsonsites.size();i++){      jsonobject site = (jsonobject)jsonsites.get(i); // exception happens here.      system.out.println(site.get("article");     } } 

the code not working when try parse inner json array, get:

exception in thread "main" java.lang.classcastexception: org.json.simple.jsonarray cannot cast org.json.simple.jsonobject 

the exception pointing line:

jsonobject site = (jsonobject)jsonsites.get(i); 

any help? tnx.

i've found working code:

jsonparser parser = new jsonparser(); object obj  = parser.parse(content); jsonarray array = new jsonarray(); array.add(obj); 

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -