Remove object from json response Android - java

Hello guys I need your help. How do I remove this from my json response?
[
{
"Woa": [
"Seo",
"Rikjeo",
"JDa"
]
},
"Aha",
"Aad",
"Char"
]
I want to remove this:
{
"Woa": [
"Seo",
"Rikjeo",
"JDa"
]
}
This is what I tried so far:
for (int i = 0; i < array.length(); ++i) {
list.add(array.getString(i));
}
list.remove(0);
But it is still not removed. How do I do that? Any ideas will be greatly appreciated
Edited list.remove(1) to (0)

After removing the item you need to create the JSON again using the list.
list.remove(1);
JSONArray jsArray = new JSONArray(list);
If you want to convert JSONArray to JSON string:
jsArray.toString()

Your list will be updated, not your JSON object.
Why don't you use a JSONparser.
JSONObject obj = new JSONObject(mystring);
obj.remove("entryname");

One problem is that the element you are trying to remove is the first one, and the index of the first element of a JSONArray object is zero.
You are calling list.remove(1) which removes the second element of the array.
The following should be sufficient:
list.remove(0);
... without the stuff prior to it.
If this is not working, then my guess is that you are removing the element too late; i.e. after the JSONArray object has been serialized. However, we need to see more (relevant) code to be sure.
The Android documentation for JSONArray fails to mention that array indexing is zero-based. However, it is. I checked the source code. Furthermore most other Java data structures (arrays, lists etcetera) use zero-based indexing. (A notable exception is Java data structures modelled on the W3C DOM APIs.)

You should make something that's more general so you don't have to modify your code EVERY time the JSON changes. For example:
//here we create the JSONArray object from the string that contains the json data
JSONArray array = new JSONArray(myJsonString);
//here we create a list that represents the final result - a list of Strings
List<String> list = new ArrayList<>();
//here we parse every object that the JSONArray contains
for (int i = 0; i < array.length(); i++) {
//if the current item is an JSONObject, then we don't need it, so we continue our iteration
//this check is not necessary because of the below check,
//but it helps you see things clearly: we IGNORE JSONObjects from the array
if (array.get(i) instanceof JSONObject) {
continue;
}
//if our current object is a String, we add it to our final result
if (array.get(i) instanceof String) {
list.add(array.getString(i));
}
}

