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

        }
    },

I used jquery like this code . but i am not able the fetch the data bu this approach please suggest me how do i do this. it seems the there is JSON object. so i am not able to trace the whole JSON by array.

$.ajax({
    url:url,
    dataType:'jsonp',
    crossDomain:true,
    success:function(data) {           
        todaydate= data.data[0];         
        alert(todaydate);          
    }
});
Gnucki
  • 5,043
  • 2
  • 29
  • 44
Travelers
  • 67
  • 9

3 Answers3

1

This is not an Array but an Object:

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

This means that you cannot do data.data[0]. Instead, you can do:

$.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);
    }
});

EDIT:

Here is the way to fetch the names of your hotels:

$.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);
    }
});

And here is the way to retrieve all the hotels in a flat array:

$.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);
        }
    }
});
Gnucki
  • 5,043
  • 2
  • 29
  • 44
  • That's because the field *utime* is not defined! I don't know which of your field you want. Change it or tell me which one you want that I update my code! – Gnucki Dec 11 '14 at 15:46
  • if i want name then what should i do. i know if it would be json array. data.data.on[i]; – Travelers Dec 11 '14 at 16:02
  • I got it thank you!!! very much now i want the whole name in no of object array. http://developer.goibibo.com/api/voyager/?app_id=b409234f&app_key=b16024b017d9b73507e86e9e30ac3bfb&method=hotels.get_hotels_data_by_city&city_id=6771549831164675055 please check the link and i want every object access – Travelers Dec 11 '14 at 16:08
0

First try console.log(data) to see the structure you are receiving

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

and then follow the structure to get each value.

for example:

 var hotelGeoNode = response.data['4325474491990470056'].hotel_geo_node;
Ragnar
  • 4,393
  • 1
  • 27
  • 40
0

Lets say you have an array (arr) to send to javascript. The code you must use to encode a array is

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

json_encode(array($arr))

Ok, said that, the code to fetch this array is

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);

        }

the complete code you can find here how to html form post - file uploading and reading json response from php server

Community
  • 1
  • 1
IgorAlves
  • 5,086
  • 10
  • 52
  • 83