使用JSON和jQuery加载tripadvisor数据

时间:2014-10-27 10:34:06

标签: javascript php ajax json

我从tripadvisor获得了json数据这里有tripadvisor json和addrees的例子: http://api.tripadvisor.com/api/partner/2.0/map/-6.138235729678662,106.82242203190232?key=5a4401fbeeaf472ca5143d4(screet)&q=Ibis+Mangga+Dua+Hotel

      {
        "data": [
           {
            "see_all_photos": "http://www.tripadvisor.com/LocationPhotos-g294229-d299539-m22657-Ibis_Jakarta_Mangga_Dua_Hotel-Jakarta_Java.html",
           "web_url": "http://www.tripadvisor.com/Hotel_Review-g294229-d299539-Reviews-m22657-Ibis_Jakarta_Mangga_Dua_Hotel-Jakarta_Java.html",
          "percent_recommended": 52,
            "location_string": "Jakarta, Java",
      "location_id": "299539",
       "write_review": "http://www.tripadvisor.com/UserReview-g294229-d299539-m22657- Ibis_Jakarta_Mangga_Dua_Hotel-Jakarta_Java.html",
      "price_level": "$",
      "api_detail_url": "http://api.tripadvisor.com/api/partner/2.0/location/299539?key=5a4401fbeeaf472ca51",
      "address_obj": {
        "street1": "Jalan Pangeran Jayakarta 73",
        "street2": "",
        "city": "Jakarta",
        "state": null,
        "country": "Indonesia",
        "postalcode": "10730",
        "address_string": "Jalan Pangeran Jayakarta 73, Jakarta 10730 Indonesia"
       },
      "distance": ".49",
       "category": {
        "name": "hotel",
        "localized_name": "Hotel"
       },
       "awards": [],
      "subcategory": [
        {
          "name": "hotel",
          "localized_name": "Hotel"
        }
       ],
        "num_reviews": "287",
       "rating_image_url": "http://www.tripadvisor.com/img/cdsi/img2/ratings/traveler/3.0-22657-5.png",
       "name": "Ibis Jakarta Mangga Dua Hotel",
       "ancestors": [
          {
           "abbrv": null,
           "level": "City",
           "name": "Jakarta",
           "location_id": "294229"
         },
         {
           "abbrv": null,
           "level": "Region",
            "name": "Java",
           "location_id": "294228"
         },
         {
           "abbrv": null,
           "level": "Country",
           "name": "Indonesia",
           "location_id": "294225"
         }
        ],
        "bearing": "southeast",
        "longitude": "106.82842",
        "rating": "3.0",
        "latitude": "-6.14201",
        "ranking_data": {
         "geo_location_id": "294229",
         "ranking_string": "#122 of 322 hotels in Jakarta",
         "geo_location_name": "Jakarta",
         "ranking_out_of": "322",
          "ranking": "122"
        }
      }
     ],
   "paging": {
    "previous": null,
     "skipped": "0",
     "results": "1",
     "next": null,
      "total_results": "1"
   }
   }

与此代码相似:loading data with json and jquery

 function loadFlickr(flickrid)
 {
 // Display a loading icon in our display element
  $('#feed').html('<span><img src="/blog/images/lightbox-ico-loading.gif" alt="">    </span>');
 // Request the JSON and process it
 $.ajax({
 type:'GET',
 url:"http://api.flickr.com/services/feeds/photos_public.gne",
 data:"id="+flickrid+"&lang=en-us&format=json&jsoncallback=?",
 success:function(feed) {
 // Create an empty array to store images
 var thumbs = [];
 // Loop through the items
 for(var i=0, l=feed.items.length; i < l && i < 16; i)
 {
 // Manipulate the image to get thumb and medium sizes
 var img = feed.items[i].media.m.replace(
 /^(.*?)_m.jpg$/,
 '<a href="/blog/$1.jpg"><img src="/blog/$1_s.jpg" alt=""></a>'
  );
 // Add the new element to the array
 thumbs.push(img);
 }
 // Display the thumbnails on the page
 $('#feed').html(thumbs.join(''));
 // A function to add a lightbox effect
 addLB();
  },
 dataType:'jsonp'
  });
  } 

通话功能

loadFlickr("29080075@N02");

我如何使用java脚本或jquery保存到数组,我将使用该数组用于打印屏幕

之前感谢

4 个答案:

答案 0 :(得分:1)

将json对象存储在变量 -

$str //your json string

var_dump(json_decode($str, true));

答案 1 :(得分:0)

使用

将其保存到变量中
   var myArray = JSON.parse(Yourvariablecontainingjson); 

w3schools eplanation

答案 2 :(得分:0)

var string = JSON.stringify( object );
var object = JSON.parse( string );

答案 3 :(得分:-1)

只需使用json_encode($ variableContainingtheupperJsonCode)。它将返回一个数组。