Org.json.simple.JSONArray cannot be attributed to org.json.simple.JSONObject

I am trying to parse below json file:

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

Here is what I 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 does not work when I try to parse the internal json array , so I get:

 Exception in thread "main" java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject 

The exception points to this line:

 JSONObject site = (JSONObject)jsonSites.get(i); 

Any help? Hh

+12
java json
source share
10 answers

I found the working code:

 JSONParser parser = new JSONParser(); Object obj = parser.parse(content); JSONArray array = new JSONArray(); array.add(obj); 
+19
source share

The first element of the sites array is the array, as you can see the JSON indent:

 {"units":[{"id":42, ... "sites": [ [ { "id":316, "article":42, "clip":133904 } ], {"length":5} ] ... } 

Therefore, you should relate to its value accordingly; you could probably do something like:

 JSONObject site = (JSONObject)(((JSONArray)jsonSites.get(i)).get(0)); 
+6
source share

this worked:

 System.out.println("resultList.toString() " + resultList); org.json.JSONObject obj = new JSONObject(resultList); org.json.JSONArray jsonArray = obj.getJSONArray(someField); for(int i=0;i<jsonArray.length();i++){ System.out.println("array is " + jsonArray.get(i)); } 
+2
source share

JSONObject site=jsonSites.getJSONObject(i) should work

+1
source share
 JSONObject obj=(JSONObject)JSONValue.parse(content); JSONArray arr=(JSONArray)obj.get("units"); System.out.println(arr.get(1)); //this will print {"id":42,...sities ..} 

@cyberz is right, but explain it the other way around

+1
source share

use jsonsimpleobject direclty as below

 JSONObject unitsObj = parser.parse(new FileReader("file.json"); 
0
source share

You can read the entire contents of the file in String first.

 FileInputStream fileInputStream = null; String data=""; StringBuffer stringBuffer = new StringBuffer(""); try{ fileInputStream=new FileInputStream(filename); int i; while((i=fileInputStream.read())!=-1) { stringBuffer.append((char)i); } data = stringBuffer.toString(); } catch(Exception e){ LoggerUtil.printStackTrace(e); } finally{ if(fileInputStream!=null){ fileInputStream.close(); } } 

You will now have all the content in a String (data variable).

 JSONParser parser = new JSONParser(); org.json.simple.JSONArray jsonArray= (org.json.simple.JSONArray) parser.parse(data); 

After that, you can use jsonArray as you want.

0
source share
 JSONObject baseReq LinkedHashMap insert = (LinkedHashMap) baseReq.get("insert"); LinkedHashMap delete = (LinkedHashMap) baseReq.get("delete"); 
0
source share

If you want to re-filter the JSON data, you can use the following method. This example gets all the document data from couchdb.

 { Gson gson = new Gson(); String resultJson = restTemplate.getForObject(url+"_all_docs?include_docs=true", String.class); JSONObject object = (JSONObject) new JSONParser().parse(resultJson); JSONArray rowdata = (JSONArray) object.get("rows"); List<Object>list=new ArrayList<Object>(); for(int i=0;i<rowdata.size();i++) { JSONObject index = (JSONObject) rowdata.get(i); JSONObject data = (JSONObject) index.get("doc"); list.add(data); } // convert your list to json String devicelist = gson.toJson(list); return devicelist; } 
0
source share
 JSONObject site = (JSONObject)jsonSites.get(i); // Exception happens here. 

The return type of jsonSites.get(i) is JSONArray , not JSONObject . Since sites have two '[', two means that there are two arrays.

0
source share

All Articles