使用gson解析jsonarray

时间:2017-01-20 07:20:47

标签: json gson

我正在尝试解析JSON数据,我从远程服务器获取数据。我做一个HTTP来获取这些数据。我得到" java.lang.IllegalStateException:不是JSON对象:[{.." 我使用gson来解析数据并获取字段。(" endPointName")

代码:

public void getDevices() throws Exception{
    //  createHttpClient();
        ContentExchange contentExchange = new ContentExchange(true); 
        contentExchange.setMethod("GET"); 
        contentExchange.setURL("http://10.37.14.XX:8080/api/clients"); 
      //  contentExchange
        httpClient.send(contentExchange); 
        if (contentExchange.waitForDone() == HttpExchange.STATUS_COMPLETED) { 
            System.out.println("##Sucesss");
             int status = contentExchange.getStatus();
             System.out.println(contentExchange.getResponseFields().getStringField("Content-Type"));
            String responseContent = contentExchange.getResponseContent();
            getAEDetails_gson(responseContent);

        }else{
            System.out.println("##failed");
        }




}

 private void getAEDetails_gson(String responseContent) throws Exception {
        JsonElement jelement = new JsonParser().parse(responseContent);
        JsonObject rootobj = jelement.getAsJsonObject();
        JsonArray paymentsArray = rootobj.getAsJsonArray();
        for (JsonElement pa : paymentsArray) {
            JsonObject paymentObj = pa.getAsJsonObject();
            String quoteid = paymentObj.get("endpoint").getAsString();
            System.out.println("name is " + quoteid);
        }
    }

示例Json(响应内容)

[
   {
      "endpoint":"testclient",
      "registrationId":"H9eTBcZbGf",
      "registrationDate":"2017-01-20T00:00:17-05:00",
      "lastUpdate":"2017-01-20T00:00:17-05:00",
      "address":"10.37.3.24:56730",
      "lwM2mVersion":"1.0",
      "lifetime":300,
      "bindingMode":"U",
      "rootPath":"/",
      "objectLinks":[
         {
            "url":"/",
            "attributes":{
               "rt":"123",
               "ct":1543
            }
         },
         {
            "url":"/1/0",
            "attributes":{

            }
         },

         {
            "url":"/1024/12",
            "attributes":{

            }
         }
      ],
      "secure":false,
      "additionalRegistrationAttributes":{

      }
   },
   {
      "endpoint":"test2",
      "registrationId":"1xqmsOx2QH",
      "registrationDate":"2017-01-20T00:00:54-05:00",
      "lastUpdate":"2017-01-20T00:00:54-05:00",
      "address":"10.37.3.24:56830",
      "lwM2mVersion":"1.0",
      "lifetime":300,
      "bindingMode":"U",
      "rootPath":"/",
      "objectLinks":[
         {
            "url":"/",
            "attributes":{
               "rt":"oma.lwm2m",
               "ct":1543
            }
         },
         {
            "url":"/1/0",
            "attributes":{

            }
         },

         {
            "url":"/1024/12",
            "attributes":{

            }
         }
      ],
      "secure":false,
      "additionalRegistrationAttributes":{

      }
   }
]

0 个答案:

没有答案