How to extract values from json array [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
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");
..
}

Related

How can i parse this json file in android studio? [duplicate]

This question already has answers here:
Android Parse JSON Array
(3 answers)
Closed 6 years ago.
how can i parse this json file because the text do not have any particular indexing names
{
"titles": [
" Thoughts On The Works Of Providence",
"\"All Is Vanity, Saith the Preacher\"",
"\"And the sins of the fathers shall be\"",
"\"Arcturus\" is his other name",
"\"By the Waters of Babylon.\"",
"\"De Gustibus--\"",
"\"Faith\" is a fine invention",
"\"Faithful to the end\" Amended",
"\"Heaven\" -- is what I cannot reach!",
"\"Heaven\" has different Signs -- to me --",
"\"Heavenly Father\" -- take to thee",
"\"Home\"",
]
}
Like this:
JSONArray array = object.getJSONArray("titles");
for(int i = 0; i < array.length(); i ++){
String title = array.getString(i);
//Do something with the string
}
You treat the JsonArray like a string array.
However in the array you provided the JSON is invalid because of the comma on the last element of the array, you need to remove this comma to have valid JSON.
It's just a simple JSON. Do it like this.
String json = "{\"titles\":[\" Thoughts On The Works Of Providence\",\"\\\"All Is Vanity, Saith the Preacher\\\"\",\"\\\"And the sins of the fathers shall be\\\"\",\"\\\"Arcturus\\\" is his other name\",\"\\\"By the Waters of Babylon.\\\"\",\"\\\"De Gustibus--\\\"\",\"\\\"Faith\\\" is a fine invention\",\"\\\"Faithful to the end\\\" Amended\",\"\\\"Heaven\\\" -- is what I cannot reach!\",\"\\\"Heaven\\\" has different Signs -- to me --\",\"\\\"Heavenly Father\\\" -- take to thee\",\"\\\"Home\\\"\"]}";
JSONObject jsonObject = new JSONObject(json);
And for accessing your items:
JSONArray jsonArray = jsonObject.getJSONArray("titles");
for(int i=0;i<jsonArray.size(); i++){
Log.d(TAG, jsonArray.getString(i));
}
You can parse this to an instanse of this class :
public class Instance{
public String[] titles;
}
but you need to remove the last comma after home. If there is no another item after the comma, it means it is not a valid JSON string.
This should work.
JSONObject jsonObject = new JSONObject(yourString);
JSONArray jsonArray= jsonObject.getJSONArray("titles");
for (int i=0; i < jsonArray.length(); i++) {
string content = jsonArray.getString(i);
....
}
Try this:
Try{
JSONArray itemArray = jsonObject.getJSONArray("titles")
for (int i = 0; i < itemArray.length(); i++) {
String value=itemArray.getString(i);
Log.e("json", i+"="+value);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

How to parse Multiple json array in android?

This may be possible of duplicate question but am struggling with this am getting json array response like this:
[{"data":"25"},{"MobID":"88"}]
JsonArray jsonarray=new JsonArray(serverresponse);
for(int i=0;i<jsonarray.length();i++){
JsonObject json=new JsonObject(i);
String data=json.getInt("data");
String Mobid=jsong.getInt("MobID");
}
}
Is it possible to parse this type of json i haven't found any parsing method for this above method as a beginner am struggling with this you people are here to help beginner like this Thanks in advance!!!
Try this:
JSONArray array = new JSONArray(serverResponse);
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
int data = object.getInt("data");
int mobid = object.getInt("MobID");
// use them ...
}
Hint: the secret (sh!) is to read the javadocs! All of the methods you needed are in the JSONArray and JSONObject javadocs.
Note: I corrected a number of style errors in your code (and some bugs). Please compare your version and mine to see what I fixed.

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

How to Extract json array within an array in android

I am trying to pass some json encoded data from cakephp to android, I have a problem when passing the data
I use
echo json_encode($todaysdata);
exit();
in CakePhp and when I debug this code in cakephp, I get a result in the browser
[{"status":{"uname":"sibin","pass":"shanu","upid":14}},
{"status": {"uname":"amal","pass":"amalu","upid":14}}
]
I need to extract these two status details seperately in Android
I tried one code in android, it gives result, but result is repeated
I want the result of two status seperately
If anybody know, please help me.
set status value as from current json String :
JSONArray jsonarr = new JSONArray("your json String");
for(int i = 0; i < jsonarr.length(); i++){
JSONObject jsonobj = jsonarr.getJSONObject(i);
// get status JSONObject
JSONObject jsonobjstatus = jsonobj.getJSONObject("status");
// get uname
String str_uname=jsonobjstatus.getString("uname");
// get pass
String str_pass=jsonobjstatus.getString("pass");
// get upid
String str_upid=jsonobjstatus.getString("upid");
}
Try with the following code.
JSONArray jsonArray = new JSONArray("yourJsonResponseInString");
for(int i=0;i<jsonArray.length();i++)
{
JSONObject e = jsonArray.getJSONObject(i);
JSONObject jsonObject = e.getJSONObject("status");
Log.i("=== UserName","::"+jsonObject.getString("uname"));
Log.i("=== Password","::"+jsonObject.getString("pass"));
Log.i("=== UId","::"+jsonObject.getString("upid"));
}

