如何从json对象输出内容?

时间:2011-09-04 05:15:14

标签: javascript jquery json

我有以下json:

   "responseData":{
      "results":[
         {
            "GsearchResultClass":"GimageSearch",
            "width":"600",
            "height":"530",
            "imageId":"ANd9GcRnCkl_5N-gAKDeyTvALVLqZJsTUP4M24qEL6MlBB3cOZo-yyAIAoNbJHkR",
            "tbWidth":"135",
            "tbHeight":"119",
            "unescapedUrl":"http://www.harlemfur.com/images/Dog_Olive.jpg",
            "url":"http://www.harlemfur.com/images/Dog_Olive.jpg",
            "visibleUrl":"www.harlemfur.com",
            "title":"\u003cb\u003eDogs\u003c/b\u003e Archives",
            "titleNoFormatting":"Dogs Archives",
            "originalContextUrl":"http://www.harlemfur.com/dogs/",
            "content":"This note below is from a \u003cb\u003edog\u003c/b\u003e",
            "contentNoFormatting":"This note below is from a dog",
            "tbUrl":"http://t0.gstatic.com/images?q\u003dtbn:ANd9GcRnCkl_5N-gAKDeyTvALVLqZJsTUP4M24qEL6MlBB3cOZo-yyAIAoNbJHkR"
         },
         {
            "GsearchResultClass":"GimageSearch",
            "width":"400",
            "height":"366",
            "imageId":"ANd9GcRde3tgTE21xhyW1CYlhQyrgvgd3ubj72GK7jl50GqXdC6QrGcVFXbD0w",
            "tbWidth":"124",
            "tbHeight":"113",
            "unescapedUrl":"http://static.howstuffworks.com/gif/dog-best-friend-1.jpg",
            "url":"http://static.howstuffworks.com/gif/dog-best-friend-1.jpg",
            "visibleUrl":"animals.howstuffworks.com",
            "title":"HowStuffWorks \u0026quot;Is a \u003cb\u003edog\u003c/b\u003e really a man\u0026#39;s best friend?\u0026quot;",
            "titleNoFormatting":"HowStuffWorks \u0026quot;Is a dog really a man\u0026#39;s best friend?\u0026quot;",
            "originalContextUrl":"http://animals.howstuffworks.com/pets/dog-best-friend.htm",
            "content":"See more \u003cb\u003edog\u003c/b\u003e images.",
            "contentNoFormatting":"See more dog images.",
            "tbUrl":"http://t3.gstatic.com/images?q\u003dtbn:ANd9GcRde3tgTE21xhyW1CYlhQyrgvgd3ubj72GK7jl50GqXdC6QrGcVFXbD0w"
         },
         {
            "GsearchResultClass":"GimageSearch",
            "width":"400",
            "height":"358",
            "imageId":"ANd9GcRq2Pn5YCq81h03V0Gv8mDrs00vFqQVFqdvMEFo3EIFuv7ILCq4wgoJwiA",
            "tbWidth":"124",
            "tbHeight":"111",
            "unescapedUrl":"http://www.mydogneeds.com/dog-groups/herding-dogs/german-shepherd-dog.jpg",
            "url":"http://www.mydogneeds.com/dog-groups/herding-dogs/german-shepherd-dog.jpg",
            "visibleUrl":"www.mydogneeds.com",
            "title":"German Shepherd, Herding \u003cb\u003eDog\u003c/b\u003e Breeds",
            "titleNoFormatting":"German Shepherd, Herding Dog Breeds",
            "originalContextUrl":"http://www.mydogneeds.com/dog-groups/dog-breed-german-shepherd-dog.html",
            "content":"Herding \u003cb\u003eDog\u003c/b\u003e Breeds - German",
            "contentNoFormatting":"Herding Dog Breeds - German",
            "tbUrl":"http://t1.gstatic.com/images?q\u003dtbn:ANd9GcRq2Pn5YCq81h03V0Gv8mDrs00vFqQVFqdvMEFo3EIFuv7ILCq4wgoJwiA"
         },
         {
            "GsearchResultClass":"GimageSearch",
            "width":"350",
            "height":"349",
            "imageId":"ANd9GcTUELr8ovAZ0E_4Gt9zYz3fsc5YG2VL7KOoWbx_hhzwnaLjcGPS0da_hVQ",
            "tbWidth":"120",
            "tbHeight":"120",
            "unescapedUrl":"http://www.costumedogs.com/wp-content/uploads/2007/07/tux.jpg",
            "url":"http://www.costumedogs.com/wp-content/uploads/2007/07/tux.jpg",
            "visibleUrl":"www.costumedogs.com",
            "title":"Poker Games Online",
            "titleNoFormatting":"Poker Games Online",
            "originalContextUrl":"http://www.costumedogs.com/",
            "content":"\u003cb\u003eDog\u003c/b\u003e",
            "contentNoFormatting":"Dog",
            "tbUrl":"http://t2.gstatic.com/images?q\u003dtbn:ANd9GcTUELr8ovAZ0E_4Gt9zYz3fsc5YG2VL7KOoWbx_hhzwnaLjcGPS0da_hVQ"
         }
      ],
      "cursor":{
         "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":"158000000",
         "currentPageIndex":0,
         "moreResultsUrl":"http://www.google.com/images?oe\u003dutf8\u0026ie\u003dutf8\u0026source\u003duds\u0026start\u003d0\u0026hl\u003den\u0026q\u003ddog"
      }
   },
   "responseDetails":null,
   "responseStatus":200

如何循环使用它来获取所有4个结果对象的url

试图了解如何从json中提取数据。

2 个答案:

答案 0 :(得分:3)

就像使用任何其他JavaScript对象一样。

var results = responseData.results;
var i;
for (i = 0; i < results.length; i++) {
    // Do something with results[i].url
}

答案 1 :(得分:1)

检查此链接..它说明了解析js中的json:

http://www.json.org/js.html

代码:

 <script language="javascript">
 var k= {"responseData":{


    /*** rest of code ****/


    },
    "responseDetails":null,
    "responseStatus":200};



        for (i = 0; i < k.responseData.results.length; i++)
        {
        alert(k.responseData.results[i].url);
        }


 </script>