Get single value from JSON object

后端 未结 3 1656
醉酒成梦
醉酒成梦 2021-01-29 06:29

I am using Google Maps API with jQuery getJSON:

$.getJSON(requestUrl, function(data) {
        var distance = //need to parse value from \'data\' object here
            


        
相关标签:
3条回答
  • 2021-01-29 06:56
    data.rows.forEach(function(value){ 
        value.elements.forEach(function(childValue) {
          console.log(childValue.distance.text);
        });
    });
    

    The above nested forEach should get you the distance value.

    0 讨论(0)
  • 2021-01-29 06:59
    $.getJSON(requestUrl, function(data) {
        var distance = data.rows[0].elements[0].distance.text;
    });
    

    Pretty simple, but I do want to know if you'll ever get more then one row or element from your function?

    0 讨论(0)
  • 2021-01-29 07:03
    var json = JSON.parse(data);
    distance= json["rows"]["elements"]["distance"]["text"];
    

    Hope it helps

    0 讨论(0)
提交回复
热议问题