Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a raw data as in a below format:
[
{
"id": "1",
"name": "abc",
"type": "consumer"
},
{
"id": "2",
"name": "cdf",
"type": "consumer"
},
{
"id": "3",
"name": "jok",
"type": "owner"
}
]
Please let me know how can I covert that into JsonArray and get the each values.
This is the simplest way to parse your JSON String. I would suggest your to read JSON documents.
But, here is a sample code.
try {
String jsonString = new String("[{\"id\": \"1\",\"name\": \"abc\",\"type\": \"consumer\"}]");
JSONArray jsonArray = new JSONArray(jsonString);
for(int index = 0;index < jsonArray.length(); index++) {
JSONObject jsonObject = jsonArray.getJSONObject(index);
String id = jsonObject.getString("id");
String name = jsonObject.getString("name");
String type = jsonObject.getString("type");
}
} catch (JSONException e) {
e.printStackTrace();
}
You can also parse using GSON https://code.google.com/p/google-gson/
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How i can parse 2 json Array in Android?
Plz see the following code;
{
"detail": [
{
"price": 51,
"numsc": 2,
"name": "this app is about animals",
"sc1": "printed-dress.jpg",
"sc2": "printed-dress2.jpg"
}
],
"colors": [
{
"color": "#5D9CEC",
"name": "blue"
},
{
"color": "#FCCACD",
"name": "pink"
}
]
}
can you help me plz??
JSONObject object = new JSONObject(your-string);
JSONArray details=object.getJSONArray("details");
for(int j=0;j<details.length();j++){
JSONObject detail= details.getJSONObject(i);
String price = detail.getString("price");
....
}
JSONArray colors = object.getJSONArray("colors");
for(int i=0;i<colors.length();i++){
JSONObject obj= colors.getJSONObject(i);
// parse your json here
String color = obj.getString("color")
}
See following code:
private void decodeJSON()
{
String JSONString = "you json String";
try
{
JSONObject obj = new JSONObject(JSONString);
JSONArray arr = obj.getJSONArray("detail");
JSONObject detail = arr.getJSONObject(0);
int price = detail.getInt("price"); // do same thing to get other values
arr = obj.getJSONArray("colors");
JSONObject color = arr.getJSONObject(0);
String colorValue = color.getString("color");
String name = color.getString("name");
// do same thing for next object in array.
} catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I recently started working with andorid.
How to parse dynamic json with multiple arrays response from php server.
Login Response
{
"status": 1,
"data": {
"msg": "LoggedIn",
"user_id": "2"
}
}
Login response with error message
{
"status": 0,
"data": "No users found with given email."
}
and an other one
Inventory List
{
"status": 1,
"data": [
{
"inventory_id": "33",
"apron_id": "123456",
"nickname": "uyi",
"location": "13",
"manufacture": "0",
"garment_type": "yuyh",
"color": "juki",
"core_material": "ytyt",
"Date_purchase": "2015-04-10",
"UID_no": "ikujki",
"serial": "iui",
"Batch": "ikk",
"Expiration": "2015-04-23",
"QTY": "898",
"apron_status": "0",
"apron_retire": "0",
"created_user": "2",
"created_time": "2015-04-10 05:22:38",
"update_time": "2015-04-10 05:22:38"
},
{
"inventory_id": "32",
"apron_id": "12345mn",
"nickname": "gfhgh",
"location": "12",
"manufacture": "0",
"garment_type": "hgjyhj",
"color": "ytgtfghtg",
"core_material": "fhgfhy",
"Date_purchase": "2015-04-28",
"UID_no": "rtryttttttttt",
"serial": "hfh",
"Batch": "rtrrtyy",
"Expiration": "2015-03-17",
"QTY": "7688",
"apron_status": "0",
"apron_retire": "0",
"created_user": "2",
"created_time": "2015-04-10 05:15:54",
"update_time": "2015-04-10 05:15:54"
}
]
}
Thanks in advance.
to get login response you can do like this
JSONObject jobj=new JSONObject(result.toString());
String status=jobj.getString("status");
if(status.equalsIgnoreCase("1"))
{
//login success
JSONObject Jdata=jobj.getJSONObject("data");
String Message=Jdata.getString("msg");
String UserId=Jdata.getString("user_id");
}
else
{
//failure
}
and for Inventory List you can do like this
JSONObject jobj=new JSONObject(result.toString());
JSONArray arrData=jobj.getJSONArray("data");
for (int i = 0; i < arrData.length(); i++)
{
JSONObject jdata=arrData.getJSONObject(i);
//here u can get all field like this
String nickname=jdata.getString("nickname");
}
Investigate the JSONObject and JSONArray classes.
You can parse extremely simply:
JSONObject json = new JSONObject(jsonString);
int status = json.getInt("status");
Using both an Array and an Object for your "data" key will make things irritating for you. You should think about using a different key, or another key that will dictate to you what you are reading.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to know how to extract data from this list.
Response
[
{
"CCTID": "46204",
"Name": "Christopher Columbus",
"CarrierId": 64239
},
{
"CCTID": "46208",
"Name": "Keith Bowles",
"CarrierId": 64239
},
{
"CCTID": "46205",
"Name": "Michael Jordan",
"CarrierId": 64239
},
{
"CCTID": "46207",
"Name": "NESV PH",
"CarrierId": 64239
}
]
How can I group this data to per driver and access the following data like a list. For example. List of drivers. Driver[0]["CCTID"] returns "46204", Driver[0]["Name"] returns "Driver[0]["CCTID"] returns "46204" Driver[0]["CarrierId"] returns "64239" so on and so forth. Any ideas? Thanks!
Try this way
try {
JSONArray arr = new JSONArray(EntityUtils.toString(response.getEntity()));
for (int i = 0; i < arr.length(); i++) {
JSONObject driver = arr.getJSONObject(i);
System.out.println("CCTID : " + driver.getString("CCTID"));
System.out.println("Name : " + driver.getString("Name"));
System.out.println("CarrierId : " + driver.getString("CarrierId"));
}
} catch (Exception e) {
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I want to parse the following JSON response. I couldn't extract the JSONArray which is inside the JSON object. I'm a novice to JSON parsing, any help would be appreciated.
{
"Result": {
"Data": [
{
"id": "1",
"Name": "ABC",
"release": "8",
"cover_image": "august.png",
"book_path": "Aug.pdf",
"magazine_id": "1",
"Publisher": "XYZ",
"Language": "Astrological Magazine",
"Country": "XYZ"
},
{
"id": "2",
"Name": "CDE",
"release": "8",
"cover_image": "august2012.png",
"book_path": "aug.pdf",
"magazine_id": "2",
"Publisher": "XYZ",
"Language": "Astrological Magizine",
"Country": "XYZ"
}
]
}
}
Basic code for implementing JSON Parsing is like:
JsonObject objJSON = new JSONObject("YourJSONString");
JSONObject objMain = objJSON.getJSONObject("NameOfTheObject");
JSONArray objArray = objMain.getJSONArray("NameOfTheArray"); // Fetching array from the object
Update:
Based on your comment, i can see you haven't fetched JSONArray "Data", without it you are trying to fetch values/attributes of a particular object:
JSONObject jObj = jsonObj.getJSONfromURL(category_url);
JSONObject menuObject = jObj.getJSONObject("Result"); String attributeId = menuObject.getString("Data");
String attributeId = menuObject.getString("Data"); // Wrong code
JSONArray objArray = menuObject.getJSONArray("Data"); // Right code
I like to use the GSON library: http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html
It's a JSON-parsing library by Google.
OK, step by step:
String json = "{\"Result\":{\"Data\":[{\"id\":\"1\",\"Name\":\"ABC\",\"release\":\"8\",\"cover_image\":\"august.png\",\"book_path\":\"Aug.pdf\",\"magazine_id\":\"1\",\"Publisher\":\"XYZ\",\"Language\":\"Astrological Magazine\",\"Country\":\"XYZ\"},{\"id\":\"2\",\"Name\":\"CDE\",\"release\":\"8\",\"cover_image\":\"august2012.png\",\"book_path\":\"aug.pdf\",\"magazine_id\":\"2\",\"Publisher\":\"XYZ\",\"Language\":\"Astrological Magizine\",\"Country\":\"XYZ\"}]}}";
try
{
JSONObject o = new JSONObject(json);
JSONObject result = o.getJSONObject("Result");
JSONArray data = result.getJSONArray("Data");
for (int i = 0; i < data.length(); i++)
{
JSONObject entry = data.getJSONObject(i);
String name = entry.getString("Name");
Log.d("name key", name);
}
}
catch (JSONException e)
{
e.printStackTrace();
}
Json is hardcoded, so I had to escape it.
This code gets result object and then data array. A loop goes through an array and gets a value of Name.
I got in LogCat:
ABC
CDE
Note that you should surround it with try-catch or add throws to a method.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JSON Array iteration in Android/Java
I am fetching JSON string from server and I have already got JSON string by code.
But I didn't understand how to parse it.
Below is my JSON string
{
"university": {
"name": "oxford",
"url": "http://www.youtube.com"
},
"1": {
"id": "2",
"title": "Baseball",
"datetime": "2011-11-11 10:41:46"
},
"2": {
"id": "1",
"title": "Two basketball team players earn all state honors",
"datetime": "2011-11-11 10:40:57"
}
}
Please provide any guidance or code snippet.
Use JSON classes for parsing e.g
JSONObject mainObject = new JSONObject(Your_Sring_data);
JSONObject uniObject = mainObject.getJSONObject("university");
String uniName = uniObject.getString("name");
String uniURL = uniObject.getString("url");
JSONObject oneObject = mainObject.getJSONObject("1");
String id = oneObject.getString("id");
....
Below is the link which guide in parsing JSON string in android.
http://www.ibm.com/developerworks/xml/library/x-andbene1/?S_TACT=105AGY82&S_CMP=MAVE
Also according to your json string code snippet must be something like this:-
JSONObject mainObject = new JSONObject(yourstring);
JSONObject universityObject = mainObject.getJSONObject("university");
JSONString name = universityObject.getString("name");
JSONString url = universityObject.getString("url");
Following is the API reference for JSOnObject: https://developer.android.com/reference/org/json/JSONObject.html#getString(java.lang.String)
Same for other object.