My JSON structure is simple :
"categories": [2],
I am succesfull in getting this output on my Android studio code as :
JSONArray jsonArray = jsonObject.getJSONArray("categories");
Log.e("CAT ID", String.valueOf(jsonArray));
Output is :
E/CATÂ ID: [2] //and other IDS respectively
My question is how can I remove the braces and just get the Integer values?
You can call get() to get array value.
//If array contains only one value use get(0)
JSONArray jsonArray = jsonObject.getJSONArray("categories");
jsonArray.get(0);
//If Array contains more value use for() loop
for(int i=0;i<jsonArray.length;i++){
jsonArray.get(i);
}
jsut use below code :
String.valueOf(jsonArray)
.replace("[","")
.replace("]","")
or
jsonArray.toString()
.replace("[","")
.replace("]","")
Related
i am new in this development
here i want to get "Category", and according to category wants to get
"Title" & "Price"
i already generated pojo classes
Item items = gson.fromJson(response,Item.class);
JSONArray json = new JSONArray(response);
//TODO: PARSE category list and CATEGORY's : TITLE, PRICE that is
items
actually i want it in a recycler adapter
> Response: [{"category": "Espresso & Coffee",
> "items": [{"title": "Vacuum Coffee",
> "size": [{
> "label": "medium",
> "price": 125
> }]}],
> }]
I got stuck in the same situation as you did a while ago and ended up handling the json manually instead of gson. Instead of using JsonArrayRequest, try JsonObjectRequest.
After, get each array by 'chopping' up the returns like this:
JsonArray c = response.getJSONArray("array");
Then you attain the values stored within the array by going through a for loop with the limit i<c.length().
However, since you have an arrray with the array you'd be doing something along this line:
for(int i=0; i<c.length(); i++){
//populates the array, in your case, jsonarray size = 4
JSONObject jsonObject = c.getJSONObject(i);
String cat= jsonObject.getString("category"); //gets category String
JSONArray items = jsonObject.getJSONArray("items");
....
here, you write an inner loop in order to get the array items in your object....i hope i'm helping you solve the problem instead of confusing you, lol. If you require an example, here's one which helped me out in figuring which goes where : https://github.com/codepath/android_guides/wiki/Rotten-Tomatoes-Networking-Tutorial
Hi, I have a JSONObject that has multiple arrays in it. I want to get the name of each array as a string does anyone know how I can do this?
Here's how my json looks. so the value I'm trying to get out is the name of the array so B or C in this case. the purpose of this is to set the text of a header on a list view to this value.
{"Contacts": //JSONObject
{
"B"://JSONArray..
[
{"ContactName":sdfsdf,"ID":900,"Number":1368349},
{"ContactName":adsdfd,"ID":1900,"Number":136856},
{"ContactName":adglkhdofg,"ID":600,"Number":136845}
],
"C":[
{"ContactName":alkghoi,"ID":900,"Number":1368349},
{"ContactName":wetete,"ID":1900,"Number":136856},
{"ContactName":dfhtfh,"ID":600,"Number":136845}
]
.....//and so on..
}
}
You can use the JSONObject method names() to return an array of the string names in the object.
It's been a while and i'm trying to ignore some frustrating issue i'm having with json things in java, i'm new to this and read alot however, parsing json in javascript or php was alot better (or easier i dunno) but now in java i cannot convert a jsonobject to jsonarray if it doesn't have a parent, cuz it uses .getJsonArray('array)
BUT what IF i have this :
{"49588":"1.4 TB","49589":"1.4 TB MultiAir","49590":"1.4 TB MultiAir TCT","49591":"1.6L MultiJet","49592":"1750 Tbi","49593":"2.0L MultiJet","49594":"2.0L MultiJet TCT"}
i'm not succeeding in anyway to convert it to array
what i want is to convert this JSONObject to JSONArray loop within its items and add them to a Spinner, now that's the first issue, the second question is: if i convert this to JSONArray how can i add the ID, Text to the spinner? just like the HTML Select tag
<option value="0">Item 1</option>
so it's an issue and a question hope someone can find the solution for this jsonarray thing, without modifying the json output from the website, knowing that if i modify and add a parent to this json, the JSONArray will work. but i want to find the solution for that.
Nothing special i have in the code:
Just a AsynTask Response, a log which is showing the json output i put at the beginning of this question
Log.d("response", "res " + response);
// This will work
jsonCarsTrim = new JSONObject(response);
// This won't work
JSONArray jdata = new JSONArray(response);
Thanks !
How about this:
JSONObject json = new JSONObject(yourObject);
Iterator itr = json.keys();
ArrayList<CharSequence> entries = new ArrayList<CharSequence>();
ArrayList<Integer> links = new ArrayList<Integer>();
int i = 0;
while(itr.hasNext()) {
String key = itr.next().toString();
links.add(i,Integer.parseInt(key));
entries.add(i, (CharSequence) json.getString(key)); //for example
i++;
}
//this is the activity
entriesAdapter = new ArrayAdapter<CharSequence>(this,
R.layout.support_simple_spinner_dropdown_item, entries);
//spinner is the spinner the data is added too
spinner.setAdapter(entriesAdapter);
this should work (works for me), you may have to modify the code.
The way shown, i am adding all entries of the json object into a Spinner, where my json key is the index value and the linked String value of the json object will be shown as Spinner entry (title) in my activity.
Now when an Item is selected, fetch the SelectedItemPosition and you can look it up in the "links" array list, to get the real value.
I'm not sure if this is thing you want but give it a try. There is tutorial to convert the Json to Map. After you convert it, you can iterate through the map.
http://www.mkyong.com/java/how-to-convert-java-map-to-from-json-jackson/
What you have is a JSON object type, not an array type. To iterate you can get the Iterator from the keys method.
After completion of execution of an activity i want to make jSONArray empty or delete it. Because when i am calling the activity repeatedly the new data gets append to the old data present in JSONArray. My jsonArray is as shown below..
[{"record":[
{......},
{.......}
]
},
{"record":[
{..........},
{.........}
]
}
]
First time:
[{"record":[{"intensity":"Low","Body_SubParts":"Scalp","symptom":"Bleeding"}]}]
Second time:
[{"record":[{"intensity":"Low","Body_SubParts":"Scalp","symptom":"Bleeding"},
{"intensity":"Low","Body_SubParts":"Eyes","symptom":"Bleeding"}]}]
After building the JSON array, nullify the JSONArray object..Next time again build the JSON Array
Answered here
// Assuming you have constructed the replacement as
JSONObject jsonArray11_obj2 = new JSONObject();
jsonArray11_obj2.put("intensity", "high");
jsonArray11_obj2.put("Body_SubParts", "Scalp");
// ...
// Then the code doing the replacement would look similar to
int index = 0;
((JSONObject)jsonArray.get(index)).getJSONArray("record").put(0, jsonArray11_obj2);
I'll keep it short. I want to obtain the string value from "token". But from what I can see there is a "data" array within my json object. How do I retrieve the array and, specifically, the token string from this array?
{
"response":true,
"state":8,"data":{
"token":"$2a$08$oeH79FjMV6ZQTO.9qqfToujNBbst420Xx7o9jJdFgbJsJijtEpX\/O",
"numbers":[{
"number":"4581102282",
"enabled":"0",
"custid":"1528511113",
"route":"rock",
"clir":"2",
"mob":"1",
"clip":"",
"mbx":"0",
"trunk":"2",
"valid_from":"2011-09-07",
"valid_to":"2011-10-05"
}]
}
}
Simply use
JSONObject jsonObj=new JSONObject(response);
JSONObject jsonData=jsonObj.getJSONObject("data");
String token=jsonData.getString("token");