0

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 ?

akjoshi
  • 15,374
  • 13
  • 103
  • 121
user1411688
  • 31
  • 2
  • 3
  • Are this real patient data sets without any kind of protection? Or will some kind of authenticatin be added? – Janusz Jul 17 '12 at 10:43
  • no, this is just a test version, for internal use; in production ot will be protected with a fireweall and a key pbased authentication for sensing devices (pulse sensors on fingers) and the clients with request data over HTTPS – user1411688 Jul 26 '12 at 13:35

5 Answers5

3

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/

Blundell
  • 75,855
  • 30
  • 208
  • 233
1

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....
}
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
1

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

Community
  • 1
  • 1
sunil
  • 6,444
  • 1
  • 32
  • 44
0

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);
}
Community
  • 1
  • 1
Nirali
  • 13,571
  • 6
  • 40
  • 53
0

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

Community
  • 1
  • 1
Arthur
  • 674
  • 1
  • 8
  • 14