Android parsing JSON from REST web-service

I have a REST web service at http://susana.iovanalex.ro/esculap/ws2.php?key=abc&action=getpatientlist which returns a JSON payload similar to the one below:
[{"id":"15","nume":"Suciu","prenume":"Monica","location":"Cardiologie, Salon 4, Pat
2","alarm_pulse_min":"60","alarm_pulse_max":"90","alarm_oxy_min":"90"},
{"id":"101","nume":"Test Node","prenume":"Arduino","location":"UPT Electro,
B019","alarm_pulse_min":"50","alarm_pulse_max":"160","alarm_oxy_min":"93"},
{"id":"160","nume":"Vasilescu","prenume":"Ion","location":"Cardiologie, Salon 4, Pat
2","alarm_pulse_min":"60","alarm_pulse_max":"120","alarm_oxy_min":"80"},
{"id":"161","nume":"Lungescu","prenume":"Simion","location":"Pneumologie, Salon 5, Pat
5","alarm_pulse_min":"70","alarm_pulse_max":"110","alarm_oxy_min":"95"},
{"id":"162","nume":"Paunescu","prenume":"Ramona","location":"Cardiologie, Salon 4, Pat
2","alarm_pulse_min":"0","alarm_pulse_max":"0","alarm_oxy_min":"0"}]
When using Android's parser on following the tutorial from http://www.androidcompetencycenter.com/2009/10/json-parsing-in-android/ I want to return in an ArrayList the processed data.
I'm constantly getting the following error:
org.json.JSONException: Value [{"prenume":"Monica", ... "nume":"Paunescu"}] of type
org.json.JSONArray cannot be converted to JSONObject.
Could you please give me a code snippet or a pointer on where can I find some more info ?
Your whole Json is a JSONArray of JSONObjects.
Your trying to get a JSONObject i.e:
JSONObject jObject = new JSONObject("[{"prenume":"Monica", ... "nume":"Paunescu"}]");
but that is an Array!
Try:
JSONArray jArray = new JSONArray("[{"prenume":"Monica", ... "nume":"Paunescu"}]");
for(int i=0; i < jArray.length(); i++){
JSONObject jObject = jArray.getJSONObject(i);
String prenume = jObject.getString("prenume");
Log.i("TAG", "Prenume is: "+ prenume);
}
It's all explained here: http://www.json.org/ and here http://www.json.org/java/
see here http://www.json.org/ your web service contain an json Array not json Object so parse as:
JSONArray JSONArrays = new JSONArray(jString);
for(int n = 0; n < JSONArrays.length(); n++)
{
JSONObject object = JSONArrays.getJSONObject(n);
// do some stuff....
}
Best thing would be to use GSON for parsing the JSONAray string, that you received as output. this helps you to manage data as real objects, rather than Strings.
please have a look at the following post to parse your JSON array
How to Parse JSON Array in Android with Gson
only thing you need to do is create Classes with parameter names from your JSON output
You can refer this link
https://stackoverflow.com/a/11446076/1441666
{
"result": "success",
"countryCodeList":
[
{"countryCode":"00","countryName":"World Wide"},
{"countryCode":"kr","countryName":"Korea"}
]
}
Here below I am fetching country details
JSONObject json = new JSONObject(jsonstring);
JSONArray nameArray = json.names();
JSONArray valArray = json.toJSONArray(nameArray);
JSONArray valArray1 = valArray.getJSONArray(1);
valArray1.toString().replace("[", "");
valArray1.toString().replace("]", "");
int len = valArray1.length();
for (int i = 0; i < valArray1.length(); i++) {
Country country = new Country();
JSONObject arr = valArray1.getJSONObject(i);
country.setCountryCode(arr.getString("countryCode"));
country.setCountryName(arr.getString("countryName"));
arrCountries.add(country);
}
I think you should better use a library that can handle REST requests and objects serialization/deserialization for you out of box.
Take a look at Push Messages using REST api in android

Categories

Resources