如何从json对象获取预期的元素值

时间:2014-09-09 04:49:28

标签: javascript php arrays ajax json

如何对这个复杂的json对象使用json_decode()方法?特别是我想得到x的值和y的值。

    {"list":{"num":"11","type":"map"},"tagName":"resultset","childList":[{"list":{"addr":"Delhi","type":"exact","z":"3","y":"28.6327","x":"77.2197"},"tagName":"result","childList":[]},{"list":{"addr":"Delhi","type":"alternate"},"tagName":"result","childList":[]},{"list":{"addr":"Deoli,New > Delhi,Delhi","type":"alternate"},"tagName":"result","childList":[]},{"list":{"addr":"Pur > Delhi,New > Delhi,Delhi","type":"alternate"},"tagName":"result","childList":[]},{"list":{"addr":"Sabhaypur > Delhi,New > Delhi,Delhi","type":"alternate"},"tagName":"result","childList":[]},{"list":{"addr":"Chirag > Delhi,New > Delhi,Delhi","type":"alternate"},"tagName":"result","childList":[]},{"list":{"addr":"Delhi > Tyre Delhi,Kilokri,New > Delhi,Delhi","type":"alternate"},"tagName":"result","childList":[]},{"list":{"addr":"New > Delhi,Delhi","type":"alternate"},"tagName":"result","childList":[]},{"list":{"addr":"New > Delhi > District,Delhi","type":"alternate"},"tagName":"result","childList":[]},{"list":{"addr":"Babarpur,New > Delhi,Delhi","type":"alternate"},"tagName":"result","childList":[]},{"list":{"addr":"Barwala,New > Delhi,Delhi","type":"alternate"},"tagName":"result","childList":[]}]} 

here is array object

    Array > Array ( [list] => Array ( [num] => 11 [type] => map ) [tagName] => > resultset [childList] => Array ( [0] => Array ( [list] => Array ( > [addr] => Delhi [type] => exact [z] => 3 [y] => 28.6327 [x] => 77.2197 > ) [tagName] => result [childList] => Array ( ) ) [1] => Array ( [list] > => Array ( [addr] => Delhi [type] => alternate ) [tagName] => result [childList] => Array ( ) ) [2] => Array ( [list] => Array ( [addr] => > Deoli,New Delhi,Delhi [type] => alternate ) [tagName] => result > [childList] => Array ( ) ) [3] => Array ( [list] => Array ( [addr] => > Pur Delhi,New Delhi,Delhi [type] => alternate ) [tagName] => result > [childList] => Array ( ) ) [4] => Array ( [list] => Array ( [addr] => > Sabhaypur Delhi,New Delhi,Delhi [type] => alternate ) [tagName] => > result [childList] => Array ( ) ) [5] => Array ( [list] => Array ( > [addr] => Chirag Delhi,New Delhi,Delhi [type] => alternate ) [tagName] > => result [childList] => Array ( ) ) [6] => Array ( [list] => Array ( [addr] => Delhi Tyre Delhi,Kilokri,New Delhi,Delhi [type] => alternate > ) [tagName] => result [childList] => Array ( ) ) [7] => Array ( [list] > => Array ( [addr] => New Delhi,Delhi [type] => alternate ) [tagName] => result [childList] => Array ( ) ) [8] => Array ( [list] => Array ( [addr] => New Delhi District,Delhi [type] => alternate ) [tagName] => > result [childList] => Array ( ) ) [9] => Array ( [list] => Array ( > [addr] => Babarpur,New Delhi,Delhi [type] => alternate ) [tagName] => > result [childList] => Array ( ) ) [10] => Array ( [list] => Array ( > [addr] => Barwala,New Delhi,Delhi [type] => alternate ) [tagName] => > result [childList] => Array ( ) ) ) )

2 个答案:

答案 0 :(得分:1)

如果您正在使用jQuerys ajax,那么您可以在成功方法中访问响应,如下所示:(假设您正在进行GET)

$.ajax({
    type: "GET",
    url: "myphp.php",
    success: function() {
        var x = resp.childList[0].list.x;
        var y = resp.childList[0].list.y;
        $('#result').html('X: ' + x + 'Y: ' + y);
    }
});

请参阅如何使用PHP的jQuerys ajax。 http://iviewsource.com/codingtutorials/learning-how-to-use-jquery-ajax-with-php-video-tutorial/

答案 1 :(得分:0)

我得到x和y这样我能够在html div中保存我的json响应。: - )

    function StartAjax(ResultsId){
        $.ajax({
          type: "GET",
          url: "myphp.php",
          cache: false,
         success: function (data) {
          data=eval("("+data+")");

            for (var x = 0; x < data.childList.length; x++) {
             if(data.childList[x].list.type=='exact')
             {
               y=data.childList[x].list.x;
               k=data.childList[x].list.y;
    }
)};
    </script>
    </head>
    <body>
    <a href="#" onclick="StartAjax('ResultsId');">Click Here to see content from myphp.php</a>
    <div id="ResultsId"></div>