0

之后我从数据服务获得响应以从显示异常的字符串创建 JSON 数组,这是我的代码

response = client.execute(request);
// getJSONString(response);
entity = response.getEntity();
int length = (int) entity.getContentLength();
StringBuffer sb = new StringBuffer(length);
if (entity.getContentLength() != 0) {
   reader = new InputStreamReader(entity.getContent(), "UTF-8");

   String line = "";

   BufferedReader bufferedReader = new BufferedReader(reader);
   while((line = bufferedReader.readLine()) != null)
       result += line;
   reader.close();

   jar = new JSONArray(result);**this statement getting exception**
   for (int i = 0; i < jar.length(); i++) {
      try {
         JSONObject c = jar.getJSONObject(i);
         String vtype = c.getString("ChannelID");
         res.setText(""+vtype);
      } catch (JSONException e) {
        e.printStackTrace();
      }
   }
}

这是字符串中的响应这是我在字符串结果中的响应:

{
    "d":[{
    "__metadata":{
        "id":
        "XXX.XX.xX.XX:33396/FalconCPDataService.svc/DESystemOptions(guid'ca08834a-da4b-4bdd-9def-c91a7ea098b5')", "uri":
        "XXX.XX.xX.XX:33396/FalconCPDataService.svc/DESystemOptions(guid'ca08834a-da4b-4bdd-9def-c91a7ea098b5')", "type":
        "FalconCP_3_1Model.DESystemOption"
    },"DEOptionsLock":{
        "__deferred":{
            "uri":
            "XXX.XX.xX.XX:33396/FalconCPDataService.svc/DESystemOptions(guid'ca08834a-da4b-4bdd-9def-c91a7ea098b5')/DEOptionsLock"
        }
    },"ID":"ca08834a-da4b-4bdd-9def-c91a7ea098b5", "ApplicationType":0, "OptionsLocksID":
    "dd1f2e09-68c4-42ac-bfdf-afdb8d4bf631", "DefaultPulsewidth":230, "DefaultRate":40, "CreatedDate":
    "\/Date(1373465393870)\/", "IndicationType":1
    }]
}

在粗体声明中,我遇到了一个例外。需要帮助来解决这个问题

4

1 回答 1

1

尝试这个:

JSONObject jo = new JSONObject(result)
jar = jo.getJSONArray("d");
于 2014-03-18T13:27:37.933 回答