0

我有以下代码用于调用返回 JSON 格式如下的 API:-

{
  "data": [
    {
      " Ser": 1,
      " No": 1
    },
    {
      " Ser": 2,
      " No": 2
    },
    {
      " Ser": 3,
      " No": 3
    },
    {
      " Ser": 4,
      " No": 4
    },
    {
      " Ser": 5,
      " No": 5
    },

  ]
}

代码是: -

    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet("/json/api/getinfo?type=100");
    HttpResponse response = client.execute(request);
// Get the response, how i can loop through the returned JSON and assign the reterned json to a global parameters which i can access from ym system using has values #parameter1# , #parameter2#, etc.

那么如何遍历返回的 JSON 并将其分配给全局参数“NO”呢?此致

4

1 回答 1

1

您应该通过文档中提到的从HttpReponse对象中获取内容。然后应该将内容从任何 JSON 库(例如来自json.org/java的库)馈送到 a 。然后,您可以通过 遍历 JSON 输出并从中提取元素。HttpEntity.getContent()JSONObjectJSONObject

HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) 
{
    //
    // Using Commons IO library's IOUtils method 
    // to read the content from the stream.
    //
    String json = IOUtils.toString(entity.getContent());
    JSONObject obj = new JSONObject(json);
    // Process the JSON

    // shutdown the connection.
}
于 2012-12-02T11:37:16.867 回答