you can parse your string to JSONArray, the first element of the array is what you want.
#Test
public void test() {
String str = "[ {\"Woa\": [\"Seo\",\"Rikjeo\",\"JDa\"]},\"Aha\",\"Aad\",\"Char\"]";
try {
JSONArray array;
array = new JSONArray(str);
System.out.println(array);
System.out.println(array.get(0));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
[{"Woa":["Seo","Rikjeo","JDa"]},"Aha","Aad","Char"]
{"Woa":["Seo","Rikjeo","JDa"]}
PASSED: test

You Can one by one remove
while (user_data.length() > 0) {
user_data.remove(user_data.keys().next());
}

Related

JSON array parsing with Java [duplicate]

I am building an android app that needs to download and synchronise with an online database, I am sending my query from the app to a php page which returns the relevant rows from a database in JSON format.
can someone please tell me the best way to iterate through a JSON array?
I receive an array of objects:
[{json object},{json object},{json object}]
What is the simplest piece of code I could use to access the JSONObjects in the array?
EDIT: now that I think of it the method I used to iterate the loop was:
for (String row: json){
id = row.getInt("id");
name = row.getString("name");
password = row.getString("password");
}
So I guess I had was somehow able to turn the returned Json into and iterable array. Any Ideas how I could achieve this?
I apologise for my vaguness but I had this working from an example I found on the web and have since been unable to find it.
I think this code is short and clear:
int id;
String name;
JSONArray array = new JSONArray(string_of_json_array);
for (int i = 0; i < array.length(); i++) {
JSONObject row = array.getJSONObject(i);
id = row.getInt("id");
name = row.getString("name");
}
Is that what you were looking for?
I have done it two different ways,
1.) make a Map
HashMap<String, String> applicationSettings = new HashMap<String,String>();
for(int i=0; i<settings.length(); i++){
String value = settings.getJSONObject(i).getString("value");
String name = settings.getJSONObject(i).getString("name");
applicationSettings.put(name, value);
}
2.) make a JSONArray of names
JSONArray names = json.names();
JSONArray values = json.toJSONArray(names);
for(int i=0; i<values.length(); i++){
if (names.getString(i).equals("description")){
setDescription(values.getString(i));
}
else if (names.getString(i).equals("expiryDate")){
String dateString = values.getString(i);
setExpiryDate(stringToDateHelper(dateString));
}
else if (names.getString(i).equals("id")){
setId(values.getLong(i));
}
else if (names.getString(i).equals("offerCode")){
setOfferCode(values.getString(i));
}
else if (names.getString(i).equals("startDate")){
String dateString = values.getString(i);
setStartDate(stringToDateHelper(dateString));
}
else if (names.getString(i).equals("title")){
setTitle(values.getString(i));
}
}
Unfortunately , JSONArray doesn't support foreach statements, like:
for(JSONObject someObj : someJsonArray) {
// do something about someObj
....
....
}
When I tried #vipw's suggestion, I was faced with this exception:
The method getJSONObject(int) is undefined for the type JSONArray
This worked for me instead:
int myJsonArraySize = myJsonArray.size();
for (int i = 0; i < myJsonArraySize; i++) {
JSONObject myJsonObject = (JSONObject) myJsonArray.get(i);
// Do whatever you have to do to myJsonObject...
}
If you're using the JSON.org Java implementation, which is open source, you can just make JSONArray implement the Iterable interface and add the following method to the class:
#Override
public Iterator iterator() {
return this.myArrayList.iterator();
}
This will make all instances of JSONArray iterable, meaning that the for (Object foo : bar) syntax will now work with it (note that foo has to be an Object, because JSONArrays do not have a declared type). All this works because the JSONArray class is backed by a simple ArrayList, which is already iterable. I imagine that other open source implementations would be just as easy to change.
On Arrays, look for:
JSONArray menuitemArray = popupObject.getJSONArray("menuitem");
You are using the same Cast object for every entry.
On each iteration you just changed the same object instead creating a new one.
This code should fix it:
JSONArray jCastArr = jObj.getJSONArray("abridged_cast");
ArrayList<Cast> castList= new ArrayList<Cast>();
for (int i=0; i < jCastArr.length(); i++) {
Cast person = new Cast(); // create a new object here
JSONObject jpersonObj = jCastArr.getJSONObject(i);
person.castId = (String) jpersonObj.getString("id");
person.castFullName = (String) jpersonObj.getString("name");
castList.add(person);
}
details.castList = castList;
While iterating over a JSON array (org.json.JSONArray, built into Android), watch out for null objects; for example, you may get "null" instead of a null string.
A check may look like:
s[i] = array.isNull(i) ? null : array.getString(i);

Looping over an array with alternating elements

