How to get String Array from JSON Object - android
I am trying to get a list of available numbers from the following json object
void doJSONParser(String str)
{
try {
JSONObject order = new JSONObject(str);
JSONArray index = order.getJSONArray("webnautes");
for(int i = 0; i < index.length(); i++)
{
JSONObject tt = index.getJSONObject(i);
name += "name: " + tt.getString("name")+"\n";
phone = new String[tt.getString("phone").length()];
//phone += "phone: " + tt.getString("phone")+"\n";
}
Log.d("JSON DATA:", name);
Log.d("JSON DATA:", phone.toString());
}
catch (JSONException e)
{
e.printStackTrace();
Log.d("JSON ERROR", e.getLocalizedMessage());
}
}
Try this
JSONArray phoneJson = tt.getJSONArray("phone");
String[] arr = new String[phoneJson.length()];
for(int i = 0; i < phoneJson.length(); i++)
arr[i] = phoneJson.getString(i);
Related
I am getting error as value of java.lang.String cannot be converted to JSONObject [duplicate]
This question already has answers here: How do I parse JSON in Android? [duplicate] (3 answers) Closed 5 years ago. my JSONResponse looks as follows [["1","somevalue","1234567890","1239876093","some"], ["2","somevalue","1234567890","1234567890","some"]] and code accessing it is as follows try{ String[] details = {""}; JSONArray array1 = new JSONArray(response); JSONArray array = array1.getJSONArray(0); JSONObject object; //print(array.length() + ""); for(int i = 0; i < array.length(); i++){ object = array.getJSONObject(i); details[i] = object.getString("roll") + " " + object.getString("name"); ArrayList<String> list = new ArrayList<>(); list.addAll(Arrays.asList(details)); ArrayAdapter<String> defaulters = new ArrayAdapter<String>(ShowDefaulters.this, android.R.layout.simple_list_item_1, list); listView.setAdapter(defaulters); } } catch (Exception e){ print(e.getMessage()); }
You must get jsonarray on object array. example: private void test(){ String json_text ="[[\"1\",\"somevalue\",\"1234567890\",\"1239876093\",\"some\"], [\"2\",\"somevalue\",\"1234567890\",\"1234567890\",\"some\"]]"; try{ JSONArray array1 = new JSONArray(json_text); JSONArray array = array1.getJSONArray(0); Log.e("ARR", array.toString()); JSONArray user_arr = new JSONArray(array.toString()); for(int i=0; i< user_arr.length(); i++){ Log.e("USER", user_arr.get(i).toString()); } } catch (Exception e){ Log.e("ERROR", e.getMessage()); } } or: private void test(){ String json_text ="[[\"1\",\"somevalue\",\"1234567890\",\"1239876093\",\"some\"], [\"2\",\"somevalue\",\"1234567890\",\"1234567890\",\"some\"]]"; try{ JSONArray array1 = new JSONArray(json_text); JSONArray array = array1.getJSONArray(0); Log.e("ARR", array.toString()); JSONArray user_arr = new JSONArray(array.toString()); Log.e("USER", "roll: " + user_arr.get(0).toString()); Log.e("USER", "name: " + user_arr.get(1).toString()); } catch (Exception e){ Log.e("ERROR", e.getMessage()); } }
Decode JSON String in Android
I have a contacts which is in the form of JSON. Now I want to decode them into String array. There are two arrays; names and phones. I'm using this code: String[] names; String[] phones; String test = "[{\"name\":\"A\",\"phone\":\"911\"},{\"name\":\"A1\",\"phone\":\"911\"},{\"name\":\"Abid\",\"phone\":\"371812\"}]"; try { JSONArray jsonArray = new JSONArray(test); JSONObject jsonObject = new JSONObject(jsonArray.toString()); Log.i("INFO", String.valueOf(jsonObject.length())); } catch (JSONException e) { e.printStackTrace(); } This generates an error. How can I add all names in names array and all phones in phones array. Like names[0] is assigned A which is a first name and phones[0] assigned 911 which is first phone number corresponding to first name. How can I do that, I'm new in android?
The problem is in this line JSONObject jsonObject = new JSONObject(jsonArray.toString()); you are trying to convert JSONArray in JSONObject , which you can't. if you want to access JSONObject inside of JSONArray you have to loop through each, or you can get specific object by it's index. String[] names; String[] phones; String test = "[{\"name\":\"A\",\"phone\":\"911\"},{\"name\":\"A1\",\"phone\":\"911\"},{\"name\":\"Abid\",\"phone\":\"371812\"}]"; try { JSONArray jsonArray = new JSONArray(test); phones = names = new String[jsonArray.length()]; for(int i = 0 ; i < jsonArray.length(); i ++){ JSONObject jsonObject = jsonArray.getJSONObject(i); names[i] = jsonObject.getString("name"); phones[i] = jsonObject.getString("phone"); Log.i("INFO", "name : " + jsonObject.getString("name") + " , phone : " + jsonObject.getString("phone")); } } catch (JSONException e) { e.printStackTrace(); }
You can't convert json arrya to json object like that: try { JSONArray jsonArray = new JSONArray(test); for(int i=0; i<jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); // Your code goes here.. } } catch (JSONException e) { e.printStackTrace(); }
here is the required code. ArrayList<String> names = new ArrayList<>(); ArrayList<String> phones = new ArrayList<>(); String test = "[{\"name\":\"A\",\"phone\":\"911\"},{\"name\":\"A1\",\"phone\":\"911\"},{\"name\":\"Abid\",\"phone\":\"371812\"}]"; try { JSONArray jsonArray = new JSONArray(test); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); String name = jsonObject.getString("name"); String phone = jsonObject.getString("phone"); names.add(name); phones.add(phone); Log.i("INFO", name + ", " + phone); } } catch (JSONException e) { e.printStackTrace(); } above you have used simple arrays they can store data only of fixed size. But arrayList and List can store data of variable size or you do not need to fix their size.
how I can access data from a JSONArray in Android?
how I can access data from a JSONArray? It is this and contains this information: "deadlines": [ { "start": 1439539200, "end": 1439542800 }, { "start": 1440144000, "end": 1440147600 }, { "start": 0, "end": 0 } ] I need to have in a String tag each item "start". Thanks EDIT My code is this: JSONArray array = moduleObject.specialForcedConf; // array = [{"deadlines":[{"start":1439539200,"end":1439542800},{"start":1440144000,"end":1440147600},{"start":0,"end":0}]}] for (int j=0; j < array.length(); j++) { try { JSONObject obj = array.getJSONObject(j); String start = obj.getString("start"); String end = obj.getString("end"); Log.e("", "start = " + start); } catch (JSONException e) { Log.e("", "error = " + e.getMessage()); } } I get this error: "error = No value for start"
Do this JSONArray array = moduleObject.specialForcedConf; // array = [{"deadlines":[{"start":1439539200,"end":1439542800},{"start":1440144000,"end":1440147600},{"start":0,"end":0}]}] JSONArray jArray = array.getJSONObject(0).getJSONArray("deadlines"); for (int i = 0; i < jArray.length(); i++) // assuming your array is jArray { try { JSONObject obj = jArray.getJSONObject(i); String start= obj.getString("start"); // store in an ArrayList String end = obj.getString("end"); //// store in an ArrayList } catch (JSONException e) { // Error } }
JSONArray mJsonArray=new JSONArray("Deadlines"); for(int i=0;i<mJsonArray.length();i++){ JSONObject mJsonObject=new JSONObject(mJsonArray.get(i).toString)); String start = mJsonObject.optString("start",""); String end = mJsonObject.optString("end",""); }
Try this: StringBuilder sb = new StringBuilder(); JSONArray arr = new JSONArray("deadlines"); for(int i=0;i<arr.length;i++){ JSONObject obj = arr.getJSONObject(i); sb.append(obj.get("start").toString()); sb.append(","); } String strStartTag = sb;
JSONArray ja = new JSONArray(yourjsondata)); for (int i = 0; i < ja.length(); i++) { JSONObject jo_feed = new JSONObject(ja.get(i).toString()); String start = jo_feed.getString("start"); }
Nested JSONArray Record Parsing Issue Android
I am making a App which receives a JSON Data From Server, User will select a value from Spinner like 0,1,2... and so on, On the Basis of number selection JSON will return a data from user defined index like 0,1.. i don't know how to parse a inner JSON Data in Android json = new JSONObject(JSONParser.Result); JSONArray jArray = json.getJSONArray("data"); StopElement _stop = new StopElement(); Log.d("JSON Algo Result", json.toString()); if (jArray!=null) { for (int i = 0; i < jArray.length(); i++) { jsonarray = jArray.getJSONArray(i); if (jsonarray != null) { for (int j = 0; j < jsonarray.length(); i++){ if(i==0) { jsonarray = jArray.getJSONArray(j); _stop.setName(jsonarray.getString(0)); StopElement.Stop_name_list.add(_stop.getName()); } } } else { break; } } } else { return null; } **JSON Data** [[["Sadiqabad Chowk","33.634525","73.074326","suzk1"],["Chungi No 8","33.627262","73.093567","suzk1"],["Jahaz Ground","33.628395","73.101936","suzk1"],["Khana Bridge","33.629967","73.112823","suzk1"],["Kuri Road","33.643162","73.102928","21"],["Faizabad","33.663212","73.084801","21"]],[["Sadiqabad Chowk","33.634525","73.074326","suzk1"],["Petrol Pump","33.634109","73.076363","suzk1"],["Chandni Chowk","33.631584","73.072563","suzk1"],["Rahmanabad","33.639065","73.075714","3"],["Passport Office","33.642410","73.076981","3"],["Shamsabad","33.650101","73.079994","3"],["Faizabad","33.663212","73.084801","3"]],[["Sadiqabad Chowk","33.634525","73.074326","suzk1"],["Chungi No 8","33.627262","73.093567","suzk1"],["Jahaz Ground","33.628395","73.101936","suzk1"],["Khana Bridge","33.629967","73.112823","suzk1"],["Zia Masjid","33.637196","73.107407","124-A"],["Kuri Road","33.643162","73.102928","124-A"],["Dhok Kala Khan","33.653118","73.095444","124-A"],["Faizabad","33.663212","73.084801","124-A"]],[["Sadiqabad Chowk","33.634525","73.074326","suzk1"],["Chungi No 8","33.627262","73.093567","suzk1"],["Jahaz Ground","33.628395","73.101936","suzk1"],["Khana Bridge","33.629967","73.112823","suzk1"],["Zia Masjid","33.637196","73.107407","136"],["Kuri Road","33.643162","73.102928","136"],["Iqbal Town","33.644279","73.100113","136"],["Dhok Kala Khan","33.653118","73.095444","136"],["Faizabad","33.663212","73.084801","136"]],[["Sadiqabad Chowk","33.634525","73.074326","suzk13"],["Dhok Ali Akbar","33.636997","73.092117","suzk13"],["Highway Stop","33.679722","73.075584","suzk13"],["Bhinder","33.556244","73.167946","suzk3"],["Lohi Bher","33.586273","73.145493","124"],["Wild Life Park","33.578770","73.132309","124"],["Airport Chowk","33.593803","73.139938","124"],["Gangal","33.612591","73.125801","124"],["Khana Bridge","33.629967","73.112823","124"],["Zia Masjid","33.637196","73.107407","124"],["Kuri Road","33.643162","73.102928","124"],["Dhok Kala Khan","33.653118","73.095444","124"],["Faizabad","33.663212","73.084801","124"]],[["Sadiqabad Chowk","33.634525","73.074326","suzk13"],["Petrol Pump","33.634109","73.076363","suzk13"],["Chandni Chowk","33.631584","73.072563","suzk13"],["Central Hospital","33.628784","73.070641","suzk13"],["Naz Cinema","33.623936","73.067802","suzk13"],["Waris Khan","33.620728","73.066078","suzk13"],["Comittee Chowk","33.612946","73.065193","suzk13"],["Liaquat Bagh","33.606808","73.064835","suzk13"],["Marir Hassan","33.596905","73.064445","suzk7"],["Punjab House","33.592701","73.065453","suzk7"],["Jhanda","33.588970","73.076195","suzk7"],["Raheemabad","33.599159","73.080048","21"],["Airport","33.603565","73.097137","21"],["Tajabad","33.601593","73.126213","21"],["Koral Chowk","33.605282","73.131279","21"],["Khana Bridge","33.629967","73.112823","21"],["Kuri Road","33.643162","73.102928","21"],["Faizabad","33.663212","73.084801","21"]],[["Sadiqabad Chowk","33.634525","73.074326","suzk13"],["Petrol Pump","33.634109","73.076363","suzk13"],["Chandni Chowk","33.631584","73.072563","suzk13"],["Central Hospital","33.628784","73.070641","suzk13"],["Naz Cinema","33.623936","73.067802","suzk13"],["Waris Khan","33.620728","73.066078","suzk13"],["Comittee Chowk","33.612946","73.065193","suzk13"],["Liaquat Bagh","33.606808","73.064835","suzk13"],["Medical College","33.602753","73.067200","29"],["Sir Syed Boys School","33.609600","73.078766","29"],["Fauji Tower","33.606770","73.084106","29"],["Chaklala Station","33.601013","73.095924","29"],["Raheemabad","33.599159","73.080048","29"],["Airport","33.603565","73.097137","21"],["Tajabad","33.601593","73.126213","21"],["Koral Chowk","33.605282","73.131279","21"],["Khana Bridge","33.629967","73.112823","21"],["Kuri Road","33.643162","73.102928","21"],["Faizabad","33.663212","73.084801","21"]],[["Sadiqabad Chowk","33.634525","73.074326","suzk13"],["Petrol Pump","33.634109","73.076363","suzk13"],["Chandni Chowk","33.631584","73.072563","suzk13"],["Central Hospital","33.628784","73.070641","suzk13"],["Naz Cinema","33.623936","73.067802","suzk13"]]]
this will work definitely for you. String data= "[[['Jahaz Ground','33.628395','73.101936','suzk1'],['Khana Bridge','33.628395','73.101936','suzk1']]]"; try { JSONArray json = new JSONArray(data); JSONArray aray = (JSONArray)json.get(0); for(int i =0; i< aray.length();i++){ JSONArray object = (JSONArray)aray.get(i); String title = (String)object .get(0); String lat = (String)object.get(1); String lng = (String)object.get(2); String com = (String)object.get(3); } } catch (JSONException e) { e.printStackTrace(); }
try this way, U can get result as per your request private String jsonString="[[['Sadiqabad Chowk','33.634525','73.074326','suzk1'],['Chungi No 8','33.627262','73.093567','suzk1'],['Jahaz Ground','33.628395','73.101936','suzk1'],['Khana Bridge','33.629967','73.112823','suzk1'],['Kuri Road','33.643162','73.102928','21'],['Faizabad','33.663212','73.084801','21']],[['Sadiqabad Chowk','33.634525','73.074326','suzk1'],['Petrol Pump','33.634109','73.076363','suzk1'],['Chandni Chowk','33.631584','73.072563','suzk1'],['Rahmanabad','33.639065','73.075714','3'],['Passport Office','33.642410','73.076981','3'],['Shamsabad','33.650101','73.079994','3'],['Faizabad','33.663212','73.084801','3']],[['Sadiqabad Chowk','33.634525','73.074326','suzk1'],['Chungi No 8','33.627262','73.093567','suzk1'],['Jahaz Ground','33.628395','73.101936','suzk1'],['Khana Bridge','33.629967','73.112823','suzk1'],['Zia Masjid','33.637196','73.107407','124-A'],['Kuri Road','33.643162','73.102928','124-A'],['Dhok Kala Khan','33.653118','73.095444','124-A'],['Faizabad','33.663212','73.084801','124-A']],[['Sadiqabad Chowk','33.634525','73.074326','suzk1'],['Chungi No 8','33.627262','73.093567','suzk1'],['Jahaz Ground','33.628395','73.101936','suzk1'],['Khana Bridge','33.629967','73.112823','suzk1'],['Zia Masjid','33.637196','73.107407','136'],['Kuri Road','33.643162','73.102928','136'],['Iqbal Town','33.644279','73.100113','136'],['Dhok Kala Khan','33.653118','73.095444','136'],['Faizabad','33.663212','73.084801','136']],[['Sadiqabad Chowk','33.634525','73.074326','suzk13'],['Dhok Ali Akbar','33.636997','73.092117','suzk13'],['Highway Stop','33.679722','73.075584','suzk13'],['Bhinder','33.556244','73.167946','suzk3'],['Lohi Bher','33.586273','73.145493','124'],['Wild Life Park','33.578770','73.132309','124'],['Airport Chowk','33.593803','73.139938','124'],['Gangal','33.612591','73.125801','124'],['Khana Bridge','33.629967','73.112823','124'],['Zia Masjid','33.637196','73.107407','124'],['Kuri Road','33.643162','73.102928','124'],['Dhok Kala Khan','33.653118','73.095444','124'],['Faizabad','33.663212','73.084801','124']],[['Sadiqabad Chowk','33.634525','73.074326','suzk13'],['Petrol Pump','33.634109','73.076363','suzk13'],['Chandni Chowk','33.631584','73.072563','suzk13'],['Central Hospital','33.628784','73.070641','suzk13'],['Naz Cinema','33.623936','73.067802','suzk13'],['Waris Khan','33.620728','73.066078','suzk13'],['Comittee Chowk','33.612946','73.065193','suzk13'],['Liaquat Bagh','33.606808','73.064835','suzk13'],['Marir Hassan','33.596905','73.064445','suzk7'],['Punjab House','33.592701','73.065453','suzk7'],['Jhanda','33.588970','73.076195','suzk7'],['Raheemabad','33.599159','73.080048','21'],['Airport','33.603565','73.097137','21'],['Tajabad','33.601593','73.126213','21'],['Koral Chowk','33.605282','73.131279','21'],['Khana Bridge','33.629967','73.112823','21'],['Kuri Road','33.643162','73.102928','21'],['Faizabad','33.663212','73.084801','21']],[['Sadiqabad Chowk','33.634525','73.074326','suzk13'],['Petrol Pump','33.634109','73.076363','suzk13'],['Chandni Chowk','33.631584','73.072563','suzk13'],['Central Hospital','33.628784','73.070641','suzk13'],['Naz Cinema','33.623936','73.067802','suzk13'],['Waris Khan','33.620728','73.066078','suzk13'],['Comittee Chowk','33.612946','73.065193','suzk13'],['Liaquat Bagh','33.606808','73.064835','suzk13'],['Medical College','33.602753','73.067200','29'],['Sir Syed Boys School','33.609600','73.078766','29'],['Fauji Tower','33.606770','73.084106','29'],['Chaklala Station','33.601013','73.095924','29'],['Raheemabad','33.599159','73.080048','29'],['Airport','33.603565','73.097137','21'],['Tajabad','33.601593','73.126213','21'],['Koral Chowk','33.605282','73.131279','21'],['Khana Bridge','33.629967','73.112823','21'],['Kuri Road','33.643162','73.102928','21'],['Faizabad','33.663212','73.084801','21']],[['Sadiqabad Chowk','33.634525','73.074326','suzk13'],['Petrol Pump','33.634109','73.076363','suzk13'],['Chandni Chowk','33.631584','73.072563','suzk13'],['Central Hospital','33.628784','73.070641','suzk13'],['Naz Cinema','33.623936','73.067802','suzk13']]]"; private void getJSONResult(String jsonString) { // TODO Auto-generated method stub try { JSONArray jsonArray = new JSONArray(jsonString); // i.e. the JsonMainArray JSONArray JsonInnerArray= (JSONArray)jsonArray.get(0); // i.e. the JsonInnerArray for(int i =0; i< JsonInnerArray.length();i++){ JSONArray jsonObject = (JSONArray)JsonInnerArray.get(i); //i.e. the jsonObject of JsonInnerArray String title = (String)jsonObject .get(0); String latitude = (String)jsonObject.get(1); String longitude = (String)jsonObject.get(2); String companyName = (String)jsonObject.get(3); } } catch (JSONException e) { e.printStackTrace(); } }
I Solved My Issue like this try { int count = 0; if (success == 1) { json = new JSONObject(JSONParser.Result); jArray = json.getJSONArray("data"); StopElement _stop = new StopElement(); for (int i = 0; i < jArray.length(); i++) { count = count + 1; // Log.d("Counter", String.valueOf(count)); get_path.add(String.valueOf(count)); jsonarray = jArray.getJSONArray(i); Log.d("path Array", jsonarray.toString()); if (getitemno>0) { getpath = jArray.getJSONArray(getitemno); for (int j = 0; j < getpath.length(); j++) { getresult = getpath.getJSONArray(j); _stop.setName(getresult.getString(0)); _map.setLat(getresult.getDouble(1)); _map.setLng(getresult.getDouble(2)); StopElement.Stop_name_list.add(_stop.getName()); MapCoordinatesElement.Lat.add(_map.getLat()); MapCoordinatesElement.Lng.add(_map.getLng()); //Log.d("Stop names", _stop.getName()); // Log.d("Index Array", getpath.toString()); } } else { getpath = jArray.getJSONArray(0); for (int j = 0; j < getpath.length(); j++) { getresult = getpath.getJSONArray(j); _stop.setName(getresult.getString(0)); _map.setLat(getresult.getDouble(1)); _map.setLng(getresult.getDouble(2)); StopElement.Stop_name_list.add(_stop.getName()); MapCoordinatesElement.Lat.add(_map.getLat()); MapCoordinatesElement.Lng.add(_map.getLng()); //Log.d("Stop names", _stop.getName()); //Log.d("Index Array", getpath.toString()); } } } // Log.d("Stop names of Algo", StopElement.Stop_name_list.toString()); // Log.d("Index Stop names", StopElement.Stop_name_list.toString()); } catch (JSONException e) { e.printStackTrace(); }
How to get the value from JSON in Android?
I am develop in Android and Xively. I get the JSON data like the following. {"id":10541167124,"title":"air-conditioner","private":"false","feed":"https://api.xively.com/v2/feeds/10541167124.json","status":"frozen","updated":"2014-08-15T08:23:01.612382Z","created":"2014-08-15T08:17:17.336198Z","creator":"https://xively.com/users/x22819","version":"1.0.0","datastreams":[{"id":"air-aonditioner1","current_value":"25","at":"2014-08-15T08:23:01.521837Z","max_value":"25.0","min_value":"25.0","tags":["air=off","eco=off","freeze_proofing=off","power=on","stop_time=1","temp_set=24","wind_speed=low"],"unit":{"symbol":"C","label":"Celsius"}}],"product_id":"uq8_wTGk4Kdmfmlaa1B-","device_serial":"7AHJ84EAQDEZ"} I use the following code to get the tags data. String resultJSON = response.getContent(); try { JSONObject jsonRoot = new JSONObject(resultJSON); JSONArray jsonData = jsonRoot.getJSONArray("datastreams"); for(int i=0 ; i < jsonData.length() ; i++) { JSONObject jsonOBject = jsonData.getJSONObject(i); String tags = jsonOBject.getString("tags"); Log.(TAG, "tags = "+ tags + "\n"); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } The data of tags is like the following D/DemoActivity(25686): tags = ["air=off","eco=off","freeze_proofing=off","power=on","stop_time=1","temp_set=24","wind_speed=low"] But I want to get the more detail value in tags , for example : air is ? power is ? temp_set is ? How do I get the more detail data in tags ?
You can pass the text result of tags to the constructor of JSON class and iterate on it while replacing all the "=" of each string to " is " sample: for(int i=0 ; i < jsonData.length() ; i++) { JSONObject jsonOBject = jsonData.getJSONObject(i); String tags = jsonOBject.getString("tags"); JSONArray arr = new JSONArray(tags); for(int i = 0 ; i < arr.length(); i++) Log.d("VALUE", arr.getString(i).replaceAll("=", " is ")); } result: 08-19 01:43:32.169: D/VALUE(1444): air is off 08-19 01:43:32.169: D/VALUE(1444): eco is off 08-19 01:43:32.175: D/VALUE(1444): freeze_proofing is off 08-19 01:43:32.175: D/VALUE(1444): power is on 08-19 01:43:32.175: D/VALUE(1444): stop_time is 1 08-19 01:43:32.189: D/VALUE(1444): temp_set is 24 08-19 01:43:32.195: D/VALUE(1444): wind_speed is low
Try this way,hope this will help you to solve your problem. try{ String jsonResponse = "{\"id\":10541167124,\"title\":\"air-conditioner\",\"private\":\"false\",\"feed\":\"https://api.xively.com/v2/feeds/10541167124.json\",\"status\":\"frozen\",\"updated\":\"2014-08-15T08:23:01.612382Z\",\"created\":\"2014-08-15T08:17:17.336198Z\",\"creator\":\"https://xively.com/users/x22819\",\"version\":\"1.0.0\",\"datastreams\":[{\"id\":\"air-aonditioner1\",\"current_value\":\"25\",\"at\":\"2014-08-15T08:23:01.521837Z\",\"max_value\":\"25.0\",\"min_value\":\"25.0\",\"tags\":[\"air=off\",\"eco=off\",\"freeze_proofing=off\",\"power=on\",\"stop_time=1\",\"temp_set=24\",\"wind_speed=low\"],\"unit\":{\"symbol\":\"C\",\"label\":\"Celsius\"}}],\"product_id\":\"uq8_wTGk4Kdmfmlaa1B-\",\"device_serial\":\"7AHJ84EAQDEZ\"}"; JSONObject dataJson = new JSONObject(jsonResponse); HashMap<String,Object> row = new HashMap<String, Object>(); row.put("id", dataJson.getString("id")); row.put("title",dataJson.getString("title")); row.put("private",dataJson.getString("private")); row.put("feed",dataJson.getString("feed")); row.put("status",dataJson.getString("status")); row.put("updated",dataJson.getString("updated")); row.put("created",dataJson.getString("created")); row.put("creator",dataJson.getString("creator")); row.put("version",dataJson.getString("version")); ArrayList<HashMap<String, Object>> datastreamsList = new ArrayList<HashMap<String, Object>>(); JSONArray datastreamsjsonArray = dataJson.getJSONArray("datastreams"); for (int i=0;i<datastreamsjsonArray.length();i++){ HashMap<String,Object> datastreamsMap = new HashMap<String, Object>(); datastreamsMap.put("id",datastreamsjsonArray.getJSONObject(i).getString("id")); datastreamsMap.put("current_value",datastreamsjsonArray.getJSONObject(i).getString("current_value")); datastreamsMap.put("at",datastreamsjsonArray.getJSONObject(i).getString("at")); datastreamsMap.put("max_value",datastreamsjsonArray.getJSONObject(i).getString("max_value")); datastreamsMap.put("min_value",datastreamsjsonArray.getJSONObject(i).getString("min_value")); JSONArray tagsJsonArray = datastreamsjsonArray.getJSONObject(i).getJSONArray("tags"); ArrayList<String> tagsList = new ArrayList<String>(); for (int j=0;j<tagsJsonArray.length();j++){ tagsList.add(tagsJsonArray.getString(j)); } datastreamsMap.put("tags",tagsList); HashMap<String,String> unitMap = new HashMap<String, String>(); unitMap.put("symbol",datastreamsjsonArray.getJSONObject(i).getJSONObject("unit").getString("symbol")); unitMap.put("label",datastreamsjsonArray.getJSONObject(i).getJSONObject("unit").getString("label")); datastreamsMap.put("unit",unitMap); datastreamsList.add(datastreamsMap); } row.put("datastreams",datastreamsList); row.put("product_id",dataJson.getString("product_id")); row.put("device_serial",dataJson.getString("device_serial")); System.out.print("id :"+row.get("id")); System.out.print("title :"+row.get("title")); System.out.print("private :"+row.get("private")); System.out.print("feed :"+row.get("feed")); System.out.print("status :"+row.get("status")); System.out.print("updated :"+row.get("updated")); System.out.print("created :"+row.get("created")); System.out.print("creator :"+row.get("creator")); System.out.print("version :"+row.get("version")); ArrayList<HashMap<String,Object>> datastreamsListData = (ArrayList<HashMap<String,Object>>)row.get("datastreams"); for (int i=0;i<datastreamsListData.size();i++){ HashMap<String,Object> datastreamsMapData = datastreamsListData.get(i); System.out.print("id :" + datastreamsMapData.get("id")); System.out.print("current_value :" + datastreamsMapData.get("current_value")); System.out.print("at :"+datastreamsMapData.get("at")); System.out.print("max_value :" + datastreamsMapData.get("max_value")); System.out.print("min_value :"+datastreamsMapData.get("min_value")); ArrayList<String> tagsListData = (ArrayList<String>) datastreamsMapData.get("tags"); for (int j=0;j<tagsListData.size();j++){ System.out.print(tagsListData.get(j)); } HashMap<String,Object> unitMapData = (HashMap<String,Object>) datastreamsMapData.get("unit"); System.out.print("symbol :"+unitMapData.get("symbol")); System.out.print("label :"+unitMapData.get("label")); } System.out.print("product_id :"+row.get("product_id")); System.out.print("device_serial :"+row.get("device_serial")); }catch (Throwable e){ e.printStackTrace(); }
The problem is that tags are inside a JSONArray, so use the following code instead: try { JSONObject jsonRoot = new JSONObject(resultJSON); JSONArray jsonData = jsonRoot.getJSONArray("datastreams"); for (int i = 0 ; i < jsonData.length() ; i++) { JSONObject jsonOBject = jsonData.getJSONObject(i); JSONArray arrTags = jsonOBject.getJSONArray("tags"); for (int j = 0; j < arrTags.length(); j++) { Log.i("Array Tag [ "+ i +" ] ", arrTags.opt(j).toString()); } } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); }