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);
Related
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("]","")
I'm loading a json array in my app.but i need to know if the json array is exist or not? because if the json array isn't exist, the app will crash.
for example, I have this json:
{
"music1":
[
{
"art":"",
"artist":"",
"music":"",
"flag":"",
"text":"",
"level":""
},
{
"art":"",
"artist":"",
"music":"",
"flag":"",
"text":"",
"level":""
},
]
}
and I want to know is there any json object named "music1" in my code or not, and then if there was I want to get the json array and show it in a list in my android app.
I'm looking forward a hero that could help me!
I wish this explanation would fit in a comment.
Anyway:
{
"music1":
[
{
"art":"",
"artist":"",
"music":"",
"flag":"",
"text":"",
"level":""
},
{
"art":"",
"artist":"",
"music":"",
"flag":"",
"text":"",
"level":""
}
]
}
Let call the above json object o.
So to check if o has music1, all you need to check is write the following line:
if(o.has("music1")){
JSONArray array= o.getJSONArray("music1");
}
Then you extract the objects of the json array :
for(int i=0;i<array.length();i++){
array.getJSONObject(i);
}
Check the availability of field:
if(jsonObj.has("music1")){
//yes
}
or this case:
JSONArray mJSONArray = jsonObj.optJSONArray("nmusic1");
If array is not available mJSONArray will be null
I have a JSON as below
{
"shippingGroupListJson":[
{
"lineId":123,
"shippedTo":{
"country":"US"
},
"cartons":1,
"group":"4",
"shipInscription":{
"municipalInscription":"",
"inscriptionType":"",
"stateInscription":"",
"suframaInscriptionNumber":"",
"inscriptionBranch":"",
"contributorClass":"",
"inscriptionDigit":"",
"inscriptionNumber":""
},
"shipCartonDetails":[
],
"shippedContact":{
"firstName":"Mjjkk",
"email":"nob#gmail.com",
"fax":"--",
"phone":"80-121",
"lastName":"Henry"
},
"mobilityShipStatus":"Not Yet Shipped",
"shipDate":"13 Dec 2014"
},
{
"lineId":0,
"shippedTo":[
],
"cartons":0,
"group":"5",
"shipInscription":[
],
"shipCartonDetails":[
],
"shippedContact":[
],
"mobilityShipStatus":"",
"shipDate":"",
"shipStatus":""
}
]
}
If you see in this above JSON in key "shippedTo", when there is value , I get a JSON Object and when no value is present then i get a Blank JSONArray.
I need to fix this issue. I cannot communicate with the service team to change this as they won't make changes to it. Can any one tell me how can i do the required changes.
I know this is not the rite way, but i need to do something..
I tried using String.replaceAll(oldChar,newChar);
You say you are using GSON, in which case JsonArray and JsonObject are both subclasses of JsonElement.
But you do not say if you are using the DOM or Streaming technique with GSON.
You should show your parsing code, or the class that's being automatically used for the parsing.
In the meantime, based on what you have said, I would define my shippedTo field in my class as an Object. That should parse correctly (if using DOM), and after that you can analyse exactly what you have in that field.
Instead of:
private MyCountryType shippedTo;
Have:
private Object shippedTo;
You could simply check its type at runtime i.e
JSONObject jsPar=new JSONObject(json_data);
JSONArray jsarrPar=js.getJSONArray("shippingGroupListJson");
for(int i=0;i<jsarrPar.size();i++){
JSONObject js= jsarrPar.getJSONObject(i);
if(js.get("shippedTo") instanceof JSONArray){
//Treat it as an array
}
else{
//Treat it as a single json object
}
}
Furthermore if you need it as an JSONObject immaterial of it being a single element then do this to get your customized string
JSONObject jsPar=new JSONObject(json_data);
JSONArray jsarrPar=js.getJSONArray("shippingGroupListJson");
for(int i=0;i<jsarrPar.size();i++){
JSONObject js= jsarrPar.getJSONObject(i);
JSONObject js=new JSONObject(json_data);
if(js.get("shippedTo") instanceof JSONArray){
js.put("shippedTo",new JSONObject()); //purge the original array with this new blank element
}
else{
//Do your normal stuff
}
}
String curmodstring=jsPar.toString();
Hope you get the general idea.
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.