Problema with JSONObject and JSONArray - android

JSONObject result = new JSONObject("{\"result\":{\"players\":[{\"account_id\":4294967295,\"player_slot\":0,\"hero_id\":66,\"item_0\":180,\"item_1\":92,\"item_2\":42,\"item_3\":0,\"item_4\":0,\"item_5\":46,\"kills\":0,\"deaths\":10,\"assists\":8,\"leaver_status\":0,\"gold\":311,\"last_hits\":28,\"denies\":0,\"gold_per_min\":200,\"xp_per_min\":316,\"gold_spent\":5743,\"hero_damage\":3441,\"tower_damage\":188,\"hero_healing\":3515,\"level\":15,\"ability_upgrades\":[{\"ability\":5330,\"time\":189,\"level\":1},{\"ability\":5328,\"time\":385,\"level\":2},{\"ability\":5330,\"time\":479,\"level\":3},{\"ability\":5328,\"time\":539,\"level\":4},{\"ability\":5330,\"time\":791,\"level\":5},{\"ability\":5331,\"time\":857,\"level\":6},{\"ability\":5330,\"time\":985,\"level\":7},{\"ability\":5329,\"time\":1068,\"level\":8},{\"ability\":5328,\"time\":1194,\"level\":9},{\"ability\":5328,\"time\":1251,\"level\":10},{\"ability\":5331,\"time\":1415,\"level\":11},{\"ability\":5329,\"time\":1933,\"level\":12},{\"ability\":5329,\"time\":1970,\"level\":13},{\"ability\":5329,\"time\":2109,\"level\":14},{\"ability\":5002,\"time\":2342,\"level\":15}]},{\"account_id\":4294967295,\"player_slot\":1,\"hero_id\":88,\"item_0\":202,\"item_1\":29,\"item_2\":44,\"item_3\":0,\"item_4\":0,\"item_5\":0,\"kills\":7,\"deaths\":8,\"assists\":7,\"leaver_status\":0,\"gold\":138,\"last_hits\":31,\"denies\":2,\"gold_per_min\":234,\"xp_per_min\":344,\"gold_spent\":6435,\"hero_damage\":9492,\"tower_damage\":74,\"hero_healing\":0,\"level\":15,\"ability_upgrades\":[{\"ability\":5462,\"time\":298,\"level\":1},{\"ability\":5464,\"time\":371,\"level\":2},{\"ability\":5462,\"time\":445,\"level\":3},{\"ability\":5463,\"time\":555,\"level\":4},{\"ability\":5462,\"time\":746,\"level\":5},{\"ability\":5465,\"time\":881,\"level\":6},{\"ability\":5462,\"time\":942,\"level\":7},{\"ability\":5463,\"time\":979,\"level\":8},{\"ability\":5464,\"time\":1225,\"level\":9},{\"ability\":5465,\"time\":1365,\"level\":10},{\"ability\":5464,\"time\":1367,\"level\":11},{\"ability\":5463,\"time\":1966,\"level\":12},{\"ability\":5464,\"time\":1967,\"level\":13},{\"ability\":5463,\"time\":2361,\"level\":14},{\"ability\":5002,\"time\":2482,\"level\":15}]},{\"account_id\":4294967295,\"player_slot\":2,\"hero_id\":13,\"item_0\":36,\"item_1\":41,\"item_2\":40,\"item_3\":104,\"item_4\":46,\"item_5\":63,\"kills\":3,\"deaths\":13,\"assists\":5,\"leaver_status\":0,\"gold\":1738,\"last_hits\":97,\"denies\":4,\"gold_per_min\":284,\"xp_per_min\":357,\"gold_spent\":6694,\"hero_damage\":6801,\"tower_damage\":26,\"hero_healing\":0,\"level\":16,\"ability_upgrades\":[{\"ability\":5069,\"time\":130,\"level\":1},{\"ability\":5071,\"time\":280,\"level\":2},{\"ability\":5072,\"time\":349,\"level\":3},{\"ability\":5069,\"time\":380,\"level\":4},{\"ability\":5071,\"time\":531,\"level\":5},{\"ability\":5073,\"time\":639,\"level\":6},{\"ability\":5069,\"time\":759,\"level\":7},{\"ability\":5069,\"time\":824,\"level\":8},{\"ability\":5071,\"time\":1119,\"level\":9},{\"ability\":5071,\"time\":1201,\"level\":10},{\"ability\":5073,\"time\":1319,\"level\":11},{\"ability\":5072,\"time\":1380,\"level\":12},{\"ability\":5072,\"time\":1429,\"level\":13},{\"ability\":5072,\"time\":1691,\"level\":14},{\"ability\":5002,\"time\":2168,\"level\":15},{\"ability\":5073,\"time\":2399,\"level\":16}]},{\"account_id\":4294967295,\"player_slot\":3,\"hero_id\":59,\"item_0\":79,\"item_1\":63,\"item_2\":43,\"item_3\":37,\"item_4\":32,\"item_5\":17,\"kills\":11,\"deaths\":9,\"assists\":4,\"leaver_status\":0,\"gold\":100,\"last_hits\":90,\"denies\":16,\"gold_per_min\":319,\"xp_per_min\":397,\"gold_spent\":11024,\"hero_damage\":18103,\"tower_damage\":913,\"hero_healing\":723,\"level\":17,\"ability_upgrades\":[{\"ability\":5272,\"time\":236,\"level\":1},{\"ability\":5273,\"time\":298,\"level\":2},{\"ability\":5272,\"time\":366,\"level\":3},{\"ability\":5273,\"time\":476,\"level\":4},{\"ability\":5272,\"time\":549,\"level\":5},{\"ability\":5274,\"time\":650,\"level\":6},{\"ability\":5272,\"time\":827,\"level\":7},{\"ability\":5273,\"time\":893,\"level\":8},{\"ability\":5273,\"time\":958,\"level\":9},{\"ability\":5271,\"time\":1058,\"level\":10},{\"ability\":5274,\"time\":1131,\"level\":11},{\"ability\":5271,\"time\":1262,\"level\":12},{\"ability\":5271,\"time\":1356,\"level\":13},{\"ability\":5271,\"time\":1574,\"level\":14},{\"ability\":5002,\"time\":1715,\"level\":15},{\"ability\":5274,\"time\":2099,\"level\":16},{\"ability\":5002,\"time\":2449,\"level\":17}]},{\"account_id\":4294967295,\"player_slot\":4,\"hero_id\":1,\"item_0\":11,\"item_1\":145,\"item_2\":63,\"item_3\":46,\"item_4\":147,\"item_5\":0,\"kills\":2,\"deaths\":6,\"assists\":3,\"leaver_status\":0,\"gold\":2658,\"last_hits\":258,\"denies\":15,\"gold_per_min\":442,\"xp_per_min\":535,\"gold_spent\":12454,\"hero_damage\":2703,\"tower_damage\":689,\"hero_healing\":0,\"level\":19,\"ability_upgrades\":[{\"ability\":5004,\"time\":141,\"level\":1},{\"ability\":5003,\"time\":258,\"level\":2},{\"ability\":5003,\"time\":345,\"level\":3},{\"ability\":5005,\"time\":444,\"level\":4},{\"ability\":5003,\"time\":551,\"level\":5},{\"ability\":5006,\"time\":693,\"level\":6},{\"ability\":5003,\"time\":785,\"level\":7},{\"ability\":5005,\"time\":935,\"level\":8},{\"ability\":5005,\"time\":1063,\"level\":9},{\"ability\":5005,\"time\":1070,\"level\":10},{\"ability\":5006,\"time\":1096,\"level\":11},{\"ability\":5004,\"time\":1436,\"level\":12},{\"ability\":5004,\"time\":1483,\"level\":13},{\"ability\":5004,\"time\":1565,\"level\":14},{\"ability\":5002,\"time\":1647,\"level\":15},{\"ability\":5006,\"time\":1728,\"level\":16},{\"ability\":5002,\"time\":1867,\"level\":17},{\"ability\":5002,\"time\":2023,\"level\":18},{\"ability\":5002,\"time\":2211,\"level\":19}]},{\"account_id\":4294967295,\"player_slot\":128,\"hero_id\":32,\"item_0\":208,\"item_1\":71,\"item_2\":170,\"item_3\":63,\"item_4\":22,\"item_5\":0,\"kills\":9,\"deaths\":4,\"assists\":10,\"leaver_status\":0,\"gold\":2738,\"last_hits\":93,\"denies\":3,\"gold_per_min\":399,\"xp_per_min\":441,\"gold_spent\":12586,\"hero_damage\":11004,\"tower_damage\":2995,\"hero_healing\":0,\"level\":17,\"ability_upgrades\":[{\"ability\":5143,\"time\":122,\"level\":1},{\"ability\":5144,\"time\":268,\"level\":2},{\"ability\":5144,\"time\":329,\"level\":3},{\"ability\":5143,\"time\":452,\"level\":4},{\"ability\":5142,\"time\":637,\"level\":5},{\"ability\":5145,\"time\":720,\"level\":6},{\"ability\":5143,\"time\":853,\"level\":7},{\"ability\":5144,\"time\":923,\"level\":8},{\"ability\":5144,\"time\":1108,\"level\":9},{\"ability\":5145,\"time\":1370,\"level\":10},{\"ability\":5142,\"time\":1371,\"level\":11},{\"ability\":5143,\"time\":1513,\"level\":12},{\"ability\":5142,\"time\":1632,\"level\":13},{\"ability\":5142,\"time\":1855,\"level\":14},{\"ability\":5002,\"time\":1928,\"level\":15},{\"ability\":5145,\"time\":2104,\"level\":16},{\"ability\":5002,\"time\":2139,\"level\":17}]},{\"account_id\":89395208,\"player_slot\":129,\"hero_id\":92,\"item_0\":79,\"item_1\":180,\"item_2\":36,\"item_3\":90,\"item_4\":0,\"item_5\":0,\"kills\":3,\"deaths\":3,\"assists\":17,\"leaver_status\":0,\"gold\":2200,\"last_hits\":31,\"denies\":1,\"gold_per_min\":298,\"xp_per_min\":336,\"gold_spent\":9388,\"hero_damage\":8401,\"tower_damage\":1073,\"hero_healing\":867,\"level\":15,\"ability_upgrades\":[{\"ability\":5482,\"time\":213,\"level\":1},{\"ability\":5481,\"time\":310,\"level\":2},{\"ability\":5481,\"time\":352,\"level\":3},{\"ability\":5480,\"time\":440,\"level\":4},{\"ability\":5481,\"time\":611,\"level\":5},{\"ability\":5480,\"time\":693,\"level\":6},{\"ability\":5481,\"time\":881,\"level\":7},{\"ability\":5483,\"time\":1098,\"level\":8},{\"ability\":5480,\"time\":1266,\"level\":9},{\"ability\":5480,\"time\":1489,\"level\":10},{\"ability\":5483,\"time\":1553,\"level\":11},{\"ability\":5482,\"time\":1919,\"level\":12},{\"ability\":5482,\"time\":1990,\"level\":13},{\"ability\":5482,\"time\":2146,\"level\":14},{\"ability\":5002,\"time\":2476,\"level\":15}]},{\"account_id\":4294967295,\"player_slot\":130,\"hero_id\":53,\"item_0\":152,\"item_1\":98,\"item_2\":96,\"item_3\":63,\"item_4\":65,\"item_5\":88,\"kills\":7,\"deaths\":6,\"assists\":17,\"leaver_status\":0,\"gold\":3534,\"last_hits\":277,\"denies\":5,\"gold_per_min\":642,\"xp_per_min\":751,\"gold_spent\":16900,\"hero_damage\":12718,\"tower_damage\":1597,\"hero_healing\":0,\"level\":23,\"ability_upgrades\":[{\"ability\":5247,\"time\":169,\"level\":1},{\"ability\":5246,\"time\":285,\"level\":2},{\"ability\":5247,\"time\":336,\"level\":3},{\"ability\":5245,\"time\":479,\"level\":4},{\"ability\":5247,\"time\":531,\"level\":5},{\"ability\":5248,\"time\":641,\"level\":6},{\"ability\":5247,\"time\":685,\"level\":7},{\"ability\":5246,\"time\":746,\"level\":8},{\"ability\":5246,\"time\":870,\"level\":9},{\"ability\":5246,\"time\":1080,\"level\":10},{\"ability\":5248,\"time\":1103,\"level\":11},{\"ability\":5245,\"time\":1328,\"level\":12},{\"ability\":5245,\"time\":1370,\"level\":13},{\"ability\":5245,\"time\":1502,\"level\":14},{\"ability\":5002,\"time\":1614,\"level\":15},{\"ability\":5248,\"time\":1890,\"level\":16},{\"ability\":5002,\"time\":1943,\"level\":17},{\"ability\":5002,\"time\":2180,\"level\":18},{\"ability\":5002,\"time\":2299,\"level\":19},{\"ability\":5002,\"time\":2386,\"level\":20},{\"ability\":5002,\"time\":2396,\"level\":21},{\"ability\":5002,\"time\":2458,\"level\":22},{\"ability\":5002,\"time\":2462,\"level\":23}]},{\"account_id\":4294967295,\"player_slot\":131,\"hero_id\":31,\"item_0\":79,\"item_1\":214,\"item_2\":108,\"item_3\":0,\"item_4\":46,\"item_5\":0,\"kills\":7,\"deaths\":6,\"assists\":6,\"leaver_status\":0,\"gold\":1791,\"last_hits\":63,\"denies\":2,\"gold_per_min\":299,\"xp_per_min\":430,\"gold_spent\":8716,\"hero_damage\":9625,\"tower_damage\":791,\"hero_healing\":569,\"level\":17,\"ability_upgrades\":[{\"ability\":5134,\"time\":165,\"level\":1},{\"ability\":5135,\"time\":318,\"level\":2},{\"ability\":5134,\"time\":338,\"level\":3},{\"ability\":5136,\"time\":394,\"level\":4},{\"ability\":5134,\"time\":506,\"level\":5},{\"ability\":5137,\"time\":569,\"level\":6},{\"ability\":5136,\"time\":604,\"level\":7},{\"ability\":5134,\"time\":680,\"level\":8},{\"ability\":5136,\"time\":857,\"level\":9},{\"ability\":5136,\"time\":1023,\"level\":10},{\"ability\":5137,\"time\":1043,\"level\":11},{\"ability\":5135,\"time\":1226,\"level\":12},{\"ability\":5135,\"time\":1319,\"level\":13},{\"ability\":5135,\"time\":1501,\"level\":14},{\"ability\":5002,\"time\":1749,\"level\":15},{\"ability\":5137,\"time\":2116,\"level\":16},{\"ability\":5002,\"time\":2143,\"level\":17}]},{\"account_id\":110928105,\"player_slot\":132,\"hero_id\":49,\"item_0\":152,\"item_1\":154,\"item_2\":112,\"item_3\":36,\"item_4\":63,\"item_5\":46,\"kills\":17,\"deaths\":4,\"assists\":15,\"leaver_status\":0,\"gold\":5225,\"last_hits\":153,\"denies\":9,\"gold_per_min\":550,\"xp_per_min\":856,\"gold_spent\":16389,\"hero_damage\":24287,\"tower_damage\":2961,\"hero_healing\":0,\"level\":25,\"ability_upgrades\":[{\"ability\":5228,\"time\":170,\"level\":1},{\"ability\":5226,\"time\":260,\"level\":2},{\"ability\":5226,\"time\":300,\"level\":3},{\"ability\":5228,\"time\":358,\"level\":4},{\"ability\":5228,\"time\":431,\"level\":5},{\"ability\":5229,\"time\":471,\"level\":6},{\"ability\":5227,\"time\":525,\"level\":7},{\"ability\":5226,\"time\":555,\"level\":8},{\"ability\":5227,\"time\":668,\"level\":9},{\"ability\":5228,\"time\":697,\"level\":10},{\"ability\":5229,\"time\":786,\"level\":11},{\"ability\":5226,\"time\":1125,\"level\":12},{\"ability\":5227,\"time\":1182,\"level\":13},{\"ability\":5227,\"time\":1248,\"level\":14},{\"ability\":5002,\"time\":1296,\"level\":15},{\"ability\":5229,\"time\":1368,\"level\":16},{\"ability\":5002,\"time\":1577,\"level\":17},{\"ability\":5002,\"time\":1712,\"level\":18},{\"ability\":5002,\"time\":1859,\"level\":19},{\"ability\":5002,\"time\":1936,\"level\":20},{\"ability\":5002,\"time\":2111,\"level\":21},{\"ability\":5002,\"time\":2276,\"level\":22},{\"ability\":5002,\"time\":2331,\"level\":23},{\"ability\":5002,\"time\":2424,\"level\":24},{\"ability\":5002,\"time\":2424,\"level\":25}]}],\"radiant_win\":false,\"duration\":2293,\"start_time\":1381906236,\"match_id\":346805844,\"match_seq_num\":316035716,\"tower_status_radiant\":4,\"tower_status_dire\":1846,\"barracks_status_radiant\":3,\"barracks_status_dire\":63,\"cluster\":151,\"first_blood_time\":112,\"lobby_type\":0,\"human_players\":10,\"leagueid\":0,\"positive_votes\":0,\"negative_votes\":0,\"game_mode\":1}}");
JSONArray players = result.optJSONArray("players");
Player player; String data = null;
for (int i = 0; i < players.length(); i++) {
player = new Player(players.getJSONObject(i));
data += player.getFormattedHero();
}
display.setText(data);
assuming i have this code on my project. i am wondering what is the problem on this code. if any of you can help me i would be very glad. click here to view the formatted json string.

