繁体   English   中英

Android从REST Web服务解析JSON

[英]Android parsing JSON from REST web-service

我在http://susana.iovanalex.ro/esculap/ws2.php?key=abc&action=get Patientlist中有一个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.

您能否给我一个代码片段或一个指针,以了解在哪里可以找到更多信息?

您的整个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/

看到这里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....
}

最好的办法是使用GSON解析您作为输出收到的JSONAray字符串。 这可以帮助您将数据管理为实际对象,而不是字符串。

请看看以下文章以解析您的JSON数组

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

只需要做的就是从JSON输出中使用参数名称创建类

您可以参考此链接

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

我认为您最好使用一个可以开箱即用地处理REST请求和对象序列化/反序列化的库。 看看Android中使用REST API的推送消息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM