How to convert JSONObjects to JSONArray? - java
I have a response like this:
{
"songs":{
"2562862600":{"id":"2562862600""pos":1},
"2562862620":{"id":"2562862620""pos":1},
"2562862604":{"id":"2562862604""pos":1},
"2573433638":{"id":"2573433638""pos":1}
}
}
Here is my code:
List<NameValuePair> param = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(url, "GET", param);
JSONObject songs= json.getJSONObject("songs");
How do I convert "songs" to a JSONArray?
Something like this:
JSONObject songs= json.getJSONObject("songs");
Iterator x = songs.keys();
JSONArray jsonArray = new JSONArray();
while (x.hasNext()){
String key = (String) x.next();
jsonArray.put(songs.get(key));
}
Even shorter and with json-functions:
JSONObject songsObject = json.getJSONObject("songs");
JSONArray songsArray = songsObject.toJSONArray(songsObject.names());
Your response should be something like this to be qualified as Json Array.
{
"songs":[
{"2562862600": {"id":"2562862600", "pos":1}},
{"2562862620": {"id":"2562862620", "pos":1}},
{"2562862604": {"id":"2562862604", "pos":1}},
{"2573433638": {"id":"2573433638", "pos":1}}
]
}
You can parse your response as follows
String resp = ...//String output from your source
JSONObject ob = new JSONObject(resp);
JSONArray arr = ob.getJSONArray("songs");
for(int i=0; i<arr.length(); i++){
JSONObject o = arr.getJSONObject(i);
System.out.println(o);
}
To deserialize the response need to use HashMap:
String resp = ...//String output from your source
Gson gson = new GsonBuilder().create();
gson.fromJson(resp,TheResponse.class);
class TheResponse{
HashMap<String,Song> songs;
}
class Song{
String id;
String pos;
}
JSONObject songs= json.getJSONObject("songs");
use JSONArray for direct conversion
JSONArray array = (JSONArray)songs.get("songs");
Related
How to convert a JSON string into a JSON arraylist? [duplicate]
This question already has answers here: Convert string to JSON array (10 answers) Closed 4 years ago. I have a String and i want to show it in list so i have to convert it in Array list How can i do this? I have tried these things String Result =response.body().getGetFlightAvailibilityResponse().getGetFlightAvailibilityResult().getGetFlightAvailibilityResponse().getFlightDetails(); try { Gson gson = new Gson(); Type type = new TypeToken<List<Object>>() {}.getType(); List<String> contactList = gson.fromJson(Result, type); // JSONArray jsonArray = new JSONArray(Result); } catch (Exception e) { e.printStackTrace(); } This is my String [[SrNo,AirlineCode,FlightNo,FromAirportCode,ToAirportCode,DepDate,DepTime,ArrDate,ArrTime,FlightClass,FlightTime,TotalAmount,TaxAmount,Stops,ValCarrier,FromTerminal,ToTerminal,MainClass,FareBasis,AgencyCharge,FareType,AvailSeats,FlightRemarks,TrackNo],[100YO,9W,346,BOM,DEL,04/10/2018,07:30,04/10/2018,09:40,O,130,3998,1798,0,9W,2, 3,Y,,0,N,0,,0$307|37|100YO],[101YO,9W,953,BOM,JAI,04/10/2018,14:35,04/10/2018,16:30,O,115,4550,1949,1,9W,2, 2,Y,,0,N,0,,0$307|37|101YO],[101YO,9W,788,JAI,DEL,04/10/2018,17:00,04/10/2018,18:05,O,65,0,0,0,9W,2, 3,Y,,0,N,0,,0$307|37|101YO],[102YO,9W,2418,BOM,JAI,04/10/2018,18:20,04/10/2018,20:10,O,110,4550,1949,1,9W,2, 2,Y,,0,N,0,,0$307|37|102YO],[102YO,9W,756,JAI,DEL,04/10/2018,21:15,04/10/2018,22:20,O,65,0,0,0,9W,2, 3,Y,,0,N,0,,0$307|37|102YO],[103YO,9W,309,BOM,DEL,04/10/2018,19:00,04/10/2018,21:15,V,135,4995,1845,0,9W,2, 3,Y,,0,N,0,,0$307|37|103YO],[104YO,9W,2374,BOM,LKO,04/10/2018,12:25,04/10/2018,14:45,W,140,5053,2573,1,9W,2, 2,Y,,0,N,0,,0$307|37|104YO],[104YO,9W,856,LKO,DEL,04/10/2018,15:40,04/10/2018,17:00,W,80,0,0,0,9W,2, 3,Y,,0,N,0,,0$307|37|104YO],[105YO,9W,2929,BOM,AMD,04/10/2018,02:50,04/10/2018,04:00,H,70,5315,1985,1,9W,2, 1,Y,,0,N,0,,0$307|37|105YO],[105YO,9W,685,AMD,DEL,04/10/2018,05:35,04/10/2018,07:20,H,105,0,0,0,9W,1, 3,Y,,0,N,0,,0$307|37|105YO],[106YO,9W,385,BOM,IDR,04/10/2018,17:35,04/10/2018,18:55,V,80,6229,2029,1,9W,2,,Y,,0,N,0,,0$307|37|106YO],[106YO,9W,794,IDR,DEL,04/10/2018,20:20,04/10/2018,22:50,V,150,0,0,0,9W,, 3,Y,,0,N,0,,0$307|37|106YO],[107YO,9W,2377,BOM,RPR,04/10/2018,06:35,04/10/2018,08:30,V,115,6329,2034,1,9W,2,,Y,,0,N,0,,0$307|37|107YO],[107YO,9W,2735,RPR,DEL,04/10/2018,09:00,04/10/2018,10:50,V,110,0,0,0,9W,, 3,Y,,0,N,0,,0$307|37|107YO],[108YO,9W,895,BOM,BHO,04/10/2018,18:40,04/10/2018,20:10,V,90,6743,2053,1,9W,2,,Y,,0,N,0,,0$307|37|108YO],[108YO,9W,928,BHO,DEL,04/10/2018,21:00,04/10/2018,22:25,V,85,0,0,0,9W,, 3,Y,,0,N,0,,0$307|37|108YO],[109YO,9W,2967,BOM,RPR,04/10/2018,17:25,04/10/2018,19:25,V,120,7016,2066,1,9W,2,,Y,,0,N,0,,0$307|37|109YO],[109YO,9W,2677,RPR,DEL,04/10/2018,20:05,04/10/2018,21:55,V,110,0,0,0,9W,, 3,Y,,0,N,0,,0$307|37|109YO],[10YO,SG,153,BOM,COK,04/10/2018,09:30,04/10/2018,11:30,C,120,7334,725,1,SG,1, 1,Y,,500,R,0,,0$307|37|10YO],[10YO,SG,184,COK,DEL,04/10/2018,13:45,04/10/2018,18:00,C,255,0,0,1,SG,1,,Y,,0,R,0,,0$307|37|10YO],[110YO,9W,413,BOM,BLR,04/10/2018,16:30,04/10/2018,18:15,H,105,7724,2959,1,9W,2,,Y,,0,N,0,,0$307|37|110YO],[110YO,9W,833,BLR,DEL,04/10/2018,18:55,04/10/2018,21:50,H,175,0,0,0,9W,, 3,Y,,0,N,0,,0$307|37|110YO],[111YO,9W,703,BOM,DED,04/10/2018,11:00,04/10/2018,13:15,K,135,11531,2881,1,9W,2,,Y,,0,N,0,,0$307|37|111YO],[111YO,9W,150,DED,DEL,04/10/2018,13:50,04/10/2018,15:10,K,80,0,0,0,9W,, 3,Y,,0,N,0,,0$307|37|111YO],[112YO,9W,701,BOM,DED,04/10/2018,13:05,04/10/2018,15:20,M,135,36278,4060,1,9W,2,,Y,,0,N,0,,0$307|37|112YO],[112YO,9W,186,DED,DEL,04/10/2018,17:10,04/10/2018,18:20,M,70,0,0,0,9W,, 3,Y,,0,N,0,,0$307|37|112YO],[11YO,SG,153,BOM,COK,04/10/2018,09:30,04/10/2018,11:30,C,120,9854,1245,1,SG,1, 1,Y,,500,R,0,,0$307|37|11YO],[11YO,SG,184,COK,DEL,04/10/2018,13:45,04/10/2018,18:00,C,255,0,0,1,SG,1,,Y,,0,R,0,,0$307|37|11YO],[12YO,SG,153,BOM,COK,04/10/2018,09:30,04/10/2018,11:30,E,120,10274,1265,1,SG,1, 1,Y,,500,R,0,,0$307|37|12YO],[12YO,SG,184,COK,DEL,04/10/2018,13:45,04/10/2018,18:00,C,255,0,0,1,SG,1,,Y,,0,R,0,,0$307|37|12YO],[13YO,AI,217,BOM,DEL,04/10/2018,10:25,04/10/2018,11:45,S,80,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|13YO],[14YO,AI,61,BOM,DEL,04/10/2018,01:00,04/10/2018,02:30,S,90,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|14YO],[15YO,AI,66,BOM,DEL,04/10/2018,01:00,04/10/2018,02:30,S,90,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|15YO],[16YO,AI,67,BOM,DEL,04/10/2018,01:00,04/10/2018,02:30,S,90,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|16YO],[17YO,AI,107,BOM,DEL,04/10/2018,01:00,04/10/2018,02:30,S,90,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|17YO],[18YO,AI,212,BOM,DEL,04/10/2018,01:00,04/10/2018,02:30,S,90,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|18YO],[19YO,AI,323,BOM,DEL,04/10/2018,01:00,04/10/2018,02:30,S,90,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|19YO],[1YO,SG,160,BOM,DEL,04/10/2018,06:00,04/10/2018,07:55,H,115,2811,711,0,SG,1, 1C,Y,,500,R,0,,0$307|37|1YO],[20YO,AI,326,BOM,DEL,04/10/2018,01:00,04/10/2018,02:30,S,90,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|20YO],[21YO,AI,455,BOM,DEL,04/10/2018,01:00,04/10/2018,02:30,S,90,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|21YO],[22YO,AI,523,BOM,DEL,04/10/2018,01:00,04/10/2018,02:30,S,90,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|22YO],[23YO,AI,752,BOM,DEL,04/10/2018,01:00,04/10/2018,02:30,S,90,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|23YO],[24YO,AI,789,BOM,DEL,04/10/2018,01:00,04/10/2018,02:30,S,90,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|24YO],[25YO,AI,801,BOM,DEL,04/10/2018,01:00,04/10/2018,02:30,S,90,4255,555,0,AI,2,,Y,,0,N,0,,0$307|37|25YO],[26YO,AI,802,BOM,DEL,04/10/2018,01:00,04/10/2018,02:30,S,90,4255,555,0,AI,, 3,Y,,0,N,0,,0$307|37|26YO],[27YO,AI,1111,BOM,DEL,04/10/2018,01:00,04/10/2018,02:30,S,90,4255,555,0,AI,, 3,Y,,0,N,0,,0$307|37|27YO],[28YO,AI,300,BOM,DEL,04/10/2018,03:00,04/10/2018,04:30,S,90,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|28YO],[29YO,AI,256,BOM,DEL,04/10/2018,00:10,04/10/2018,01:45,S,95,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|29YO],[2YO,SG,160,BOM,DEL,04/10/2018,06:00,04/10/2018,07:55,C,115,3021,521,0,SG,1, 1C,Y,,500,R,0,,0$307|37|2YO],[30YO,AI,257,BOM,DEL,04/10/2018,00:10,04/10/2018,01:45,S,95,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|30YO],[31YO,AI,5011,BOM,DEL,04/10/2018,00:10,04/10/2018,02:10,S,120,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|31YO],[32YO,AI,97,BOM,DEL,04/10/2018,06:00,04/10/2018,08:00,S,120,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|32YO],[33YO,AI,98,BOM,DEL,04/10/2018,06:00,04/10/2018,08:00,S,120,4255,555,0,AI,2, 3,Y,,0,N,0,,0$307|37|33YO],[34YO,AI,99,BOM,DEL,04/10/2018,06:00,04/10/2018,08:00,S,120,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|34YO],[35YO,AI,4141,BOM,DEL,04/10/2018,10:00,04/10/2018,12:00,S,120,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|35YO],[36YO,AI,3,BOM,DEL,04/10/2018,12:00,04/10/2018,14:00,S,120,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|36YO],[37YO,AI,864,BOM,DEL,04/10/2018,07:00,04/10/2018,09:05,S,125,4255,555,0,AI,2, 3,Y,,0,N,0,,0$307|37|37YO],[38YO,AI,144,BOM,DEL,04/10/2018,17:00,04/10/2018,19:10,S,130,4255,555,0,AI,2, 3,Y,,0,N,0,,0$307|37|38YO],[39YO,AI,101,BOM,DEL,04/10/2018,21:00,04/10/2018,23:10,S,130,4255,555,0,AI,2, 3,Y,,0,N,0,,0$307|37|39YO],[3YO,SG,152,BOM,DEL,04/10/2018,07:20,04/10/2018,09:35,H,135,2811,711,0,SG,1, 1C,Y,,500,R,0,,0$307|37|3YO],[40YO,AI,200,BOM,DEL,04/10/2018,21:00,04/10/2018,23:10,S,130,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|40YO],[41YO,AI,232,BOM,DEL,04/10/2018,05:15,04/10/2018,07:30,S,135,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|41YO],[42YO,AI,348,BOM,DEL,04/10/2018,07:55,04/10/2018,10:10,S,135,4255,555,0,AI,2, 3,Y,,0,N,0,,0$307|37|42YO],[43YO,AI,806,BOM,DEL,04/10/2018,08:00,04/10/2018,10:15,S,135,4255,555,0,AI,2, 3,Y,,0,N,0,,0$307|37|43YO],[44YO,AI,809,BOM,DEL,04/10/2018,10:00,04/10/2018,12:15,S,135,4255,555,0,AI,2, 3,Y,,0,N,0,,0$307|37|44YO],[45YO,AI,677,BOM,DEL,04/10/2018,13:00,04/10/2018,15:15,S,135,4255,555,0,AI,2, 3,Y,,0,N,0,,0$307|37|45YO],[46YO,AI,687,BOM,DEL,04/10/2018,16:00,04/10/2018,18:15,S,135,4255,555,0,AI,2, 3,Y,,0,N,0,,0$307|37|46YO],[47YO,AI,310,BOM,DEL,04/10/2018,19:45,04/10/2018,22:00,S,135,4255,555,0,AI,2, 3,Y,,0,N,0,,0$307|37|47YO],[48YO,AI,866,BOM,DEL,04/10/2018,09:00,04/10/2018,11:25,S,145,4255,555,0,AI,2, 3,Y,,0,N,0,,0$307|37|48YO],[49YO,AI,660,BOM,DEL,04/10/2018,18:00,04/10/2018,20:25,S,145,4255,555,0,AI,2, 3,Y,,0,N,0,,0$307|37|49YO],[4YO,SG,152,BOM,DEL,04/10/2018,07:20,04/10/2018,09:35,C,135,3021,521,0,SG,1, 1C,Y,,500,R,0,,0$307|37|4YO],[50YO,AI,555,BOM,DEL,04/10/2018,01:00,04/10/2018,03:30,S,150,4255,555,0,AI,2, 3,Y,,0,N,0,,0$307|37|50YO],[51YO,AI,666,BOM,DEL,04/10/2018,01:00,04/10/2018,03:30,S,150,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|51YO],[52YO,AI,2123,BOM,DEL,04/10/2018,01:00,04/10/2018,03:30,S,150,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|52YO],[53YO,AI,150,BOM,DEL,04/10/2018,05:00,04/10/2018,07:30,S,150,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|53YO],[54YO,AI,100,BOM,DEL,04/10/2018,07:00,04/10/2018,09:30,S,150,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|54YO],[55YO,AI,85,BOM,DEL,04/10/2018,12:00,04/10/2018,14:30,S,150,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|55YO],[56YO,AI,5190,BOM,DEL,04/10/2018,21:00,04/10/2018,23:30,S,150,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|56YO],[57YO,AI,5191,BOM,DEL,04/10/2018,21:00,04/10/2018,23:30,S,150,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|57YO],[58YO,AI,250,BOM,DEL,04/10/2018,01:00,04/10/2018,04:00,S,180,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|58YO],[59YO,AI,2323,BOM,DEL,04/10/2018,01:00,04/10/2018,04:00,S,180,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|59YO],[5YO,SG,158,BOM,DEL,04/10/2018,18:45,04/10/2018,20:55,H,130,2811,711,0,SG,1, 1C,Y,,500,R,0,,0$307|37|5YO],[60YO,AI,5578,BOM,DEL,04/10/2018,20:00,04/10/2018,23:00,S,180,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|60YO],[61YO,AI,5580,BOM,DEL,04/10/2018,20:00,04/10/2018,23:00,S,180,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|61YO],[62YO,AI,413,BOM,DEL,04/10/2018,20:15,04/10/2018,23:30,S,195,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|62YO],[63YO,AI,423,BOM,DEL,04/10/2018,20:15,04/10/2018,23:30,S,195,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|63YO],[64YO,AI,777,BOM,DEL,04/10/2018,20:15,04/10/2018,23:30,S,195,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|64YO],[65YO,AI,1066,BOM,DEL,04/10/2018,20:15,04/10/2018,23:30,S,195,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|65YO],[66YO,AI,635,BOM,DEL,04/10/2018,07:05,04/10/2018,11:10,S,245,4255,555,0,AI,2, 3,Y,,0,N,0,,0$307|37|66YO],[67YO,AI,7019,BOM,DEL,04/10/2018,06:00,04/10/2018,08:00,S,120,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|67YO],[68YO,AI,6293,BOM,DEL,04/10/2018,21:00,04/10/2018,23:30,S,150,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|68YO],[69YO,AI,9823,BOM,DEL,04/10/2018,01:00,04/10/2018,04:30,S,210,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|69YO],[6YO,SG,158,BOM,DEL,04/10/2018,18:45,04/10/2018,20:55,C,130,3021,521,0,SG,1, 1C,Y,,500,R,0,,0$307|37|6YO],[70YO,AI,9897,BOM,DEL,04/10/2018,01:00,04/10/2018,04:30,S,210,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|70YO],[71YO,AI,9824,BOM,DEL,04/10/2018,01:00,04/10/2018,05:00,S,240,4255,555,0,AI,,,Y,,0,N,0,,0$307|37|71YO],[72YO,AI,888,BOM,DEL,04/10/2018,19:00,04/10/2018,21:15,S,135,4255,555,0,AI,2, 3,Y,,0,N,0,,0$307|37|72YO],[73YO,AI,442,BOM,DEL,04/10/2018,15:25,04/10/2018,19:10,K,225,12865,965,0,AI,2, 3,Y,,0,N,0,,0$307|37|73YO],[74YO,AI,621,BOM,DEL,04/10/2018,00:20,04/10/2018,02:00,M,100,19532,1282,0,AI,2,,Y,,0,N,0,,0$307|37|74YO],[75YO,AI,4012,BOM,DEL,04/10/2018,06:00,04/10/2018,08:00,M,120,19532,1282,0,AI,,,Y,,0,N,0,,0$307|37|75YO],[76YO,AI,4015,BOM,DEL,04/10/2018,06:00,04/10/2018,08:00,M,120,19532,1282,0,AI,,,Y,,0,N,0,,0$307|37|76YO],[77YO,AI,4017,BOM,DEL,04/10/2018,06:00,04/10/2018,08:00,M,120,19532,1282,0,AI,,,Y,,0,N,0,,0$307|37|77YO],[78YO,AI,5353,BOM,DEL,04/10/2018,06:00,04/10/2018,08:00,M,120,19532,1282,0,AI,,,Y,,0,N,0,,0$307|37|78YO],[79YO,AI,3131,BOM,DEL,04/10/2018,12:00,04/10/2018,14:00,M,120,19532,1282,0,AI,,,Y,,0,N,0,,0$307|37|79YO],[7YO,SG,154,BOM,DEL,04/10/2018,21:30,04/10/2018,23:50,H,140,2811,711,0,SG,1, 1C,Y,,500,R,0,,0$307|37|7YO],[80YO,AI,8899,BOM,DEL,04/10/2018,07:30,04/10/2018,09:30,M,120,19532,1282,0,AI,,,Y,,0,N,0,,0$307|37|80YO],[81YO,9W,361,BOM,DEL,04/10/2018,20:45,04/10/2018,22:55,B,130,2549,1729,0,9W,2, 3,Y,,0,N,0,,0$307|37|81YO],[82YO,9W,762,BOM,DEL,04/10/2018,06:00,04/10/2018,08:15,B,135,2549,1729,0,9W,2, 3,Y,,0,N,0,,0$307|37|82YO],[83YO,9W,301,BOM,DEL,04/10/2018,07:00,04/10/2018,09:15,B,135,2549,1729,0,9W,2, 3,Y,,0,N,0,,0$307|37|83YO],[84YO,9W,355,BOM,DEL,04/10/2018,08:30,04/10/2018,10:45,B,135,2549,1729,0,9W,2, 3,Y,,0,N,0,,0$307|37|84YO],[85YO,9W,333,BOM,DEL,04/10/2018,08:00,04/10/2018,10:20,B,140,2549,1729,0,9W,2, 3,Y,,0,N,0,,0$307|37|85YO],[86YO,9W,331,BOM,DEL,04/10/2018,10:00,04/10/2018,12:20,B,140,2549,1729,0,9W,2, 3,Y,,0,N,0,,0$307|37|86YO],[87YO,9W,311,BOM,DEL,04/10/2018,18:00,04/10/2018,20:20,B,140,2549,1729,0,9W,2, 3,Y,,0,N,0,,0$307|37|87YO],[88YO,9W,839,BOM,DEL,04/10/2018,09:00,04/10/2018,11:25,B,145,2549,1729,0,9W,2, 3,Y,,0,N,0,,0$307|37|88YO],[89YO,9W,307,BOM,DEL,04/10/2018,11:00,04/10/2018,13:20,B,140,2549,1729,0,9W,2, 3,Y,,0,N,0,,0$307|37|89YO],[8YO,SG,154,BOM,DEL,04/10/2018,21:30,04/10/2018,23:50,C,140,3021,521,0,SG,1, 1C,Y,,500,R,0,,0$307|37|8YO],[90YO,9W,351,BOM,DEL,04/10/2018,14:00,04/10/2018,16:10,B,130,2633,1733,0,9W,2, 3,Y,,0,N,0,,0$307|37|90YO],[91YO,9W,310,BOM,DEL,04/10/2018,22:35,05/10/2018,00:45,B,130,2633,1733,0,9W,2, 3,Y,,0,N,0,,0$307|37|91YO],[92YO,9W,329,BOM,AMD,04/10/2018,05:20,04/10/2018,06:30,B,70,2642,1858,1,9W,2, 1,Y,,0,N,0,,0$307|37|92YO],[92YO,9W,687,AMD,DEL,04/10/2018,07:00,04/10/2018,08:40,B,100,0,0,0,9W,1, 3,Y,,0,N,0,,0$307|37|92YO],[93YO,9W,353,BOM,DEL,04/10/2018,17:00,04/10/2018,19:15,W,135,3200,1760,0,9W,2, 3,Y,,0,N,0,,0$307|37|93YO],[94YO,9W,2929,BOM,AMD,04/10/2018,02:50,04/10/2018,04:00,W,70,3373,1893,1,9W,2, 1,Y,,0,N,0,,0$307|37|94YO],[94YO,9W,687,AMD,DEL,04/10/2018,07:00,04/10/2018,08:40,W,100,0,0,0,9W,1, 3,Y,,0,N,0,,0$307|37|94YO],[95YO,9W,914,BOM,AMD,04/10/2018,16:55,04/10/2018,18:15,W,80,3373,1893,1,9W,2, 1,Y,,0,N,0,,0$307|37|95YO],[95YO,9W,687,AMD,DEL,05/10/2018,07:00,05/10/2018,08:40,W,100,0,0,0,9W,1, 3,Y,,0,N,0,,0$307|37|95YO],[96YO,9W,914,BOM,AMD,04/10/2018,16:55,04/10/2018,18:15,W,80,3373,1893,1,9W,2, 1,Y,,0,N,0,,0$307|37|96YO],[96YO,9W,655,AMD,DEL,05/10/2018,15:10,05/10/2018,16:50,W,100,0,0,0,9W,1, 3,Y,,0,N,0,,0$307|37|96YO],[97YO,9W,637,BOM,AMD,04/10/2018,21:45,04/10/2018,23:05,W,80,3641,1906,1,9W,2, 1,Y,,0,N,0,,0$307|37|97YO],[97YO,9W,687,AMD,DEL,05/10/2018,07:00,05/10/2018,08:40,W,100,0,0,0,9W,1, 3,Y,,0,N,0,,0$307|37|97YO],[98YO,9W,637,BOM,AMD,04/10/2018,21:45,04/10/2018,23:05,W,80,3641,1906,1,9W,2, 1,Y,,0,N,0,,0$307|37|98YO],[98YO,9W,655,AMD,DEL,05/10/2018,15:10,05/10/2018,16:50,W,100,0,0,0,9W,1, 3,Y,,0,N,0,,0$307|37|98YO],[99YO,9W,339,BOM,DEL,04/10/2018,03:00,04/10/2018,05:05,O,125,3998,1798,0,9W,2, 3,Y,,0,N,0,,0$307|37|99YO],[9YO,SG,153,BOM,COK,04/10/2018,09:30,04/10/2018,11:30,H,120,3021,921,1,SG,1, 1,Y,,500,R,0,,0$307|37|9YO],[9YO,SG,184,COK,DEL,04/10/2018,13:45,04/10/2018,18:00,H,255,0,0,1,SG,1,,Y,,0,R,0,,0$307|37|9YO]]
Here, you are getting JSONObject so you can convert it on JSONArray jsonArray = new JSONArray(readlocationFeed); line with following JSONObject jsnobject = new JSONObject(readlocationFeed); and after JSONArray jsonArray = jsnobject.getJSONArray("locations"); for (int i = 0; i < jsonArray.length(); i++) { JSONObject explrObject = jsonArray.getJSONObject(i); } This object you can add on Arraylist.
This will help you. JSONArray jsonArrayList = new JSONArray(); ArrayList<String> label=new ArrayList<>(); JSONArray jsonArray = new JSONArray(readlocationFeed); if(jsonArray.length()>0){ for(int i=0;i<jsonArray.length();i++){ JSONArray jsonArrayForValue = new JSONArray(jsonArray.get(i)); if(i==0){ ////////**********For Label/Header*******/////////// for(int j=0;j<jsonArrayForValue.length();j++){ label.add(jsonArrayForValue.get(j)); } }else{ JSONObject eachDeatail=new JSONObject(); for(int j=0;j<jsonArrayForValue.length();j++){ eachDeatail.put(label.get(j),jsonArrayForValue.get(label.get(j)).toString()); } jsonArrayList.add(eachDeatail); } } }
Convert JSON into List of Object property (Java)
I have simple json which looks like this : [ { "id":"0", "name":"Bob", "place":"Colorado", }, { "id":"1", "name":"John", "place":"Chicago", }, { "id":"2", "name":"Marry", "place":"Miami", } ] What I want is using Java to create list of strings (List<String>) that contains all 'names'. I have some experience using Gson and I think about something like: Gson gson = new Gson(); String[] stringArray= gson.fromJson(jsonString, " ".class); The problem with this method is that I should create some POJO class which I didn`t in this case. Is it any way I can achieve it without creating separate class with this 'name' property ?
Using Jackson to parse, and Java 8 Streams API for extracting only the name field; the following may help you: // Your string jsonString = "[{ \"id\":\"0\", \"name\":\"Bob\", \"place\":\"Colorado\" }, { \"id\":\"1\", \"name\":\"John\", \"place\":\"Chicago\"}, { \"id\":\"2\", \"name\":\"Marry\", \"place\":\"Miami\" }]"; // using Jackson to parse ObjectMapper objectMapper = new ObjectMapper(); objectMapper.getTypeFactory(); List<MyInfo> myObjectList = objectMapper.readValue(jsonString, typeFactory.constructCollectionType(List.class, MyInfo.class)); // Java 8 Collections List<String> nameList = myObjectList.stream().map(MyInfo::getName).collect(Collectors.toList()); Beware, it implies the usage of a MyInfo class representing your a Java class in which Json objects of yours would fit in.
You can use JSONArray to get value from key 'name'. Like this: JSONArray jSONArray = new JSONArray(yourJson); List<String> list = new ArrayList<>(); for (int i = 0; i < jSONArray.length(); i++) { JSONObject object = (JSONObject) jSONArray.get(i); String value = object.getString("name"); System.out.println(value); list.add(value); }
You may try the following code snippet, import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; List<String> ls = new ArrayList<String>(); JSONObject jsonObj = new JSONObject(); JSONArray jsonArr = new JSONArray(); JSONParser jsonParse = new JSONParser(); String str = "[{\"id\": \"0\",\"name\": \"Bob\",\"place\": \"Colorado\"}," + "{\"id\": \"1\",\"name\": \"John\",\"place\": \"Chicago\"}," + "{\"id\": \"2\",\"name\": \"Marry\",\"place\": \"Miami\"}]"; try { jsonArr= (JSONArray) jsonParse.parse(str); //parsing the JSONArray if(jsonArr!=null){ int arrayLength =jsonArr.size(); //size is 3 here for(int i=0;i<arrayLength;i++){ jsonObj = (JSONObject) jsonParse.parse(jsonArr.get(i).toString()); ls.add(jsonObj.get("name").toString()); //as we need only value of name into the list } System.out.println(ls); } } catch (ParseException e) { e.printStackTrace(); }catch(Exception e1){ e1.printStackTrace(); } As you have array, use JSONArray and used jsonParse to avoid any parsing error. I have used json-simple API to acheive the above.
Parsing JSON of a particular case
I am trying retrieving the following JSON data from imagga's image recognition API. {"results":[{"image":"http://docs.imagga.com/static/images/docs/sample/japan-605234_1280.jpg","tagging_id":null,"tags":[{"confidence":63.346307851163395,"tag":"valley"},{"confidence":60.66263009377379,"tag":"mountain"},{"confidence":44.39096006516168,"tag":"canyon"},{"confidence":42.08210930346856,"tag":"landscape"},{"confidence":33.52198895357515,"tag":"geological formation"},{"confidence":32.702112467737216,"tag":"mountains"},{"confidence":28.626223994488203,"tag":"glacier"},{"confidence":28.36,"tag":"natural depression"},{"confidence":28.03481906795487,"tag":"ravine"},{"confidence":27.269738461024804,"tag":"sky"},{"confidence":26.130797131953397,"tag":"rock"},{"confidence":23.11898739400327,"tag":"travel"},{"confidence":21.75182989551758,"tag":"alp"},{"confidence":20.956625061326214,"tag":"national"},{"confidence":20.15360199670358,"tag":"park"},{"confidence":19.826365024393702,"tag":"stone"},{"confidence":19.717420656127437,"tag":"water"},{"confidence":18.049071926896588,"tag":"river"},{"confidence":17.81629840041474,"tag":"hill"},{"confidence":17.30594970410163,"tag":"tourism"},{"confidence":17.192663177192692,"tag":"clouds"},{"confidence":16.53588724897844,"tag":"scenic"},{"confidence":15.98967256769248,"tag":"peak"},{"confidence":15.792599629554461,"tag":"lake"},{"confidence":15.532788988165363,"tag":"scenery"},{"confidence":15.453814687301834,"tag":"snow"},{"confidence":15.232632664896412,"tag":"outdoors"},{"confidence":15.212304004139495,"tag":"range"},{"confidence":15.042325772263556,"tag":"hiking"},{"confidence":14.958759294889424,"tag":"tree"},{"confidence":14.78842712696222,"tag":"forest"},{"confidence":12.853490785491731,"tag":"grass"},{"confidence":12.242518977753525,"tag":"desert"},{"confidence":12.095999999999998,"tag":"natural elevation"},{"confidence":12.03899501602295,"tag":"america"},{"confidence":11.49381779097963,"tag":"environment"},{"confidence":11.250534926394025,"tag":"usa"},{"confidence":10.935999552280517,"tag":"panorama"},{"confidence":10.838870815021957,"tag":"trees"},{"confidence":10.77081532273937,"tag":"south"},{"confidence":10.385222667460749,"tag":"summer"},{"confidence":9.967993711501377,"tag":"cloud"},{"confidence":9.960797892906747,"tag":"wild"},{"confidence":9.840206836878211,"tag":"natural"},{"confidence":9.64736797817423,"tag":"geology"},{"confidence":9.622992778171428,"tag":"rocky"},{"confidence":9.5011692563878,"tag":"outdoor"},{"confidence":9.36921935993258,"tag":"wilderness"},{"confidence":9.360136841263397,"tag":"vacation"},{"confidence":9.295849004816608,"tag":"rocks"},{"confidence":9.200756690906687,"tag":"high"},{"confidence":9.098263071652019,"tag":"highland"},{"confidence":8.912795414022,"tag":"tourist"},{"confidence":8.871604649828521,"tag":"hike"},{"confidence":8.849249986309006,"tag":"landmark"},{"confidence":8.696713373486205,"tag":"cliff"},{"confidence":8.600291951670297,"tag":"scene"},{"confidence":8.535889495009538,"tag":"stream"},{"confidence":8.530021520404471,"tag":"sunny"},{"confidence":8.255077489679804,"tag":"altitude"},{"confidence":8.016191292928964,"tag":"trail"},{"confidence":7.9938748285500605,"tag":"autumn"},{"confidence":7.985278417869093,"tag":"california"},{"confidence":7.927492176055299,"tag":"spain"},{"confidence":7.774043777890904,"tag":"adventure"},{"confidence":7.560207874392119,"tag":"peaceful"},{"confidence":7.485827508554503,"tag":"fall"},{"confidence":7.283862421876644,"tag":"erosion"},{"confidence":7.272123549182718,"tag":"terrain"},{"confidence":7.24510515635207,"tag":"rural"},{"confidence":7.234934522337296,"tag":"vista"},{"confidence":7.092282542389207,"tag":"holiday"}]}]} I am using http://www.java2s.com/Code/Jar/o/Downloadorgjson20130603jar.htm library. My Java code is as follows: String imageUrl = "http://docs.imagga.com/static/images/docs/sample/japan-605234_1280.jpg", apiKey = "", apiSecret = ""; // These code snippets use an open-source library. http://unirest.io/java HttpResponse response = Unirest.get("https://api.imagga.com/v1/tagging") .queryString("url", imageUrl) .basicAuth(apiKey, apiSecret) .header("Accept", "application/json") .asJson(); String js = response.getBody().toString(); System.out.println(js.toString()); JSONObject jObject = new JSONObject(response.getBody()); // json System.out.print("hello"); JSONObject data1 = jObject.getJSONObject("results"); // get data System.out.print(data1); // object String projectname = data1.getString("tags"); // get the name // from data. System.out.print(projectname); I am getting the error that Exception in thread "main" org.json.JSONException: JSONObject["results"] not found. What I want to get is the list of "tag" and "confidence".
try this JSONArray data1 = jObject.getJSONArray("results"); Edited Answer String js = response.getBody().toString(); System.out.println(js.toString()); JSONObject jObject = new JSONObject(js); // json System.out.print("hello"); JSONArray data1 = jObject.getJSONArray("results"); for(int i = 0; i < data1.length; i++) { JSONObject jsonObject = data1.getJSONObject(i); String projectn ame = jsonObject.getString("tagging_id"); System.out.print(projectname); JSONArray tagArray = jsonObject.getJsonArray("tags"); for(int j = 0; j < tagArray.length; j++) { JSONObject tagObject = tagArray.getJSON(j); System.out.println("Tag == " + tagObject.getString("tag")); } }
To make your life more easy I'd go to model the Objects as POJO's and let Jackson's Objectmapper do the magic. See http://wiki.fasterxml.com/JacksonDataBinding
I suppose you should try jObject.getJSONArray("results") instead of jObject.getJSONObject("results"). There is also a good tool to convert json to java: http://json2java.azurewebsites.net/ If you use it, you will see: ...other code... public class RootObject { private ArrayList<Result> results; public ArrayList<Result> getResults() { return this.results; } public void setResults(ArrayList<Result> results) { this.results = results; } } ...other code... And results is list here, so use getJSONArray instead of getJSONObject
If you look into your json results has an array in it and therefore you should use getJSONArray and not getJSONObject. JSONArray data1 = jObject.getJSONArray("results");
How to parse JSON structured-JSON Array Object in Java
i'm trying to parse this JSON code { "resultCode":"350", "message":"OK", "result":1, "data": { "totalCount":"2", "videos":[ { "videoId":"73bfedf534", "VideoUrl":"www.videourlexample.com", "title":"vbsample1", "description":"" }, { "videoId":"73bfedf534", "VideoUrl":"www.videourlexample.com", "title":"vbsample2", "description":"" } ] } } I was able to parse only this. "resultCode":"350", "message":"OK", "result":1, this is the java code JSONObject jsonObject = (JSONObject) //return the JSON code above. jsonParser.parse(getHTML("...httpRequest...")); // get a String from the JSON object String resultCode = (String) jsonObject.get("resultCode"); System.out.println("[RESULTCODE] The message is: " + resultCode); // get a String from the JSON object String message = (String) jsonObject.get("message"); System.out.println("[MESSAGE] The message is: " + message); // get a number from the JSON object long result = (long) jsonObject.get("result"); System.out.println("[RESULT] The resultCode is: " + result); I can't parse the "data". Someone can help me? I would like to take each value from the json array separately... like resultCode, message and result. Thank you.
JSONObject mainObj= new JSONObject(yourJSON); String resultCode= mainObj.get("resultCode"); String message= mainObj.get("message"); String result= mainObj.get("result"); JSONObject dataObj = mainObj.get("data"); JSONArray jsonArray = (JSONArray) dataObj.get("videos"); for (int i = 0; i <jsonArray.length(); i++) { JSONObject obj= jsonArray.get(i); String videoId=obj.get("videoId"); String videoUrl=obj.get("VideoUrl"); String title=obj.get("title"); String description=obj.get("description"); System.out.println("videoId="+videoId +"videoUrl="+videoUrl+"title=title"+"description="+description); } System.out.println("resultCode"+resultCode+"message"+message+"result"+result);
You can try using this:- JSONObject dataObj = (JSONObject)dataObj .get("data"); JSONArray jsonArray = (JSONArray) dataObj.get("videos"); for (int i = 0; i <jsonArray.length(); i++) { System.out.println(((JSONObject)jsonArray.get(i)).get("videoUrl")); } Currently I have just printes videoUrl, you can similarly get other attributes for videos.
for data use: int totalCount = (int) ((Map) jsonObject.get("data")).get("totalCount"); JSONArray videos = (JSONArray) jsonObject.get("data")).get("videos"); and then parse videos JSONArray.
Android json parsing without array name
I have a Json Array as string without name and I want to parse it how can i do it in android ? My array : {"emp_info":[ {"id":"1","groupe":"1","professeur":"1"}, {"id":"2","groupe":"2","professeur":"1"} ]}
This is how you can parse it Assuming your json string is data JSONObject jsonObj = new JSONObject(data); JSONArray empInfo = jsonObj.getJSONArray("emp_info"); for(int i = 0; i < empInfo.length(); i++){ JSONObject obj = empInfo.getJSONObject(i); String id = obj.getString("id"); String groupe = obj.getString("groupe"); String professeur = obj.getString("professeur"); }
The example json you gave has a name, but if it doesn't this is how I do it. Using Gson to parse JSON, I use TypeToken to tell the gson builder it's an array. List<MyObject> jsonObject = new Gson().fromJson(json, new TypeToken<List<MyObject>>().getType()); With the following code you'll have an object representation of your json array.