简体   繁体   English

如何使用 JQUERY 从 JSON 数组对象获取数据?

[英]How can I get data from JSON array object with JQUERY?

How can I get all the data from the JSON array objects with jquery?如何使用 jquery 从 JSON 数组对象中获取所有数据?
I have tried before but my code only can get the data from the JSON object.我之前尝试过,但我的代码只能从 JSON 对象中获取数据。

This is my json file student.json :这是我的 json 文件student.json

{"status":true,
  "offset":0,
  "limit":25,
  "total":2,
  "data":[
    { "id":231,
      "title":"mytitle1",
      "content":"myconten1",
      "created_at":"2017-07-10 03:56:32",
      "updated_at":"2017-07-10 03:56:32"
    },{ "id":230,
        "title":"mytitle2",
        "content":"mycontent2",
        "created_at":"2017-07-10 03:56:06",
        "updated_at":"2017-07-10 03:56:06"
    }]}

My js script :我的js脚本:

<script>
    $(document).ready(function(){
        $(function (){
            var $orders = $('#orders');
            $.ajax({
                type: 'GET',
                url: 'json/student.json',
                success: function(data) {
                    console.log('success', data);
                    $.each(data, function(i, dataentry){
                        $orders.append('<li>dataid: '+dataentry.id+'</li>');
                    });
                }
            });
        });

    });
</script>

So first, you don't need to write this:所以首先,你不需要写这个:

$(document).ready(function(){  
             $(function (){

Because $(function() ( w/oa space ) is a short for $(document).ready(function() .因为$(function() ( w/oa space ) 是$(document).ready(function()的缩写。

Regarding your issue - I believe that data is the entire JSON, so you need to extract data.data , so I would write this:关于你的问题 - 我相信data是整个 JSON,所以你需要提取data.data ,所以我会这样写:

$(function (){
    var $orders = $('#orders');
    $.ajax({
        type: 'GET',
        url: 'json/student.json',
        success: function(response) {      // <= this is the change
            var data = response.data;      // <= going inside the data itself
            $.each(data, function(i, data){
                $orders.append('<li>dataid: '+data.id+'</li>');
            });

        }
    });
});

In your success function, the data received is the actual data response of your ajax call.在你success的功能, data接收是你的Ajax调用的实际数据响应。

You should see it in your console.log statement with all properties like offset , limit , total etc.您应该在console.log语句中看到它,其中包含所有属性,例如offsetlimittotal等。

Anyway, you're trying to loop through the whole object, not the data property inside the response, which is actually the array you probably want to loop through.无论如何,您正在尝试遍历整个对象,而不是响应中的data属性,它实际上是您可能想要遍历的数组。 You won't get any errors because $.each can loop through object literals as well as through arrays.你不会得到任何错误,因为$.each可以遍历对象文字以及数组。

Here's how it should look like (i adjusted the variable names to make it clearer):这是它的外观(我调整了变量名称以使其更清晰):

success: function(response) {
    $.each(response.data, function(i, dataEntry){     // or response['data']
         $orders.append('<li>dataid: '+dataEntry.id+'</li>');
    });
}

Hope it helps.希望它有帮助。

If your ajax call is successful, then :如果您的 ajax 调用成功,则:

  • function(data) : this data is the base object returned from server, in your case its not the data property. function(data) :此数据是从服务器返回的基本对象,在您的情况下,它不是数据属性。

  • now the data in your json is an array.现在你的 json 中的数据是一个数组。

so, instead of using foreach on root object, use it on data.data .因此,不要在根对象上使用 foreach ,而是在data.data上使用它。 hope it will help.希望它会有所帮助。

Here you go with an example solution https://jsfiddle.net/xydqLLdb/在这里,您可以使用示例解决方案https://jsfiddle.net/xydqLLdb/

 var response = {"status":true, "offset":0, "limit":25, "total":2, "data":[ { "id":231, "title":"mytitle1", "content":"myconten1", "created_at":"2017-07-10 03:56:32", "updated_at":"2017-07-10 03:56:32" },{ "id":230, "title":"mytitle2", "content":"mycontent2", "created_at":"2017-07-10 03:56:06", "updated_at":"2017-07-10 03:56:06" }]}; $.each(response.data, function(i, data){ $('ul').append('<li>dataid: ' + data.id + '</li>'); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> </ul>

Based on your scenario根据您的场景

 $(document).ready(function(){ $(function (){ var $orders = $('#orders'); $.ajax({ type: 'GET', url: 'json/student.json', success: function(response) { $.each(response.data, function(i, data){ $orders.append('<li>dataid: ' + data.id + '</li>'); }); } }); }); });

You need to loop through the data key with the repsonse data .您需要使用响应数据遍历data key

Hope this will help you.希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM