Flickr JSON解析错误

时间:2012-05-07 03:07:46

标签: java json flickr

我已编写此代码以从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"}

但不确定为什么它没有得到“照片”阵列。任何人都可以帮助我。

谢谢。

1 个答案:

答案 0 :(得分:0)

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

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

我检查过,这有效! 希望它有所帮助:)