3

我需要在单击按钮并按日期排序时打印此信息,到目前为止我有这个:我有看起来像这样的 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").click(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');

});

});

但不工作。意思是不加载用户名;相反,每次我点击链接时它都会打印这样的东西:

•[对象对象],[对象对象],[对象对象],[对象对象],[对象对象],[对象对象],[对象对象],[对象对象]

4

5 回答 5

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"
            }
        ]
    }
]
于 2013-10-01T17:59:39.883 回答
2

尝试 !

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

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


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

    });

});

});

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

于 2013-10-01T17:40:01.557 回答
0

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

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

这是第一个代言日期对

于 2013-10-01T17:57:28.843 回答
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'])
}



 }
于 2013-11-24T07:54:11.463 回答
0
$("button").click(function () {

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



for (i in data)
{

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


});

});
于 2013-11-23T07:35:41.877 回答