Extract data from response entity [closed] - android

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) {
}

Related

Android: Parse 2 jsonArray [closed]

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();
}
}

How to read Json data in android [closed]

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/

How to extract values from json array [closed]

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
Hi i am new to the android development,i want to extract values from json array can you please guide me.
Here is my json
[
{
"Id": "c0f3310b-5ec2-4af0",
"UserId": "fd83ca17-41f5-472a",
"ProfileId": "100006690",
"ProfileType": "facebook",
"ProfileDate": "/Date(1380894956000)/",
"ProfileStatus": 1
},
{
"Id": "6954433d-b78e-47b6",
"UserId": "fd83ca17-41f5-8efe",
"ProfileId": "100004492",
"ProfileDate": "/Date(1380894685000)/",
"ProfileStatus": 1,
"ProfileType": "facebook"
}
]
Thank you
Like below coding
JSONArray jObject = new JSONArray(jsoninputstring);
for (int i = 0; i < jObject.length(); i++) {
JSONObject obj = jObject.getJSONObject(i);
String name= obj.getString("Id");
String email= obj.getString("UserId");
String image= obj.getString("ProfileId");
}
Here is the tutorial for JSON parsing
http://lakyrana.blogspot.in/
http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
http://androidexample.com/JSON_Parsing_-_Android_Example/index.php?view=article_discription&aid=71&aaid=95
Create new JSONArray object and iterate over it.
JSONArray arr = new JSONArray(jsonString);
for(int i=0;i<arr.length;i++){
JSONObject obj = arr.getJSONObject(i);
// read data from obj using obj.getString method.
}
JSONArray categories = responseData.getJSONArray("categories"); // your JSON array
for(int i=0; i < categories.length(); i++){
JSONObject ob = (JSONObject) categories.get(i);
ob.getString("Id");
ob.getString("UserId"); // and so on
}
I would strongly suggest you to check out JacksonParser... You can download jar files from this link and you can easily find so much example how to use it. It is the easiest and fastest way to parse json into object.
JSONArray jsonArray = new JSONArray(yourResponseString);
for(int i=0;i<jsonArray.length();i++){
JSONObject dataObject=dataArray.getJSONObject(i);
String ID=dataObject.getString("Id");
String UserID=dataObject.getString("UserId");
String ProfileID = jsonObject.getString("ProfileId");
..
}

how to parsing Json Array in android? [closed]

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
How to parse the below json.
[
{
"products": [
{
"description": "USB2.0. 1000 dpi, Double Lens Technology. Black
(Red,Orange.Green.Grey),White(Red,Orange.Green,Grey)
<\/p>",
"product_id": "36",
"name": "New
MP-770",
"product_code": "MO018",
"images": "productimage\/36",
"price": "59900",
"weight": "1.00"
}
],
"num_page": 1
}
]
JSONArray json = jParser.getJSONFromUrl(URL);
try {
JSONObject c = json.getJSONObject(0);
JSONArray json1 = c.getJSONArray("products");
status = c.getString("num_page");
for(int i=0;i<json1.length();i++){
JSONObject c1 = json1.getJSONObject(i);
String des = c1.getString("description");
String pid = c1.getString("product_id");
String name = c1.getString("name");
String pc = c1.getString("product_code");
String images = c1.getString("images");
String price = c1.getString("price");
String weight = c1.getString("weight");
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put("description", des);
map.put("product_id", pid);
map.put("name", name);
map.put("product_code",pc);
map.put("images", images);
map.put("price", price);
map.put("weight", weight);
fetch.add(map);
}
copy paste the code and use it, fetch is your arraylist of hashmap and if any problem persist then let me know.
You can do it with Android Build in JsonObject/JsonArray or you can use a library from Google called GSON
Have a look at these two Tutorials: GOOGLE and this ONE
But please use google the next time, there are thousands of tutorials explaining this to you

Unable to parse JSON in Android [closed]

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.

Categories

Resources