0

私の PHP 応答は、次の多次元配列のようになります。

{
"results":
{"id":"153","title":"xyz","description":"abc"}, 
{"id":"154","title":"xyy","description":"abb"},
"filter_color":{"Red":1,"Blue":8},
"count_rows":{"rows":"10"}
}

jquery を使用して、このデータを取得し、そのデータをテーブルに表示したいのですが、特定のキーと値のペアを選択するにはどうすればよいでしょうか? (たとえば、結果からすべての説明を表示したいだけです)。

4

1 に答える 1

2

PHP 配列が次のような場合:

$myArray = array ( 'results' => array (
                                    array ( 'id' => '153',
                                            'title' => 'xyz',
                                            'description' => 'abc' ),
                                    array ( 'id' => '154',
                                            'title' => 'xyy',
                                            'description' => 'abb' )
                                 ),
                   'filter_color' => array ( 'Red' => 1, 'Blue' => 8 ),
                   'count_rows' => array ( 'rows' => '10' )
           );

を使用すると、次の応答が得られますjson_encode()

{
"results":
 [ {"id":"153","title":"xyz","description":"abc"}, 
   {"id":"154","title":"xyy","description":"abb"} ],
"filter_color":{"Red":1,"Blue":8},
"count_rows":{"rows":"10"}
}

jQuery は次のようになります。

$.ajax({
    url: "http://example.com",
    success: function(data) {
        // Then you can do this to print out the description of each result
        // in the browser console... or append the info to a table
        for(var i in data.results) {
            console.log(data.results[i].description);

            $("table").append("<tr><td>" + data.results[i].title + "</td><td>" + data.results[i].description + "</td></tr>");
        }
    }
});
于 2013-06-04T16:17:59.403 に答える