0

当我发送对 sourcetype Image 的请求时,我从 Bing Api 收到以下响应

{
    "d": {
        "results": [
            {
                "__metadata": {
                    "uri": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/Image?    Query='Xbox'&$skip=0&$top=1",
                    "type": "ImageResult"
                },
                "ID": "..",
                "Title": "STOCKS » XBOX",
                "MediaUrl": "http://blog.educastur.es/stocks/files/2010/12/xbox360.jpg",
                "SourceUrl": "http://blog.educastur.es/stocks/2010/12/20/xbox/",
                "DisplayUrl": "blog.educastur.es/stocks/2010/12/20/xbox",
                "Width": "1600",
                "Height": "1200",
                "FileSize": "733571",
                "ContentType": "image/jpeg",
                "Thumbnail": {
                    "__metadata": {
                        "type": "Bing.Thumbnail"
                    },
                    "MediaUrl": "http://ts3.mm.bing.net/th?id=H.4718224649029654&pid=15.1",
                    "ContentType": "image/jpg",
                    "Width": "300",
                    "Height": "225",
                    "FileSize": "11403"
                }
            }
        ]
    }
}

我需要的是MediaUrlThumbnail. 我正在使用以下代码,我可以获得title, description,但MediaUrl不是Thumbnail.

public ArrayList<String> parseJson(String json){
    ObjectMapper ob = new ObjectMapper();
    List<String> res=new ArrayList<String>();
    try {
        Map map = ob.readValue(json, Map.class);
        Object results = ((Map)map.get("d")).get("results");

        if(results instanceof ArrayList){

            for(Object o: (ArrayList)results){

               String thumbnail =  (String) ((Map) o).get("Thumbnail").get("MediaUrl"); //This does   not work
               res.add(thumbnail);
            }
        }

    } catch (IOException e) {
        logger.warn("Couldn't parse JSON",e);
    }

    return res;

}

您对如何解决此问题有任何想法吗?

4

1 回答 1

0

从评论中重新发布。它非常草率,但显然有效。

(String) ( ((Map) ((Map) o).get("Thumbnail") ).get("MediaUrl") );

我还想重申我的建议,即尽可能切换到json-simple

于 2013-11-06T14:08:23.483 回答