如何从jquery获取JSON对象

时间:2014-12-11 15:26:49

标签: jquery json

{
"data": {
    "4325474491990470056": {
        "hotel_geo_node": {
            "name": "The Capitol", 

                "property_type": [
                    "Hotel"
                ], 
                "property_budget_category": [
                    "Luxury"
                ], 

            }, 

        "hotel_data_node": {
            "rating": 4, 
            "stats": {
                "view_count": 713, 

                "prc": {
                    "go_101": {
                        "p": 4950.0, 
                        "dt": "2014-11-13", 
                        "dp": 4950.0
                    }, 
                    "go_201": {
                        "p": 5650.0, 
                        "dt": "2014-11-13", 
                        "dp": 5650.0
                    }
                }, 
                "last_vwd": {
                    "time": 1418233717
                }, 
                "book_count": 1
            }, 
            "name": "The Capitol", 
            "vid": [
                {
                    "u": "video", 
                    "l": "TABObQ27j4w"
                }
            ], 

                "vendor_priority": 318, 
                "scores": {
                    "vendor": 50, 
                    "views": 699, 
                    "trip_advisor": 324.0, 
                    "goibibo_review": 0, 
                    "frills": 2, 
                    "bookings": 1, 
                    "sub_vendor": 0, 
                    "static_data": 76
                }, 
                "data_src": "ingoibibo", 
                "location_slug": "city-center", 

            }, 
            "utime": {
                "$date": 1418255022000
            }, 

            "st": "LIV", 

            "desc": {
                "default": ""
            }, 
            "_id": "4325474491990470056", 
            "loc": {
                "city": "Bangalore", 
                "cnt_code": "IN", 
                "pin": "560001", 
                "city_cids": {
                    "trm": "BLR", 
                    "voy": "6771549831164675055"
                }, 
                "country": "INDIA", 
                "long": 77.5943716303, 
                "state": "Karnataka", 
                "location": "City Center - Mg Road / Near", 
                "lat": 12.9821798116, 
                "nhood": [
                    {
                        "_id": "1495661479872548865", 
                        "t": 122, 
                        "n": "City Center - Mg Road / Near"
                    }, 
                    {
                        "_id": "662155759444644883", 
                        "t": 20, 
                        "n": "Bangalore Palace"
                    }, 
                    {
                        "_id": "1527904823625587038", 
                        "t": 20, 
                        "n": "Bangalore Cantt Station"
                    }, 
                    {
                        "_id": "414302334487557591", 
                        "t": 20, 
                        "n": "Chinnaswamy Cricket Stadium"
                    }
                ]
            }, 

        }
    },

我使用了像这段代码的jquery。但是我无法通过这种方法获取数据请建议我如何做到这一点。似乎有JSON对象。所以我无法通过数组跟踪整个JSON。

$.ajax({
    url:url,
    dataType:'jsonp',
    crossDomain:true,
    success:function(data) {           
        todaydate= data.data[0];         
        alert(todaydate);          
    }
});

3 个答案:

答案 0 :(得分:1)

这不是Array,而是Object

$.ajax({
    url: url,
    dataType:'jsonp',
    crossDomain: true,
    success: function(data) {           
        var todaydate = data.data['4325474491990470056'];         
        alert(todaydate);          
    }
});

这意味着你不能做data.data [0]。相反,你可以这样做:

$.ajax({
    url: url,
    dataType: 'jsonp',
    crossDomain: true,
    success: function(data) {
        var firstElement;

        for (var key in data.data) {
            firstElement = data.data[key];
            break;
        }

        alert(firstElement);
        console.log(firstElement);
    }
});

修改

以下是获取酒店名称的方法:

$.ajax({
    url: url,
    dataType: 'jsonp',
    crossDomain: true,
    success: function(data) {
        var names = [];

        for (var key in data.data) {
            for (var hotelId in data.data[key]) {
                var hotel = data.data[key][hotelId];

                names.push(hotel.name);
            }
        }

        alert(names.join(', '));
        console.log(names);
    }
});

以下是检索平面阵列中所有酒店的方法:

$.ajax({
    url: url,
    dataType: 'jsonp',
    crossDomain: true,
    success: function(data) {
        var hotels = [];

        for (var key in data.data) {
            for (var hotelId in data.data[key]) {
                hotels.push(data.data[key][hotelId]);
            }
        }

        alert(hotels);
        console.log(hotels);

        // You can then access your hotels like that:
        for (var i = 0; i < hotels.length; i++) {
            var hotel = hotels[i];

            console.log(hotel.name);
        }
    }
});

答案 1 :(得分:0)

首先尝试使用console.log(数据)来查看您正在接收的结构

$.ajax({'url':url,dataType:'jsonp',crossDomain:true,success:function(response){      
     console.log(response.data); 
   }
});

然后按照结构获取每个值。

例如:

 var hotelGeoNode = response.data['4325474491990470056'].hotel_geo_node;

答案 2 :(得分:0)

假设你有一个数组(arr)发送到javascript。您必须使用的代码来编码数组

$arr[divResp]="<div>foo</div>";
$arr[js]="your JS code";
$arr[ddt]="your data";

json_encode(array($arr))

好的,说,获取此数组的代码是

var divAnswerJs="";
 var jsDinamically="";
 var dataAny="";
    request.done(function(dataset){
      for (var index in dataset){ //fetch your json data
         divAnswerJs=dataset[index].divResp;
         jsDinamically=dataset[index].js;
         dataAny=dataset[index].ddt;
      }

    //You can create your element
    $('#AnyId').html(divAnswerJs);
    //You can load javascript dinamically
    $('body').append(jsDinamically);
    //Or use your data
    $('#OtherAnyId').html(dataAny);

        }

您可以在此处找到完整的代码how to html form post - file uploading and reading json response from php server