多级数组json的解析会抛出未定义的结果

时间:2015-09-04 14:07:08

标签: jquery json

下面是HTML和Json输出。 一旦我点击按钮我就可以获得CurrentPage值,而我无法获得LongDescription值。它引起了我的不确定。请对此有任何帮助

    
    

<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=Air%20Quality",                 
                dataType: "text",
                success: function(data) {                        
                     var json = $.parseJSON(data);
                    $('#results').html('CurrentPage: ' + json.CurrentPage + '<br> LongDescription:'+ json.ResultPayload.LongDescription);


                }
            });
        });
    });
</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)

查看:json.ResultPayload.LongDescription,现在查看您的json数据。您的json数据将ResultPayload定义为数组([]表示数组),您尝试解决的问题就像它是一个对象。您需要首先索引数组的第一个元素,然后定位属性LongDescription。所以你应该使用:json.ResultPayload[0].LongDescription