I am getting the following response from Bing Api when I send request for sourcetype Image

{
    "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"
                }
            }
        ]
    }
}

and what I need is to get the MediaUrl from the Thumbnail. I am using the following code where I can get title, description, but not the MediaUrl of the 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;

}

Do you have any idea on how to fix this?

有帮助吗?

解决方案

Reposting this from comments. It's extremely sloppy, but apparently works.

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

I'd also like to re-state my recommendation of switching to json-simple where possible.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top