Difficult to sum up the question in one sentence. It's easier to show you what I want. I have the following response of a Solr query:
{
.....
},
"facet_counts":{
"facet_queries":{},
"facet_fields":{
"city":[
"New York",23258,
"Los Angeles",13322,
"Paris",1189]},
"facet_ranges":{},
"facet_intervals":{},
"facet_heatmaps":{}}}
I use GSON to parse this JSON data and get a JsonArray containing the array "city". That contains alternating elements for the field name (key) and the corresponding value. So e.g. we have 13322 hits for "Los Angeles". I want to iterate over this array to get out the key=value pairs it contains.
I can think of a number of simple solutions for this task, for example
boolean isKey = true;
String key;
String val;
for(JsonElement je : facetMT) {
if(isKey) {
key = je.getAsString();
isKey = false;
} else {
val = je.getAsString();
resultMap.add(key,val);
isKey = true;
}
}
Or any other way to distinguish the odd and even elements, like checking for divisibility by 2.
But that feels crude. I'm sure Java has some other, elegant way to do this, like working with an iterator and skipping every other element. But I'm not good enough in Java to know how to do that, or any other supersmart way.
Any suggestions, or should I just go with the crude code?
If you're sure about the length of the array then you should read twice at one:
for(int i = 0; i < facetMT.size() - 1; i = i + 2) {
JsonElement key = facetMT.get(i).getAsString();
JsonElement value= facetMT.get(i + 1).getAsString();
resultMap.add(key,val);
}
Note the i < facetMT.size() - 1 condition to avoid IndexOutOfBoundsException
If you are sure that array will always have even amount of elements you can use two of them in one iteration like
Iterator<JsonElement> it = facetMT.iterator();
while (it.hasNext()) {
// iterate over two elements
resultMap.put(it.next().getAsString(), it.next().getAsString());
}
But to be honest proper answer would be enforcing results in form of array of objects like
"city":[
{"cityName":"New York", "amount":23258},
{"cityName":"Los Angeles", "amount":13322},
{"cityName":"Paris", "amount":1189}
]
instead of what you have now
"city":[
"New York",23258,
"Los Angeles",13322,
"Paris",1189
]
This way your code could look like:
for (JsonElement jsonElement : facetMT) {
JsonObject obj = (JsonObject)jsonElement;
resultMap.put(obj.get("cityName").getAsString(),
obj.get("amount").getAsString());
}
In this case use for loop with indexes
For(int i=0;i<...
FaceMT[i]= altered value.
Alterate value doesn't work with iterator for

How to loop through json arrays nested inside json objects

I have this json text which has some symptoms associated with head diseases:
{
"Head": {
"Agitation": {
"conditions": "Generalized anxiety disorder,Medication reaction or side-effect"
},
"Anxiety": {
"conditions": "Generalized anxiety disorder,Depression (Adult)"
},
"Apathy": {
"conditions": "Depression (Adult),Medication reaction or side-effect,Dementia in head injury"
}
}
}
What I want is to access and display every symptom in this Head block using a for loop, and then access each symptom's conditions and store them separately as arrays.
This java code works but it's functionality is limited:
JSONObject jsonObject = (JSONObject) object;
JSONObject bodyPart = (JSONObject) jsonObject.get("Head");
JSONObject symptoms = (JSONObject) name.get("Agitation");
String res = (String) symptoms.get("conditions");
String[] tokens = res.split(",");
for (int i = 0; i < tokens.length; i++){
System.out.println(tokens[i]);}
Instead of displaying just the conditions of Agitation, how can I display every condition associated with every symptom without having to pass their String values manually into the get methods?
I don't know if I should use JSONArray for "Head" instead of JSONObject to access the symptoms.
Assuming this is the org.json package, you can use JSONObject's keys() method to get an iterator over the object's keys.
Iterator bodyParts = jsonObject.keys();
while (bodyParts.hasNext()){
String bodyPart = (String) bodyParts.next();
JSONObject symptomsJson = jsonObject.getJSONObject(bodyPart);
Iterator symptoms = symptomsJson.keys();
// And so on...
}
My friend, if you're using java, use objects, no plain text, my recommendation is to use Json.simple,
That way you can have List of objects and use the properties you want, take a look on the link examples.

How to remove empty objects from Json Array

So the json is something like it,
"stores": [
{
"amazon": []
},
{
"flipkart": {
"product_store": "Flipkart",
"product_store_logo": "http://images-api.datayuge.in/image/ZmxpcGthcnRfc3RvcmUucG5n.png",
"product_store_url": "https://price-api.datayuge.com/redirect?id=aHR0cHM6Ly9kbC5mbGlwa2FydC5jb20vZGwvbWktYTEtYmxhY2stNjQtZ2IvcC9pdG1leDl3eHh6M2FtamF0P3BpZD1NT0JFWDlXWFVTWlZZSEVUJmFmZmlkPWFydW5iYWJ1bA",
"product_price": "14999",
"product_offer": "",
"product_color": "",
"product_delivery": "3-4",
"product_delivery_cost": "0",
"is_emi": "1",
"is_cod": "1",
"return_time": "10 Days"
}
},
{
"snapdeal": []
}
]
So the non empty object like flipkart is a JsonObject but all other empty objects are array. So I am so confused about how to remove them.
JSONArray store_array = product_details_json.getJSONObject("data").getJSONArray("stores");
for (int i = 0; i<store_array.length(); i++){
JSONObject store = store_array.getJSONObject(i);
if (!store.getJSONObject(store.keys().next()).has("product_store")){
store_array.remove(i);
}else {
Log.i("Size :",store_array.length()+"");
}
}
But that's not working. I know I am doing this all wrong. Because it has both array and objects so i get the following error
Value [] at amazon of type org.json.JSONArray cannot be converted to JSONObject
Need Help!
I see two problems with your code:
Your JSON structure for "stores" is heterogeneous — some elements have a key that maps to an array and some to an object. That's the immediate cause of the error you are seeing. You can either modify your JSON so everything key maps to an object or code defensively.
When you remove an entry, all subsequent entries move up one space, but since you then increment the loop index i, you skip the entry that just moved into the index you just removed. The easiest way to deal with that is to iterate through store_array in reverse order.
Putting this all together (and assuming you aren't going to change your JSON structure), something like the following (untested) should work:
JSONArray store_array = product_details_json.getJSONObject("data").getJSONArray("stores");
for (int i = store_array.length() - 1; i >= 0; i--){
JSONObject store = store_array.getJSONObject(i);
Object storeData = store.get(store.keys().next());
boolean isValidStore = storeData instanceof JSONObject
&& ((JSONObject) storeData).has("product_store");
if (!isValidStore) {
store_array.remove(i);
}
}

how to iterate the json objects in the jsonarray [duplicate]

I am building an android app that needs to download and synchronise with an online database, I am sending my query from the app to a php page which returns the relevant rows from a database in JSON format.
can someone please tell me the best way to iterate through a JSON array?
I receive an array of objects:
[{json object},{json object},{json object}]
What is the simplest piece of code I could use to access the JSONObjects in the array?
EDIT: now that I think of it the method I used to iterate the loop was:
for (String row: json){
id = row.getInt("id");
name = row.getString("name");
password = row.getString("password");
}
So I guess I had was somehow able to turn the returned Json into and iterable array. Any Ideas how I could achieve this?
I apologise for my vaguness but I had this working from an example I found on the web and have since been unable to find it.
I think this code is short and clear:
int id;
String name;
JSONArray array = new JSONArray(string_of_json_array);
for (int i = 0; i < array.length(); i++) {
JSONObject row = array.getJSONObject(i);
id = row.getInt("id");
name = row.getString("name");
}
Is that what you were looking for?
I have done it two different ways,
1.) make a Map
HashMap<String, String> applicationSettings = new HashMap<String,String>();
for(int i=0; i<settings.length(); i++){
String value = settings.getJSONObject(i).getString("value");
String name = settings.getJSONObject(i).getString("name");
applicationSettings.put(name, value);
}
2.) make a JSONArray of names
JSONArray names = json.names();
JSONArray values = json.toJSONArray(names);
for(int i=0; i<values.length(); i++){
if (names.getString(i).equals("description")){
setDescription(values.getString(i));
}
else if (names.getString(i).equals("expiryDate")){
String dateString = values.getString(i);
setExpiryDate(stringToDateHelper(dateString));
}
else if (names.getString(i).equals("id")){
setId(values.getLong(i));
}
else if (names.getString(i).equals("offerCode")){
setOfferCode(values.getString(i));
}
else if (names.getString(i).equals("startDate")){
String dateString = values.getString(i);
setStartDate(stringToDateHelper(dateString));
}
else if (names.getString(i).equals("title")){
setTitle(values.getString(i));
}
}
Unfortunately , JSONArray doesn't support foreach statements, like:
for(JSONObject someObj : someJsonArray) {
// do something about someObj
....
....
}
When I tried #vipw's suggestion, I was faced with this exception:
The method getJSONObject(int) is undefined for the type JSONArray
This worked for me instead:
int myJsonArraySize = myJsonArray.size();
for (int i = 0; i < myJsonArraySize; i++) {
JSONObject myJsonObject = (JSONObject) myJsonArray.get(i);
// Do whatever you have to do to myJsonObject...
}
If you're using the JSON.org Java implementation, which is open source, you can just make JSONArray implement the Iterable interface and add the following method to the class:
#Override
public Iterator iterator() {
return this.myArrayList.iterator();
}
This will make all instances of JSONArray iterable, meaning that the for (Object foo : bar) syntax will now work with it (note that foo has to be an Object, because JSONArrays do not have a declared type). All this works because the JSONArray class is backed by a simple ArrayList, which is already iterable. I imagine that other open source implementations would be just as easy to change.
On Arrays, look for:
JSONArray menuitemArray = popupObject.getJSONArray("menuitem");
You are using the same Cast object for every entry.
On each iteration you just changed the same object instead creating a new one.
This code should fix it:
JSONArray jCastArr = jObj.getJSONArray("abridged_cast");
ArrayList<Cast> castList= new ArrayList<Cast>();
for (int i=0; i < jCastArr.length(); i++) {
Cast person = new Cast(); // create a new object here
JSONObject jpersonObj = jCastArr.getJSONObject(i);
person.castId = (String) jpersonObj.getString("id");
person.castFullName = (String) jpersonObj.getString("name");
castList.add(person);
}
details.castList = castList;
While iterating over a JSON array (org.json.JSONArray, built into Android), watch out for null objects; for example, you may get "null" instead of a null string.
A check may look like:
s[i] = array.isNull(i) ? null : array.getString(i);

Categories

Resources