0

我编写了这段代码来从 flickr 检索照片。但是当我运行程序时,它显示了这个错误:

Exception in thread "main" org.codehaus.jettison.json.JSONException: JSONObject["photo"] not found.

这是源代码

public static void main(String[] args) throws IOException, JSONException {

    HttpGet httpget = new HttpGet("http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=********&format=json&nojsoncallback=1&per_page=1&text=MatrixMovie");


    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    HttpClient httpClient = new DefaultHttpClient();

    String responseBody = httpClient.execute(httpget, responseHandler);

    System.out.println(responseBody);


    JSONObject jo2 = new JSONObject(responseBody);
    System.out.println(jo2);
    JSONArray jarray = jo2.getJSONArray("photo");
    System.out.println(jarray);

    for(int i=0; i<jarray.length(); i++)
    {

        JSONObject jobject = jarray.getJSONObject(i);
        //System.out.println(jobject.getString("id"));
        System.out.println(jobject.getString("owner"));
    }

}

}

这是我从 flickr 得到的回复。

{"photos":{"page":1,"pages":9,"perpage":1,"total":"9",
"photo": 
[{"id":"5589016310",
"owner":"61369554@N03",
"secret":"eb2bf0f940",
"server":"5293",
"farm":6,
"title":"MatrixMovie-GoldenAwake",
"ispublic":1,
"isfriend":0,
"isfamily":0}]},
"stat":"ok"}

但不知道为什么它没有得到“照片”数组。谁能帮我解决这个问题。

谢谢你。

4

1 回答 1

0

要调用照片,您应该首先将另一个对象称为照片,然后在该对象内调用数组照片!像这样:

JSONObject jo3 = jo2.getJSONObject("photos");
JSONArray jarray=jo3.getJSONArray("photo");

我检查了,这行得通!希望能帮助到你 :)

于 2016-02-08T11:40:16.183 回答