Flickr JSON在Android中解析

时间:2012-11-26 19:25:24

标签: android json parsing flickr

我想从这个url(http://api.flickr.com/services/feeds/photos_public.gne?format=json)解析JSON,但文档实际上返回了一个无效的JSON。参见

       jsonFlickrFeed({
        "title": "Uploads from everyone",
        "link": "http://www.flickr.com/photos/",
        "description": "",
        "modified": "2012-11-26T18:27:41Z",
        "generator": "http://www.flickr.com/",
        "items": [
       {
            "title": "2012 Thanksgiving Holiday Weekend",
            "link": "http://www.flickr.com/photos/agape_boarding_school/8220697785/",
            "media": {"m":"http://farm9.staticflickr.com/8478/8220697785_bb298ac5b3_m.jpg"},
            "date_taken": "2012-11-24T14:19:21-08:00",
            "description": " <p><a href=\"http://www.flickr.com/people/agape_boarding_school/\">Agape Boarding School<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/agape_boarding_school/8220697785/\" title=\"2012 Thanksgiving Holiday Weekend\"><img src=\"http://farm9.staticflickr.com/8478/8220697785_bb298ac5b3_m.jpg\" width=\"240\" height=\"159\" alt=\"2012 Thanksgiving Holiday Weekend\" /><\/a><\/p> <p>Great Day at Agape Boarding School<\/p>",
            "published": "2012-11-26T18:27:41Z",
            "author": "nobody@flickr.com (Agape Boarding School)",
            "author_id": "36563683@N07",
            "tags": "school boarding agape"
       },  ...

我想让我的解析器尽可能通用,那么更好的方法是删除“jsonFlickrFeed”(文档的一部分,只用它自己的JSON?

public class JSONParser extends AsyncTask <String, Void, JSONObject> {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

@Override
protected JSONObject doInBackground(String... params) {
    String url=params[0];

    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();           

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;
}

}

3 个答案:

答案 0 :(得分:9)

我已经解决了这个问题。

Flickr旧版网络API 为:http://api.flickr.com/services/feeds/photos_public.gne?id=xxxxxxxx&lang=en-us&format=json

Flickr最新的网络API 是:http://api.flickr.com/services/feeds/photos_public.gne?id=xxxxxxxx&lang=en-us&format=json&nojsoncallback=1

使用最新的一个网络API

替换 用户ID ,而不是 xxxxxxxx

希望它对你有所帮助。

答案 1 :(得分:1)

看起来你想要的是原始的json。你想要添加一个&#34; nojsoncallback = 1&#34;删除函数包装器。

[请参阅FLICKR JSON响应格式](https://forum.processing.org/one/topic/flickr-json-parsing-noob.html&#34;如果您只想要原始JSON,没有函数包装器&#34;)

答案 2 :(得分:0)

正如您所说,在解析之前,您只需删除String的这一部分。

如果你想要更通用,你可以搜索第一个{字符并删除它之前的所有内容,并删除最后一个}字符并删除它之后的所有内容。但这仍然无法确保输入有效。