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.
Related
I am a beginner in android coding. I am working on an app which gets data from php web service in JSON format, but I am not able to correctly parse it.
JSON returned looks like this:
{
"posts": [
{
"post": {
"Id": "1",
"Title": "Captain America",
"Lang": "ENG"
}
}
]
}
Android code:
JSONObject job = new JSONObject(json);//json is the string returned by web service
jObj = job.getJSONArray("posts");
JSONObject c = jObj.getJSONObject(0);
String title = c.getString("Title");
But I get a JSON exception:No value for Title
I cant figure out whats going wrong.
You have
{ //JSONObject job = new JSONObject(json); ok
"posts": [ // jObj = job.getJSONArray("posts"); ok
{ // JSONObject c = jObj.getJSONObject(0) ok
"post": { // forgot about jsonobject post // missed JSONObject post = c.getJSONObject("post")
"Id": "1",
"Title": "Captain America",
Change to
JSONObject c = jObj.getJSONObject(0);
JSONObject post = c.getJSONObject("post");
String title = post.getString("Title");
JSONObject job = new JSONObject(json);//json is the string returned by web service
JSONArray jar=job.getJSONArray("posts");
for (int i=0;i<jar.length();i++)
{
job=jar.getJSONObject(i);
job=jat.getJSONObject("post");
title=job.optString("Title");
}
You forget to get "post" value
I am sending request to the server and it gives me a response and giving data in JSON formats, now i want to fetch some specific value from that JSON format, so hot to do it.
{
"education": [
{
"school": {
"id": "2009305",
"name": "FG boys public high school Bannu Cantt "
},
"type": "High School"
},
{
"school": {
"id": "109989",
"name": "University of Engineering & Technology"
},
"type": "College"
}
],
"id": "xxxxxxx"
}
Now i need the school names from this xml,
Try this..
You response is in Json format not xml.
JSONObject json = new JSONObject(response);
JSONArray education = json.getJSONArray("education");
for(int i = 0; i < education.length(); i++){
JSONObject con_json = education.getJSONObject(i);
String school_type = con_json.getString("type");
JSONObject school_json = con_json.getJSONObject("school");
String school_name = school_json.getString("name");
}
It is not XML. it is completely in Json Standard format.
first build a JSONobject from your data:
JSONObject jsonObj = new JSONObject(result); //result = your Data String in fetched from server
then you cab retrieve what you want using its key. for example:
jsonObj.getString("id"); // it returns "xxxxxxx". as is in your data
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/
I have JSON object as follows:
[
{
"Project": {
"id": "1",
"project_name": "name"
},
"AllocationDetail": [
{
"id": "1",
"project_id": "1",
"team_name": "ror",
"week_percentage_work": "50",
"in_today": "1",
"actual_hours": "30",
"remaining_hours": "100",
"time_difference": null,
"created": "2012-01-13 15:48:33",
"modified": "2012-01-13 15:48:33"
},
{
"id": "2",
"project_id": "1",
"team_name": "php",
"week_percentage_work": "40",
"in_today": "2",
"actual_hours": "50",
"remaining_hours": "100",
"time_difference": null,
"created": "2012-01-13 15:49:40",
"modified": "2012-01-13 15:49:40"
}
]
}
]
I want to parse data in android and store it into DB, but i m getting confused in Jsonobject
thanks
the best JSON Tutorial i have seen, I leared json parsing from this tutorial, hope it will help
The following is a snippet code for parsing your json string. Kindly go through it:
String response = <Your JSON String>;
String Project = null;
String AllocationDetail = null;
try {
JSONArray menuObject = new JSONArray(response);
for (int i = 0; i< menuObject.length(); i++) {
Project = menuObject.getJSONObject(i).getString("Project").toString();
System.out.println("Project="+Project);
AllocationDetail = menuObject.getJSONObject(i).getString("AllocationDetail").toString();
System.out.println("AllocationDetail="+AllocationDetail);
}
JSONObject jsonObject = new JSONObject(Project);
String id = jsonObject.getString("id");
System.out.println("id="+id);
String project_name = jsonObject.getString("project_name");
System.out.println("project_name="+project_name);
JSONArray jArray = new JSONArray(AllocationDetail);
for (int i = 0; i< jArray.length(); i++) {
String team_name = jArray.getJSONObject(i).getString("team_name").toString();
System.out.println("team_name="+team_name);
String week_percentage_work = jArray.getJSONObject(i).getString("week_percentage_work").toString();
System.out.println("week_percentage_work="+week_percentage_work);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
There are two ways for parsing JSON
Google GSON
JSON
When your content of JSON is too complex and long enough you can prefer usin GSON its much easier to maintain than JSON parsing each value manually.
Use jsonlint.com to read your json better. It appears that the json that you have copied here is invalid.
String response = <Your JSON String>; // add your Json String here
response = response.replace("[","").replace("]",""); // Just remove all square braces
JSONObject json = new JSONObject(response); //Convert the Json into Json Object
JSONObject jProject = json.getJSONObject("Project"); // get your project object
String project_id = jProject.getString("id"); // Get the value of Id
String project_name = jProject.getString("project_name"); // get the value of Project_name
The above code can be use many times as you want to parse the Json.
I know its too late, but It may help many people who are trying to find the same answer.
I was trying to find the same solution, but it was too hard to use the Accepted answer because of too many lines of codes, But I got the same results with hardly few lines of codes.
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.