0

我在http://susana.iovanalex.ro/esculap/ws2.php?key=abc&action=getpatientlist有一个 REST Web 服务,它返回类似于以下内容的 JSON 有效负载:

[{"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"}]

在按照http://www.androidcompetencycenter.com/2009/10/json-parsing-in-android/的教程使用 Android 的解析器时,我想在 ArrayList 中返回处理后的数据。

我不断收到以下错误:

 org.json.JSONException: Value [{"prenume":"Monica", ... "nume":"Paunescu"}] of type         
 org.json.JSONArray cannot be converted to JSONObject.

你能给我一个代码片段或一个关于我在哪里可以找到更多信息的指针吗?

4

5 回答 5

3

你的整个 Json 是 JSONObjects 的 JSONArray。

您尝试获取 JSONObject 即:

 JSONObject jObject = new JSONObject("[{"prenume":"Monica", ... "nume":"Paunescu"}]");

但那是一个数组!

尝试:

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

这一切都在这里解释:http: //www.json.org/和这里http://www.json.org/java/

于 2012-07-17T10:43:14.163 回答
1

最好的办法是使用 GSON 来解析您作为输出收到的 JSONAray 字符串。这有助于您将数据作为真实对象而不是字符串来管理。

请查看以下帖子以解析您的 JSON 数组

如何使用 Gson 在 Android 中解析 JSON 数组

您唯一需要做的就是使用 JSON 输出中的参数名称创建类

于 2012-07-17T10:59:34.330 回答
1

请参阅此处http://www.json.org/您的 Web 服务包含一个 json 数组而不是 json 对象,因此解析为:

JSONArray JSONArrays = new JSONArray(jString); 

for(int n = 0; n < JSONArrays.length(); n++)
{
    JSONObject object = JSONArrays.getJSONObject(n);
    // do some stuff....
}
于 2012-07-17T10:43:41.817 回答
0

你可以参考这个链接

https://stackoverflow.com/a/11446076/1441666

{
"result": "success",
"countryCodeList":
[
  {"countryCode":"00","countryName":"World Wide"},
  {"countryCode":"kr","countryName":"Korea"}
] 
}

下面我正在获取国家/地区详细信息

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);
}
于 2012-07-17T11:00:11.777 回答
0

我认为您应该更好地使用可以为您处理 REST 请求和对象序列化/反序列化的库。看看在 android 中使用 REST api 的推送消息

于 2013-07-14T06:02:20.017 回答