in current json string players is an JSONArray which is inside result JSONObject so you will need to first get result JSONObject from main JSONObject then get players JSONArray from it as:
JSONObject result = new JSONObject(<JSON_STRING_HERE>);
// get result JSONObject
JSONObject jsonobj_result = result.optJSONObject("result");
// get players JSONArray
JSONArray players = jsonobj_result.optJSONArray("players");
Player player; String data = null;
for (int i = 0; i < players.length(); i++) {
player = new Player(players.getJSONObject(i));
data += player.getFormattedHero();
}

String data = null;
replaced by
StringBuffer data = new StringBuffer();
may better.

JSONObject result;
URL url = new URL(
"");
URLConnection openConnection = url.openConnection();
openConnection.connect();
InputStream inputStream = openConnection.getInputStream();
String inputStream2String = inputStream2String(inputStream);
result = new JSONObject(inputStream2String);
// here is my add code
// you can look the all names in the thi jsonObject
JSONArray names = result.names();// no hava players name in
// here; so
String string = result.getString("result");
result = new JSONObject(string);
// my add code end
// look names again
names = result.names();// hava players name so get
// optJSONArray for players
JSONArray players = result.optJSONArray("players");// getData is ok

Related

How To store json values in a set of arrays in android

i have a json file like so :
http://da.pantoto.org/api/files
i want to store these values in variables to be used later. The values should be stored in some String array variables like id[], uploadDate[], url[].
i can find examples using ListView and ArrayAdapter. but thats not what i really want. Anyone can help??
This is only an example to show how you can get the values from JSON. You need to store these values as you need.
JSONObject jsonObject = new JSONObject("your response");
JSONArray filedetails = jsonObject.getJSONArray("files");
for(int i=0; i<filedetails.size();i++){
String id = filedetails.get(i).getString("id");
JSONArray tagsdetails= filedetails.get(i).getJSONArray("tags");
for(int i=0; i<tagsdetails.size();i++){
//fetch values in it
}
}
You can't do it exactly that way, You have to iterate trough the array and get each object properties individually and then if you want you could create an array to store each object property.
//From the example: http://da.pantoto.org/api/files
JSONArray files = null;
//Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
//Making a request to url and getting response
String jsonStr = sh.makeServiceCall("http://da.pantoto.org/api/files", ServiceHandler.GET);
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
files = jsonObj.getJSONArray("files");
//YOUR NEW ARRAY
String[] ids = new String[20];
String[] urls = new String[20];
//etc
//looping through All files
for (int i = 0; i < files.length(); i++) {
JSONObject c = files.getJSONObject(i);
String id = c.getString("id");
String url = c.getString("url");
//etc
//HERE IS WHAT YOU COULD DO OPTIONALLY IF YOU WANT HAVE IT ALL ON A SINGLE ARRAY WITHOUT OBJECTS:
ids[i] = id;
urls[i] = url;
//etc
}
this is a simple way to do this. see i also done this kind of string array and later use
try {
JSONObject jObj = new JSONObject(strDocketListResponse);
JSONArray jsonArray = jObj.getJSONArray("GetManifestDocketListResult");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject detailsObject = jsonArray.getJSONObject(i);
String strDktNo = detailsObject.getString("Docketno");
String strDocketDate = detailsObject.getString("DocketDate");
String strOrigin = detailsObject.getString("Origin");
String strDestination = detailsObject.getString("Destination");
String[] strDocketNoDkt = new String[]{strDktNo};
String[] strDocketDateDkt = new String[]{strDocketDate};
String[] strDocketOriginDkt = new String[]{strOrigin};
String[] strDocketDestnDkt = new String[]{strDestination};
for (int sanjay = 0; sanjay < strDocketNoDkt.length; sanjay++) {
ItemCheckList itemCheckList = new ItemCheckList();
itemCheckList.setDocket_NO(strDocketNoDkt[sanjay]);
itemCheckList.setDocket_Date(strDocketDateDkt[sanjay]);
itemCheckList.setDocket_Origin(strDocketOriginDkt[sanjay]);
itemCheckList.setDocket_Destination(strDocketDestnDkt[sanjay]);
checkLists.add(itemCheckList);
}
checkAdapter = new ItemCheckAdapter(ActivityManifestEntry.this, checkLists, check_all);
manifestListView.setAdapter(checkAdapter);
}
} catch (Exception e) {
e.printStackTrace();
}

