How to repeat this JSON array using Java and org.json in Android?

My JSON string looks like this (contained in the sJSON string variable):

 [ {"id":284} ], [ {"name":John,"surname":Doe}, {"name":Jane,"surname":Doe} ] 

I can parse the first array like this (using Java and importing org.json):

 JSONArray arrJSON = new JSONArray(sJSON); JSONObject jsonGeneralData = new JSONObject(arrJSON.get(0).toString()); String sResult = jsonGeneralData.get("id").toString(); 

This returns the expected result, which is 284. I'm struggling to get the second array of elements and scroll through them. I am not sure if my JSON string is incorrect, or if I try to get the wrong path to it. Here is what I tried:

 JSONObject jsonPersonData = new JSONObject(arrJSON.get(1).toString()); 

This, as I understand it, I can’t figure out how to iterate over individual elements inside the second array.

EDIT:

This line seems to only parse the first line in square brackets:

 JSONArray arrJSON = new JSONArray(sJSON); 

Or is JSON wrong (the same example as above), or doesn't it parse it correctly? I managed to solve the problem by splitting it into a string and putting them each in my own JSONArray , but I don't think this is the best way to do something.

+7
source share
4 answers

You want something like this.

 JSONArray jsonArray = new JSONArray(sJSON); JSONArray jsonPersonData = jsonArray.getJSONArray(1); for (int i=0; i<jsonPersonData.length(); i++) { JSONObject item = jsonPersonData.getJSONObject(i); String name = item.getString("name"); String surname = item.getString("surname"); } 
+20
source

You are not using JSONObject.toString. So you should iterate over the array:

 for(int i=0;i<arrJSON.length();i++){ JSONObject json_data = jArray.getJSONObject(i); String name =json_data.getString("name"); String surname =json_data.getString("surname"); } 
+5
source

You should access the data using the arrJson object without creating new JSONObjects with string results from them.

+3
source

You can iterate using Iterator, I also copied the JSON sample for you, hope this helps:

  // { "drives": //[{"drives_id":"2","ip":"hq10m.backupexample.com","port":"4010","ftp_username":"NameSFTP"," // ftp_password":"12345","permission":2,"drive_name":"dev" //,"drive_letter":"S","size":"1234","ma_mode":"0"}, //{"drives_id":"3","ip":"hq6m.backupexample.com","port":"4206","ftp_username":"userSFTP" //,"ftp_password":"456","permission":2,"drive_name":"Rashtesting" //,"drive_letter":"P","size":"8","ma_mode":"0"}],"user_id":"8"} mi.setUser_id( (int) Long.parseLong(obj.get("user_id").toString())); JSONArray drivesarray= (JSONArray) obj.get("drives"); Iterator<JSONObject> driveIterator=drivesarray.iterator(); while(driveIterator.hasNext()) { JSONObject driveJSON=driveIterator.next(); client_drive_info drive=new client_drive_info(); drive.setDrives_id((int) Long.parseLong(driveJSON.get("drives_id").toString())); drive.setIp(driveJSON.get("ip" ).toString()); drive.setPort((int )Long.parseLong(driveJSON.get("port").toString())); drive.setSFTPusername(driveJSON.get("ftp_username").toString()); drive.setSFTPpassword(driveJSON.get( "ftp_password").toString()); drive.setPermission((int )Long.parseLong(driveJSON.get("permission").toString())); drive.setDrive_name(driveJSON.get("drive_name").toString()); drive.setDrive_letter(driveJSON.get("drive_letter").toString()); drive.setSize((int )Long.parseLong(driveJSON.get("size").toString())); mi.getDrives_info().add(drive); } 
+2
source

All Articles