使用jQuery AJAX加载JSON数据

时间:2013-10-01 17:32:43

标签: javascript jquery ajax json

我需要在点击按钮时打印此信息并按日期排序,到目前为止我有这个:我有json文件看起来像这样,但我无法在页面上打印它仍然是避风港没有按日期排序部分。我不确定问题是否是我正在使用的ajax版本的链接或问题是什么,因为我在youtube上看到了一个看起来像这样的例子,它运行得很好。

JSON:

  [
{
    "users": [
        {
            "name": "user1",
            "Endorsement": "some comment",
            "date": "8/11/2012"
        },
        {
            "name": "user2",
            "Endorsement": "some comment2",
            "date": "9/27/11"
        },
        {
            "name": "user3",
            "Endorsement": "some comment3"
        },
        {
            "name": "user4",
            "Endorsement": "some comment4",
            "date": "4/2/13"
        },
        {
            "name": "user5",
            "Endorsement": "some comment5"
        },
        {
            "name": "user6",
            "Endorsement": "some comment6",
            "date": "3/17/13"
        },
        {
            "name": "user7",
            "Endorsement": "some comment7",
            "date": "5/22/13"
        },
        {
            "name": "user8",
            "Endorsement": "some comment8",
            "date": "9/27/13"
        }
    ]
}

HTML更新:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Contact</title>
    <link rel="shortcut icon" href="stridesFavicon.ico">
    <link rel="stylesheet" href="css/bootstrap.css">
    <link rel="stylesheet" href="css/bootstrap-responsive.css">
    <link rel="shortcut icon" href='http://sites.google.com/site/lowcoupling/favicon_16x16.ico' />

</head>
<body>
    <!--Body content-->

  <div id='Div1'>


    <a href="#" id="clickme">Get JSON Data</a>


    </div>


    <script type="text/javascript"     src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>



    <script type="text/javascript" src="js/bootstrap.js"></script>
    <script src="myscript.js" type="text/javascript" /></script>
    <script type="text/javascript">

        $(document).ready(function () {
            $('.dropdown-toggle').dropdown();
        });  
    </script>
</body>
</html>

JS更新:

$(“#clickme”)。点击(function(){

$.getJSON("users.json", function (data) {

    var items = [];
    $.each(data, function (key, dvalue) {


        $.each(dvalue, function (key, value) {
            items.push('<li id="' + key + '">' + value + '</li>');

        });
    });

    $('<ul/>', {
        'class': 'interest-list',
        html: items.join('')

    }).appendTo('body');

});

});

但是没有用。意思是不加载用户名;相反,每当我点击链接时,它就会打印出类似的内容:

  

•[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

5 个答案:

答案 0 :(得分:3)

jason缺少一些逗号,json中的每一行都应以逗号结尾,除非它是范围中的最后一个子节点,这使得它成为:

[
    {
        "users": [
            {
                "name": "user1",
                "Endorsement": "some comment",
                "date": "8/11/2012"
            },
            {
                "name": "user2",
                "Endorsement": "some comment2",
                "date": "9/27/11"
            },
            {
                "name": "user3",
                "Endorsement": "some comment3"
            },
            {
                "name": "user4",
                "Endorsement": "some comment4",
                "date": "4/2/13"
            },
            {
                "name": "user5",
                "Endorsement": "some comment5"
            },
            {
                "name": "user6",
                "Endorsement": "some comment6",
                "date": "3/17/13"
            },
            {
                "name": "user7",
                "Endorsement": "some comment7",
                "date": "5/22/13"
            },
            {
                "name": "user8",
                "Endorsement": "some comment8",
                "date": "9/27/3"
            }
        ]
    }
]

答案 1 :(得分:2)

试试!

$("button").click(function () {

$.getJSON("users.json", function (obj) {


    $.each(obj.users, function (key, value) {
        $("ul").append("<li>" + value.name + "</li>");

    });

});

});

如果有错误,请在浏览器中使用调试控制台。 要么做一个小提琴,要么写出什么是错误。并且你的html中似乎没有ul元素。

答案 2 :(得分:0)

json无效。我使用http://jsonlint.com/进行了验证,并在第6行显示错误。您在下面的部分留下了逗号:

"Endorsement": "some comment"  
               "date":"8/11/2012"

这是第一个认可日期对

答案 3 :(得分:0)

$("button").click(function () {

$.getJSON("users.json", function (data) {



for (i in data)
{

for (k in data[i]) {
               alert(data[i][k]);
            }


});

});

答案 4 :(得分:0)

$.ajax({
    type:'POST',
   url:'Default.aspx/GetPro',
  data:"{Pid:'"+ faram+"'}",
 contentType: 'application/json; charset=utf-8',
  dataType: 'json',
 async: true,
cache: false,

success: function(msg) {
 alert("{Pid:'"+ faram+"'}");
var orders =msg;
 if (orders.length > 0) {
test(orders);
   }
 else {
  alert("No Record Found");
   }
 },
error: function(xhr, textStatus, errorThrown) {
            alert(xhr.status);
              alert(errorThrown);
              alert(xhr.responseText);
        }

});

function test(data)
{

for (i in data)
          {
//ProductName Is Property Field Of c# Object
 alert(data[i]['ProductName'])
}



 }