Parsing json array from the JSON object in Android

I am trying to parse a JSON array from a string which I receive from the server.
Example of the array is
{"data":[{"id":703,"status":0,"number":"123456","name":"Art"}]}
I am trying to parse that using the below code which is giving me Classcast Exception which shows JSonArray can not be cast to List
JSONObject o = new JSONObject(result.toString());
JSONArray slideContent = (JSONArray) o.get("data");
Iterator i = ((List<NameValuePair>) slideContent).iterator();
while (i.hasNext()) {
JSONObject slide = (JSONObject) i.next();
int title = (Integer)slide.get("id");
String Status = (String)slide.get("status");
String name = (String)slide.get("name");
String number = (String)slide.get("number");
Log.v("ONMESSAGE", title + " " + Status + " " + name + " " + number);
// System.out.println(title);
}
What should be the correct way of parsing it?
It makes sense as a JSONArray cannot be cast to a List<>, nor does it have an iterator.
JSONArray has a length() property which returns its length, and has several get(int index) methods which allow you to retrieve the element in that position.
So, considering all these, you may wish to write something like this:
JSONObject o = new JSONObject(result.toString());
JSONArray slideContent = o.getJSONArray("data");
for(int i = 0 ; i < slideContent.length() ; i++) {
int title = slideContent.getInt("id");
String Status = slideContent.getString("status");
// Get your other values here
}
you should do like this:
JSONObject o = new JSONObject(result.toString());
JSONArray array = jsonObject.getJSONArray("data");
JSONObject jtemp ;
ArrayList<MData/*a sample class to store data details*/> dataArray= new ArrayList<MData>();
MData mData;
for(int i=0;i<array.length();i++)
{
mData = new MData();
jtemp = array.getJSONObject(i); //get i record of your array
//do some thing with this like
String id = jtemp.getString("id");
mData.setId(Integer.parseInt(id));
///and other details
dataArray.put(mData);
}
and MData.class
class MData{
private int id;
/....
public void setId(int id){
this.id = id;
}
//.....
}

