2015-09-07 44 views
1

解析多級json並獲取所有數據。使用下面的代碼,我只能從json文件中獲得一個長描述。如何在下面的json文件中顯示所有長描述。請幫助。如何使用jquery ajax解析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 } 

回答

2

您可以在json.ResultPayload使用for循環

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

謝謝喜悅......其作品迭代:) – saminathan