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.org/javaのような任意の JSON ライブラリからフィードする必要があります。次に、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 に答える