如何使用jquery ajax解析json文件中的多级数组

时间:2015-09-07 10:06:03

标签: jquery ajax json

解析多级json并获取所有数据。使用下面的代码,我只能从json文件中获得一个长描述。如何在下面的json文件中显示所有长描述。请帮忙。

<input type="button" value="Get and parse JSON" class="button" />
    <br />
    <span id="results"></span>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

    <script>

        $(document).ready(function() { 
            $('.button').click(function(){        
                $.ajax({
                    url: "http://devcda.bryant.com/bryant/en/us/CommonSearchHandler.ashx?type=17&blogcategories=",                 
                    dataType: "text",                   
                    success: function(data) {                        
                         var json = $.parseJSON(data);


                            $("#results").html('<a href="' + json.CurrentPage + '">' + json.ResultPayload[0].LongDescription + "</a>");

                    }
                });
            });
        });
    </script>

Json文件:

{
        "CurrentPage": 0,
        "Facets": null,
        "RecordCount": 1,
        "ResultPayload": [
            {
                "Name": null,
                "URI": "tcm:688-98798",
                "BlogCategories": [
                    "Air Quality"
                ],
                "CreationDate": "January 01, 0001",
                "DisplayTitle": null,
                "LongDescription": "Test1",
                "PageURL": ""
            }
            {
                "Name":null,
                "URI":"tcm:688-98798",
                "BlogCategories":[
                "Air Quality"
                ],
                "CreationDate":"January 01, 0001",
                "DisplayTitle":null,
                "LongDescription":"Test2",
                "PageURL":""
            }
        ],
        "suggestions": null }

1 个答案:

答案 0 :(得分:2)

您可以使用for循环

遍历json.ResultPayload
$("#results").empty();
for(var i in  json.ResultPayload){
   $("#results").append('<a href="' + json.CurrentPage + '">' 
        + json.ResultPayload[i].LongDescription + "</a>");
}