i m trying to get values from JSONArray inside array, i m able to retrieve whole JSON values into JSONArray successfully but not able to retrieve values inside JSONArray. When i convert JSONArray to JSONObject to get values stored inside JSONArray. It gives error: org.json.JSONException: No value for "banner"
Here is JSON code, i verified JSON code with jsonlint.com and it showed JSON is Validate,
[
{"code":"banner","moduletitle":0,
"banner":
[
{"image":"http://imageurl"},
{"image":"http://imageurl"},
{"image":"http://imageurl"}
]
}
]
I m trying to get this from 3 hour but no luck. i m new in JSON and do not know how JSON Actually work, and also read abut GSON Library to get JSON values. here is My Java code.
JSONArray jsonObj = null;
String image_url = "";
String banner_code ="";
try {
jsonObj =new JSONArray(lib_function.getJSONUrl( jsontags.Top_Banner_JOSN_URLs));
Log.d("value retrun :","" +jsonObj);
//---vlaue is coming and print in Log ----//
} catch (JSONException e) {
Log.v("Error in Parser :", " " + e);
Log.d("no value retrun :", "failed to convert");
}
try{
JSONObject jo = new JSONObject();
JSONArray ja = new JSONArray();
// populate the array
jo.put("arrayName", jsonObj);
JSONArray subArray = jo.getJSONArray("banner");
image_url= subArray.getString(Integer.parseInt("image"));
Log.d("banner code",""+subArray);
}catch(Exception e)
{
Log.d("not working",""+e);
}
I folllow this question but luck:
How to parse JSON Array inside another JSON Array in Android
If anyone suggest, what i m doing wrong will be appreciate. or let me know, where i can get more information about json
UPDATE thanks too all to give their precious time for answering my stupid question. All answers are correct , but i can accept only one answer. A Big thanks to all
Here:
JSONObject jo = new JSONObject();
JSONArray ja = new JSONArray();
// populate the array
jo.put("arrayName", jsonObj);
Because parsing jsonObj JSONArray so no need to create new JSONArray and JSONObject to extract it from jsonObj. remove all above three lines.
banner JSONArray is inside JSONObject which is contained by jsonObj JSONArray, get it as:
JSONObject jsonObject=jsonObj.optJSONObject(0);
JSONArray subArray = jsonObject.getJSONArray("banner");
// get code key from `jsonObject`
String strCode=jsonObject.optString("code");
// get all images urls from `subArray`
for(int index=0;index<subArray.length();index++){
JSONObject imgJSONObject=subArray.optJSONObject(index);
// get image urls
String strImgURL=imgJSONObject.optString("image");
}
Also, if jsonObj JSONArray contains multiple JSONObject's then use for-loop to iterate it.
I am assuming you have the rest of the values accessible to you, so posting just this snippet.
code=jsonObject.getString("code");
moduletitle=jsonObject.getString("moduletitle");
banner=jsonObject.getJSONArray("banner");
jsonObj =new JSONArray(lib_function.getJSONUrl( jsontags.Top_Banner_JOSN_URLs);
From above line you will get JSONArray. So now loop it and get you banner JSONArray.Again loop bannerArray and you will get image Urls
If You want value of "image" which is in json arrray than
String response = "your response";
try{
JsonArray jAry = new JsonArray(response);
JsonObject jObj = jAry.getJsonObject(0);
JsonArray jsonBanner = jObj.getJsonArray("banner");
JsonObject temp;
for(int i=0;i<jsonBanner.length;i++){
temp = jsonBanner.getJsonObject(i);
String image = temp.optString("image");
}
}
Related
I'm trying to read a JSONObject from within another JSONObject. The first output works, the second, i.e. the JSONObject within, does not. Here I get the following error
org.json.JSONException: JSONObject["Values"] not found.
This is the output of the first object / array
ergebnis: {"Description":"","Objects":[{"RefStr":"76-1044-0","ClassName":"Application","Values":{"providerorg-refstr":"262-462-0",...}]}
This is how I try to get the data:
JSONObject jsonObject = new JSONObject(ergebnis);
System.out.println(jsonObject);
JSONObject inside = jsonObject.getJSONObject("Values");
System.out.println(inside);
Values object is present in Objects. This objects is an array. You can try like below,
JSONObject jsonObject = new JSONObject(json);
System.out.println(jsonObject);
JSONObject jsonObject1= jsonObject.getJSONObject("ergebnis");
System.out.println(jsonObject1);
JSONArray objects = jsonObject1.getJSONArray("Objects");
System.out.println(objects);
JSONObject inside = objects.getJSONObject(0).getJSONObject("Values");
System.out.println(inside);
I need to create variable amount of JSON objects and based on the result set from a database query. States retrieving from mysql database. I am saving the response in JSON array but it is saving in multiple JSON objects like that:
jString userid = rs2.getString("state");
JSONObject mainObj = new JSONObject();
JSONArray jsArray = new JSONArray();
jsArray.put(userid );
mainObj.put("states", jsArray);
With this output:
{"states":["karnataka"]}
{"states":["kerala"]}
{"states":["chennai"]}
Can I get output in single JSON object like {"States":"karnataka","kerala","chennai"}?
You probably just got some code structure confused.
Try something like this:
JSONArray jsArray = new JSONArray();
JSONObject mainObj = new JSONObject();
//some kind of loop
{
jString userid = rs2.getString("state");
jsArray.put(userid);
}
mainObj.put("states", jsArray);
I have the following json structure
{
"MerchHierarchyEBM":{
"DataArea":{
"Division":{
"UpdatedBy":"SN",
"Group":{
"GroupName":"Womens Fashion*",
"UpdatedBy":"Data Migration",
"UpdatedOn":"22-NOV-17",
"GroupID":"200"
},
"DivisionName":"Fashion",
"UpdatedOn":"22-NOV-17",
"DivisionID":"2000"
}
}
}
}
and i want to remove the "Group" key and value from the json object using java
i tried few things but didn't work following is my code .
JSONObject jsonObjIncomingDatanew =new JSONObject(Result);
jsonObjIncomingDatanew.remove("MerchHierarchyEBM.DataArea.Division.Group");
Try this:
JSONObject jsonObject = new JSONObject(Result);
jsonObject
.getJSONObject("MerchHierarchyEBM")
.getJSONObject("DataArea")
.getJSONObject("Division")
.remove("Group");
Or if getJSONObject() doesn't work, replace it with getAsJsonObject().
My problem is simple, I'd like to add a JSONObject to a JSONArray that I store in a MongoDB database. I can easily add all types of data like Strings, Ints etc but not JSONObjects.
I do this in my code :
public void done(ParseObject lan, ParseException e) {
if(e==null){
JSONObject object = new JSONObject();
try{
object.put("PlayerName","John");
object.put("ID",514145);
lan.add("Participants",object); //nothing gets inserted
lan.add("Participants",15); //works fine
lan.add("Participants",JSONObject.null); //works fine too
}catch (JSONException error){
}
lan.increment("Number");
lan.saveInBackground();
}else{
Log.i("Parse Error","error");
}
}
But nothing appears in my db and there's no error thrown.
Do you guys have any clue on how to do that ?
Try using object.toString() instead of object.
lan.add("Participants", object.toString());
JSON:
{"Participants":["{\"PlayerName\":\"John\",\"ID\":514145}"]}
To parse this JSON try this:
JSONObject jsonObj = new JSONObject(YOUR_JSON_STRING);
// Participants
JSONArray participantsJsonArray = jsonObj.getJSONArray("Participants");
// Participant
JSONObject participanJsonObject = participantsJsonArray.getJSONObject(0);
// PlayerName
String playerName = participanJsonObject.getString("PlayerName");
// ID
String id = participanJsonObject.getInt("ID");
Hope this will help~
Convert that Json Object into String and store it in string format
lan.add("Participants",object.toString());
And when you want to use that you can easily convert it into Json Object again like this
JSONObject jObj=new JSONObject("Your Json String");
A very simple JSON like this (response.getBody().toString()):
{"per_page":50,"total":93,"last_page":2,"stars":[]}
Has some problems when I want to parse it:
JSONObject object = new JSONObject(response.getBody()); // no error
System.out.println(object.getJSONObject("total")); // not found
org.json.JSONException: JSONObject["total"] not found.
Other properties cannot be parsed either:
JSONArray startups = object.getJSONArray("stars");
org.json.JSONException: JSONObject["stars"] not found.
The key is to hold the value of response.getBody()
String json = response.getBody().toString();
Inside object total is not JSONObject it is an Int value, that's why you code crashing.
So use this
System.out.println(String.valueOf(object.getInt("total")));
instead of
System.out.println(object.getJSONObject("total"));
String json = "{\"per_page\":50,\"total\":93,\"last_page\":2,\"stars\":[]}":
JSONObject jsonObject = new JSONObject (json);
JSONArray jsonArray = jsonObect.getJSONArray("stars");
int perPage = jsonObject.getInt("per_page");
try like this...
In addition to earlier answer, to parse array, use getJSONArray as
JSONArray ja = jsonObj.getJSONArray("stars");
You can loop-over the array as:
for(int j=0; j<ja.length(); j++) {
JSONObject json = ja.getJSONObject(j);
// do same as before for string, int or other data types
}