【问题标题】:Android parsing JSON from REST web-serviceAndroid 从 REST Web 服务解析 JSON
【发布时间】:2012-07-17 10:38:38
【问题描述】:

我在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.

您能否给我一个代码 sn-p 或一个指针,我可以在哪里找到更多信息?

【问题讨论】:

  • 这是没有任何保护的真实患者数据集吗?还是会添加某种身份验证?
  • 不,这只是一个测试版本,供内部使用;在生产中 ot 将通过防火墙和基于密钥 p 的身份验证来保护传感设备(手指上的脉冲传感器)和通过 HTTPS 请求数据的客户端

标签: android json web-services rest


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    JSONArray JSONArrays = new JSONArray(jString); 
    
    for(int n = 0; n < JSONArrays.length(); n++)
    {
        JSONObject object = JSONArrays.getJSONObject(n);
        // do some stuff....
    }
    

    【讨论】:

      【解决方案3】:

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

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

      How to Parse JSON Array in Android with Gson

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

      【讨论】:

        【解决方案4】:

        你可以参考这个链接

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

        【讨论】:

          【解决方案5】:

          我认为您最好使用可以为您处理 REST 请求和对象序列化/反序列化的库。 看看Push Messages using REST api in android

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-07-03
            • 2017-02-13
            • 1970-01-01
            • 2023-03-06
            相关资源
            最近更新 更多