parse json response in android parse json object details with json array - android

This is my json response.
"hits" : [ {
"recipe" : {
"uri" : "http://www.edamam.com/ontologies/edamam.owl#recipe_ef498d1ca2ecb7104aa72d516af211d8",
"label" : "A Potato Dish for Julia Recipe",
"image" : "https://www.edamam.com/web-img/9bd/9bd993aeafaf4a439a59b5689ea6153f.jpg",
i need to get this as my list view response, i tried to add this by many ways but still i am getting blank screen as respose.
JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray hits = jsonObj.getJSONArray("hits");
for (int i = 0; i < hits.length(); i++) {
JSONObject h = hits.getJSONObject(i);
JSONObject recipe = h.getJSONObject("recipe");
String Uri = recipe.getString("uri");
String Label = recipe.getString("label");
String Image = recipe.getString("image");
HashMap<String, String> recipe_ = new HashMap<>();
recipe_.put("uri", Uri);
recipe_.put("label", Label);
recipe_.put("image", Image);
recipeList.add(recipe_);
This is my solution what i was finding to call json object details by first calling json array then its object.

Related

How can I put JSON return in an array?

I'm a android beginner and I'm doing to access a JSON file in and it has an error. I have a problem in parsing this
JSONObject jsonObject = new JSONObject(jsonStr);
JSONArray accounts = jsonObject.getJSONArray("account_data");
for(int i=0;i < accounts.length();i++){
JSONObject a = accounts.getJSONObject(i);
sin = a.getString("sin");
account_name = a.getString("account_name");
address = a.getString("address");
status = a.getString("status");
due_date = a.getString("due_date");
total_amount = a.getDouble("total_amount");
sin_lbl.setText(a.getString("account_name"));
}
here is the JSON File
{"account_data":{
"sin":"200111-102 ",
"account_name":"LUMABAN, CRISTOM ",
"address":"352 MABINI ST.,, SABANG, Baliwag ",
"status":"A ",
"due_date":"2019-04-23",
"total_amount":"491.00"
},"code":1101,"message":"Account Info Retrieved"}
I have an error in putting it in array.
Instead of using JSONArray , try to use JSONObject.
String[] array = {json.get("sin"), json.get("account_name"), json.get("address"), json.get("status"), json.get("due_date"), json.get("total_amount") }
{"account_data":{"sin":"200111-102 ","account_name":"LUMABAN, CRISTOM ","address":"352 MABINI ST.,, SABANG, Baliwag ","status":"A ","due_date":"2019-04-23","total_amount":"491.00"},"code":1101,"message":"Account Info Retrieved"}
Actually, it's a json object, not array. So that you can not convert json object to json array
Difference between Json Array and Json Object:
A JSONArray is an ordered sequence of values. A JSONObject is an unordered collection of name/value pairs.
JSONArray: Its external text form is a string wrapped in square brackets with commas separating the values.
JSONObject: Its external form is a string wrapped in curly braces with colons between the names and values, and commas between the values and names.
Please use this json parshing
try {
JSONObject jsonObject = new JSONObject(jsonStr);
JSONObject accounts = jsonObject.getJSONObject("account_data");
sin = accounts.getString("sin");
account_name = accounts.getString("account_name");
address = accounts.getString("address");
status = accounts.getString("status");
due_date = accounts.getString("due_date");
total_amount = accounts.getDouble("total_amount");
sin_lbl.setText(a.getString("account_name"));
} catch (Exception e) {
}
if you asked about iterating on json object you could try this one
JSONObject jObject = new JSONObject(jsonStr);
JSONObject menu = jObject.getJSONObject("account_data");
Map<String,String> map = new HashMap<String,String>();
Iterator iter = menu.keys();
while(iter.hasNext()){
String key = (String)iter.next();
String value = menu.getString(key);
map.put(key,value);
}
so now you have your data into as pair of key and value
if you have a json array of this response you could do as following
JSONObject root = new JSONObject("your root");
JSONArray resultArray = root.getJSONArray("your array key");
for (int i = 0; i < resultArray.length(); i++) {
// here to get json object one by one and access every item into it
JSONObject resultObject = resultArray.getJSONObject(i);
posterPath = resultObject.getString("key");
title = resultObject.getString("key");
releaseDate = resultObject.getString("key");
description = resultObject.getString("key");
voteAverage = resultObject.getDouble("key");
}

Cannot convert to JSONObject after updating array

I used to have a response string that I converted to JSONObject in this way:
JSONObject obj_temp = new JSONObject(response);
Then, I added to that string some modifications. At first, the index 'TripDetails' just had 1 trip with it details (trip information, passenger information and driver information). Now, with the new modifications, 'TripDetails' is an array of trips. Each index has the same information for each trip (trip information, passenger information and driver information). But now, with this new string, Android Studio gives me this error:
of type org.json.JSONArray cannot be converted to JSONObject
Can a string be casted to JSONObject if it has this format? It's a valid JSON string. Here's the complete JSON: https://paste2.org/VemkLNMJ
It is because you made your json object an array (I don't see why since you have just 1 element). Anyway you can probably do something like this:
JSONArray arr = new JSONArray(response);
JSONObject obj_temp = arr.getJSONObject(0);
your response starts with Array and you trying to convert it into Object.
[ represents json array node
{ represents json object node
JSONArray obj_temp = new JSONArray(response);
for(int i=0; i < jsonarray.length(); i++) {
JSONObject jsonobject = obj_temp.getJSONObject(i);
String id = jsonobject.getString("id");
String title = jsonobject.getString("title");
String company = jsonobject.getString("company");
String category = jsonobject.getString("category");}

Filter array by index and save it to another array

I´m trying to filter all values in name where id = 1 and save to an array.
This is the output I get from my WS:
[{id=1; name=Des Moines; }, {id=2; name=Cedar Rapids; }, {id=3; name=Yakima; },{id=4; name=Fort Dodge; }, {id=1; name=Iowa City; }]
if I try get property it splits my content.
Could anyone help me out on how to get the desire output?
That's JSON data, you should consider parse the data into an object , there are a lot of JSON java parsers , you could read something about GSON:
https://github.com/google/gson/blob/master/UserGuide.md
You can try something like the following
your json string must be like:
["items":{id=1; name=Des Moines; }, {id=2; name=Cedar Rapids; }, {id=3; name=Yakima; },{id=4; name=Fort Dodge; }, {id=1; name=Iowa City; }]
JSONObject jsonObject = null;
jsonObject = new JSONObject("your json string goes here!!");
String items = jsonObject.getString("items");
JSONArray array = new JSONArray(weather);
ArrayList<JSONObject > arrList = new ArrayList<JSONObject >();
for(int i = 0; i < array.length(); i++)
{
JSONObject jsonObject1 = array.getJSONObject(i);
arrList.add(jsonObject1 );
String id = jsonObject1.get("id").toString();
String name = jsonObject1.get("name").toString();
}

How to get a value from JSON Object by a key?

How to get the value by key in a JSON object? I had used following code but it receives "org.json.JSONException". Advance thanks for any help.
String resultJSON = "{Data:[{\"AreaID\":\"13\", \"Phone\":\"654321\", \"RegionName\":\"Sivakasi\"}, {\"AreaID\":\"14\", \"Phone\":\"12345\", \"RegionName\":\"ANJAC\"}]}";
JSONObject jObject = new JSONObject(resultJSON);
JSONObject jsonObject = jObject.getJSONObject("Data");
Map<String,String> map = new HashMap<String,String>();
Iterator iter = jsonObject.keys();
while(iter.hasNext()){
String key = (String)iter.next();
String value = jsonObject.getString(key);
map.put(key,value);
Log.d("Key Value","key: "+key+" Value: "+value);
}
Logcat details
org.json.JSONException: Value [{"AreaID":"13","Phone":"654321","RegionName":"Sivakasi"},{"AreaID":"14","Phone":"12345","RegionName":"ANJAC"}] at Data of type org.json.JSONArray cannot be converted to JSONObject
The structure of your JSON is wrong, you should use a Key for the second JSONObject , like this:
{
Data: {
\"AreaID\": \"13\",
\"Phone\": \"654321\",
\"RegionName\": \"Sivakasi\"
},
\"KEY\": {
\"AreaID\": \"14\",
\"Phone\": \"12345\",
\"RegionName\": \"ANJAC\"
}
}
Or the DATA should be a JSONArray ( surrounded by [] ) like this :
{
Data: [
{
\"AreaID\": \"13\",
\"Phone\": \"654321\",
\"RegionName\": \"Sivakasi\"
},
{
\"AreaID\": \"14\",
\"Phone\": \"12345\",
\"RegionName\": \"ANJAC\"
}
]
}
NOTE : you can check if your json is valid or not here
Personnaly , i prefer the second way ( Using JSONArray) , because the data inside has the same attributes (AreaID, Phone, REgionName). To parse data in this case , your code should be someting like this :
String resultJSON = "{Data:[{\"AreaID\":\"13\", \"Phone\":\"654321\", \"RegionName\":\"Sivakasi\"}, {\"AreaID\":\"14\", \"Phone\":\"12345\", \"RegionName\":\"ANJAC\"}]}";
JSONObject jsonRoot = new JSONObject(resultJSON);
JSONArray jsonData = jsonRoot.getJSONArray("Data");
for(int i=0; i<jsonData.lenght;i++) {
JSONObject jsonOBject = jsonData.getJSONObject(i);
Log.d(TAG, "json ("+i+") = "+jsonOBject.toString());
// do what you want with your JSONObject , i.e :add it to an ArrayList of paresed result
String areaID = jsonOBject.getString("AreaID");
int phoneNumber = jsonOBject.getInt("Phone");
String regionName = jsonOBject.getString("RegionName");
}
This is invalid JSON format. Before you convert your string to JSON object format, be sure about it's valid or not.
Please check validity of your JSON.
Hope it may help.

Json error org.json.JSONException: No value for

it gives me the error log : " org.json.JSONException: No value for tableData"
try {
JSONObject mainJson22 = new JSONObject(reply);
JSONObject jsonResult = mainJson22.getJSONObject("UserListByBusinessAreaContextResult");
JSONArray jsonArray22 = jsonResult.getJSONArray("tableData");
// JSONArray jsonArray22 = mainJson22.getJSONArray("UserListByBusinessAreaContextResult"); // JSONArray jsonArray22 = jsonResult.getJSONArray("tableData"); Log.i("mainjson234","" + jsonArray22);
for (int i2 = 0; i2 < jsonArray22.length(); i2++) {
JSONObject objJson22 = jsonArray22.getJSONObject(i2);
the error is on line : `"JSONArray jsonArray22 = jsonResult.getJSONArray("tableData");"
heres the json:
{
"UserListByBusinessAreaContextResult": "{\"tableData\":[{\"UserID\":30,\"Username\":\"Teste\",\"Name\":\"Teste\"}]}"
}
The object UserListByBusinessAreaContextResult contains a string. This string is:
"{\"tableData\":[{\"UserID\":30,\"Username\":\"Teste\",\"Name\":\"Teste\"}]}"
Do you notice the quotes? The problem is, that your WCF service returns a JSON string, not an object. So either you will have to change the WCF service to deliver an object instead, or parse that string again. Like this (not tested):
JSONObject mainJson22 = new JSONObject(reply);
JSONObject tableData = new JSONObject(
mainJson22.getString("UserListByBusinessAreaContextResult"));

Categories

Resources