Extracting values from json file android

My json string is as follows:
i pass it to a json object as follows but i get only the golfs value.
jsonObject = new JSONObject(details_circuit);
System.out.println("jsonObject "+jsonObject.toString());
jsArray = jsonObject.getJSONArray("golfs");
if i do
jsonObject1 = new JSONObject(jsonObject.getString("circuit"));
i cannot get any value. Any idea.
jsonObject1 = new JSONObject(jsonObject.getString("circuit"));//Wrong because circuit is a jsonObject
Use this instead
JSONObject jsonObject1 = jsonObject.getJSONObject("circuit");
or use
JSONObject circuit = (JSONObject)jsonObject.get('circuit');
Reference
JSONObject jsonObject = new JSONObject(details_circuit);
JSONObject circuit=jsonObject.getJSONObject("circuit");
int id=circuit.getInt("#id");
int version=circuit.getInt("#version");
String name=circuit.getString("#name");
String description=circuit.getString("#description");
JSONObject row;
int id ;
int version;
String name;
JSONArray golfs=jsonObject.getJSONObject("golfs");
for(int i=0;i<golfs.length();i++){
row = golfs.getJSONObject(i);
id = row.getInt("id");
version=row.getInt("#version");
name=row.getString("#name");
}
"circuit": { is a JSONObject
Change to
jsonObject1 = (JSONObject)jsonObject.getJSONObject("circuit");
int id = jsonObject1.getInt("#id"):
jsonObject1 = jsonObject.getJSONObject("circuit");
Use following code.
JSONObject json = jParser.getJSONFromUrl(url);
JSONObject json1 = json.getJSONObject("circuit");
int id = json1.getInt("id");
For JsonArray Use following.
JSONArray jarray = json.getJSONArray("golfs");
JSONObject jarray = json.getJSONObject(0);
int id = json1.getInt("id");

Not getting values from json in android

I am trying to parse my json with:
for(int i = 0; i < json.getJSONArray("JSON").length(); i++) {
String taste = json.getJSONArray("JSON").getJSONObject(i).getString("taste");
String rate = json.getJSONArray("JSON").getJSONObject(i).getString("rate");
int foo = Integer.parseInt(rate);
count = count + foo;
//create object
BeerTastes tempTaste = new BeerTastes(taste, rate);
//add to arraylist
tasteList.add(tempTaste);
Log.d("taste",tempTaste.taste);
Log.d("number",tempTaste.percent);
}
But my logs at the end are not outputting anything at all so I assume I am not parsing my json correctly. The json I am looking at is:
[{"taste":"Bitter","rate":"13"},{"taste":"Malty","rate":"3"},{"taste":"Smooth","rate":"3"},{"taste":"Dry","rate":"1"}]
I think I may be wrong with:
json.getJSONArray("JSON")
because my array doesnt have a name but I it has to take a string...
I find no 'JSON' in your JSON String.So i think you should write like this:
JSONArray jsonArray = new JSONArray(json);
then:
for(int i = 0; i < jsonArray.length(); i++) {
String taste = jsonArray.getJSONObject(i).getString("taste");
String rate = jsonArray.getJSONObject(i).getString("rate");
int foo = Integer.parseInt(rate);
count = count + foo;
//create object
BeerTastes tempTaste = new BeerTastes(taste, rate);
//add to arraylist
tasteList.add(tempTaste);
Log.d("taste",tempTaste.taste);
Log.d("number",tempTaste.percent);
}

How to add values to a string array in android

I have a problem with my code,
I have a json array
[{"Response":{"data":"sibin1"}},{"Response":{"data":"sibin2"}},
{"Response": {"data":"sibin3"}}]
And iam trying to extract the json data using the below code,Here i added only some parts of the coode
JSONArray finalResult = new JSONArray(tokener);
int finalresultlengt=finalResult.length();
JSONObject json_data = new JSONObject();
for (int i = 0; i < finalResult.length(); i++)
{
json_data = finalResult.getJSONObject(i);
System.out.println("json dataa"+json_data.names().toString());
JSONObject menuObject = json_data.getJSONObject("Response");
result= menuObject.getString("data");
System.out.println(result);
}
The code is worked very well
when the value of
i=0 ,result is sibin1
i=1 ,result is sibin2
i=2 ,result is sibin3
But my problem is , i need to store the result in a string array of length finalresultlength inside the given for loop, also i need to print the values in the string array in a for loop outside the given for loop
if anybody knows please help me...........
You could do this way as well.
Create an ArrayList of size 'finalresultlengt' and the add the values in.
list.add(result); // variable 'result' in your case is the value from JSON
If you have more values to be added, create a POJO class.
class POJO {
private String dataVal;
public void setDataVal(String dataVal) {
this.dataVal = dataVal;
}
public String getDataVal() {
return dataVal;
}
}
Then create an ArrayList of type POJO.
ArrayList<POJO> list = new ArrayList<POJO>(finalresultlengt);
EDIT
JSONArray finalResult = new JSONArray(tokener);
int finalresultlengt=finalResult.length();
JSONObject json_data = new JSONObject();
ArrayList<String> list = new ArrayList<String>(finalresultlengt);
for (int i = 0; i < finalResult.length(); i++) {
json_data = finalResult.getJSONObject(i);
System.out.println("json dataa"+json_data.names().toString());
JSONObject menuObject = json_data.getJSONObject("Response");
result= menuObject.getString("data");
list.add(result);
}
Populate values from ArrayList.
for(String value : list)
System.out.println(value);

Categories

Resources