将JSON元素放入android中的数组中

时间:2014-04-20 10:58:53

标签: android json parsing url

我需要把以后的网址放在" unescapedUrl"到String数组。

这是我的JSON

{"responseData": {"results":[{"GsearchResultClass":"GimageSearch","width":"1916","height":"2608","imageId":"ANd9GcTGxX6RvwPECJIOp4O6kV618ZupfzDDb42Xu0ti-HXfwRUzIjGn5gh_JRUQ","tbWidth":"110","tbHeight":"150","unescapedUrl":"http://upload.wikimedia.org/wikipedia/commons/e/e9/Official_portrait_of_Barack_Obama.jpg","url":"http://upload.wikimedia.org/wikipedia/commons/e/e9/Official_portrait_of_Barack_Obama.jpg","visibleUrl":"en.wikipedia.org","title":"Official_portrait_of_\u003cb\u003eBarack\u003c/b\u003e_\u003cb\u003eObama\u003c/b\u003e.jpg","titleNoFormatting":"Official_portrait_of_Barack_Obama.jpg","originalContextUrl":"http://en.wikipedia.org/wiki/Barack_Obama","content":"Official_portrait_of_\u003cb\u003eBarack\u003c/b\u003e_\u003cb\u003eObama\u003c/b\u003e.jpg","contentNoFormatting":"Official_portrait_of_Barack_Obama.jpg","tbUrl":"http://t1.gstatic.com/images?q\u003dtbn:ANd9GcTGxX6RvwPECJIOp4O6kV618ZupfzDDb42Xu0ti-HXfwRUzIjGn5gh_JRUQ"},{"GsearchResultClass":"GimageSearch","width":"2687","height":"3356","imageId":"ANd9GcSVcCN3mcnthFuvaHkRqSO53BdvHr9iTuHyhiBtsCsVeiTH1ksxUILJ74Lp","tbWidth":"120","tbHeight":"150","unescapedUrl":"http://upload.wikimedia.org/wikipedia/commons/8/8d/President_Barack_Obama.jpg","url":"http://upload.wikimedia.org/wikipedia/commons/8/8d/President_Barack_Obama.jpg","visibleUrl":"en.wikipedia.org","title":"\u003cb\u003eBarack Obama\u003c/b\u003e - Wikipedia, the free encyclopedia","titleNoFormatting":"Barack Obama - Wikipedia, the free encyclopedia","originalContextUrl":"http://en.wikipedia.org/wiki/Barack_Obama","content":"\u003cb\u003eBarack Obama\u003c/b\u003e","contentNoFormatting":"Barack Obama","tbUrl":"http://t0.gstatic.com/images?q\u003dtbn:ANd9GcSVcCN3mcnthFuvaHkRqSO53BdvHr9iTuHyhiBtsCsVeiTH1ksxUILJ74Lp"},{"GsearchResultClass":"GimageSearch","width":"605","height":"412","imageId":"ANd9GcQCGnit0lRUVD6X6fECArOEWfrNGdJ_Vc1RNMlE3aGTpyFNovbXtPYfNJXb","tbWidth":"135","tbHeight":"92","unescapedUrl":"http://www.history.com/images/media/slideshow/barack-obama/barack-obama-color.jpg","url":"http://www.history.com/images/media/slideshow/barack-obama/barack-obama-color.jpg","visibleUrl":"www.history.com","title":"\u003cb\u003eBarack Obama\u003c/b\u003e — History.com Photo Galleries","titleNoFormatting":"Barack Obama — History.com Photo Galleries","originalContextUrl":"http://www.history.com/photos/barack-obama","content":"President \u003cb\u003eBarack Obama\u003c/b\u003e","contentNoFormatting":"President Barack Obama","tbUrl":"http://t1.gstatic.com/images?q\u003dtbn:ANd9GcQCGnit0lRUVD6X6fECArOEWfrNGdJ_Vc1RNMlE3aGTpyFNovbXtPYfNJXb"},{"GsearchResultClass":"GimageSearch","width":"1443","height":"2100","imageId":"ANd9GcTyV4Gg9GTzq2mlcENtl-J92xYR0Mx3pgDHNuzY_kj9U0wyMEr7Rhqlj2Y","tbWidth":"103","tbHeight":"150","unescapedUrl":"http://upload.wikimedia.org/wikipedia/commons/0/01/Poster-sized_portrait_of_Barack_Obama_OrigRes.jpg","url":"http://upload.wikimedia.org/wikipedia/commons/0/01/Poster-sized_portrait_of_Barack_Obama_OrigRes.jpg","visibleUrl":"pag.wikipedia.org","title":"\u003cb\u003eBarack Obama\u003c/b\u003e - Wikipedia","titleNoFormatting":"Barack Obama - Wikipedia","originalContextUrl":"http://pag.wikipedia.org/wiki/Barack_Obama","content":"\u003cb\u003eBarack Obama\u003c/b\u003e - Wikipedia","contentNoFormatting":"Barack Obama - Wikipedia","tbUrl":"http://t3.gstatic.com/images?q\u003dtbn:ANd9GcTyV4Gg9GTzq2mlcENtl-J92xYR0Mx3pgDHNuzY_kj9U0wyMEr7Rhqlj2Y"}],"cursor":{"resultCount":"1,500,000,000","pages":[{"start":"0","label":1},{"start":"4","label":2},{"start":"8","label":3},{"start":"12","label":4},{"start":"16","label":5},{"start":"20","label":6},{"start":"24","label":7},{"start":"28","label":8}],"estimatedResultCount":"1500000000","currentPageIndex":0,"moreResultsUrl":"http://www.google.com/images?oe\u003dutf8\u0026ie\u003dutf8\u0026source\u003duds\u0026start\u003d0\u0026hl\u003diw\u0026q\u003dbarak+obama","searchResultTime":"0.28"}}, "responseDetails": null, "responseStatus": 200}

我怎么能实现这一点?

2 个答案:

答案 0 :(得分:2)

如评论中所述,您必须使用JSONParsing。

见: -

Object obj = jsonParser.parse(result.toString());           
JSONObject myJsonObject = (JSONObject) obj;

url= (String) myInfoJsonObject.get("unescapedUrl");

对要解析其值的所有键执行上述步骤。

答案 1 :(得分:0)

您必须首先解析json,然后将其添加到数组或arraylist。

{  // json object node
    "responseData": { // json object responseData
        "results": [  // json array results
            {         // json object node
                "GsearchResultClass": "GimageSearch",
                "width": "1916",
                "height": "2608",
                "imageId": "ANd9GcTGxX6RvwPECJIOp4O6kV618ZupfzDDb42Xu0ti-HXfwRUzIjGn5gh_JRUQ",
                "tbWidth": "110",
                "tbHeight": "150",
                "unescapedUrl": "http://upload.wikimedia.org/wikipedia/commons/e/e9/Official_portrait_of_Barack_Obama.jpg" // get the url by the key

解析

ArrayList<String> urls = new ArrayList<String>();
JSONObject jb = new JSONObject("your json string");
JSONObject response = jb.getJSONObject("responseData");
JSONArray jr = response.getJSONArray("results");

for(int i=0;i<jr.length();i++)
{
         JSOnObject jb1 =(JSONObject) jr.get(i);
         String url = jb1.getString("unescapedUrl");
         urls.add(url);
         // use arraylist and the url to arraylist
}