Android parsing JSON from REST web-service - android

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

Related

How to read this JSON

String json =
[
{"a":{"aa":"string","ab":"string"} },
{"b":{"ba":"string","bb":"string"} }
]
I am trying to parse this data using JsonObject.
When i use this code :
JSONStringer Js= new JSONStringer(json);
Log.d("json", ""+Js);
it gives me this : (the first line of json but i want all data)
{"a":{"aa":"string","ab":"string"} }
How can I read this with Android ?
It is a JsonArray not a simple JsonObject
try this:
JSONArray a = new JSONArray(json);
for (int i = 0; i < a.length(); i++) {
JSONObject row = a.getJSONObject(i);
Log.d("json", ""+row);
}
Actually, this JSON is kind of weird, as it wraps the array objects into one additional object each ("a" and "b"), which will make it hard to parse. It should look like this:
String json = [ {"name":"a","aa":"string","ab":"string"},
{"name":"b","ba":"string","bb":"string"} ]
And then you would parse it like this:
JSONArray root = new JSONArray(jsonString); //this is the above string
for(int i = 0; i < root.length(); i++){
JSONObject current = root.getJSONObject(i);
//Do the parsing here
}

Parse JSON string from remote URL in Android

Well I'm new to Android. I'm getting a JSON string from a remote URL.
[{"key":"myString1","val":"myValue1"},{"key":"myString2","val":"myValue2"},{"key":"myString3","val":"myValue3"},{"key":"myString4","val":"myValue4"},{"key":"myString5","val":"myValue5"}]
I just need to parse this JSON string & display all key-val pair. I tried something like below from one of the tutorial.
JSONArray jArray = new JSONArray(str);
json = jArray.getJSONObject(0); //This will take first pair.
But I don't know the syntax for iterating through whole json object. Any help would be appreciated. Thanks in Advance.
There's nothing special in it. You do it like iterating any other array.
Let's say you have two String arrays to be filled with values: String[] mKey, mValue
Reading from JSON array will be like:
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
mKey[i] = object.getString("key");
mValue[i] = object.getString("val");
}

How to parse JSON Array in android?

I have a JSON Array as follows:
jSONArray: {"li":[["apple\r\n","orange\r\n","mango\r\n"]]}
I want only the list as [apple, orange, mango] in android. Can anybody explain how to parse it in android without \r\n. I have tried as follows:
JSONArray jsonArray1 = new JSONArray(results.toString()); // which is the JSON Array
for(int i=0; i < jsonArray1.length(); i++) {
JSONObject jsonobject = new JSONObject();
JSONObject issue = jsonArray1.getJSONObject(i);
String _pubKey = issue.getString("li");
}
So, I am getting pubKey as [["apple\r\n","orange\r\n","mango\r\n"]]. I dont want \r\n.
Just replace those characters while parsing.
Example: If fruit is the string you want to put in your JSONArray jsonArray, call:
jsonArray.put(fruit.trim().replace("\r", "").replace("\n", ""));

Android Json parsing how to get Array

I have an JSON something like:
{
"store":"usa",
"values":["1","2","3","4"];
}
and so I go ahead and get a JSON object for values:
JSONObject values = json.getJSONObject("values");
So now I have this Json object but methods like getName("X") does not work for this type of JSON. There is no key value for the Array now. Its just Strings oen after another.
I want it to return like
String[] listValues = value.getArray();
But I don't see anything like this.
Any ideas ?
Thanks !!
Have you tried this?
JSONArray a = json.getJSONArray("values");
for (int i = 0; i < a.size(); i++) {
Log.d("Type", a.getString(i););
}
String myJson = "{
"store":"usa",
"values":["1","2","3","4"]
}";
JSONObject jsonObject = new JSONObject(myJson);
JSONArray array = jsonObject.getJSONArray("values");

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

Categories

Resources