user1411688
user1411688

Reputation: 31

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 ?

Upvotes: 0

Views: 5193

Answers (5)

Arthur
Arthur

Reputation: 674

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

Upvotes: 0

Nirali
Nirali

Reputation: 13805

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

Upvotes: 0

sunil
sunil

Reputation: 6604

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

Upvotes: 1

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

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....
}

Upvotes: 1

Blundell
Blundell

Reputation: 76458

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/

Upvotes: 3

Related Questions