2015-09-14 47 views
1

嗨下面是二級json數組解析的代碼。一級json輸出我能夠獲取。第二級我不能。即下CategoryImage我有兩個屬性,我可以不能夠獲取任何人都可以在this.Thanks幫助如何使用jquery ajax解析第二層JSON數組

<script>   
     $(document).ready(function() { 
      var cat=""; 
      categories(cat); 
      $('ul li').click(function(){ 
       var cat = $(this).text();    
       categories(cat);    
      }); 
     }); 
     function categories(cat){     
      $.ajax({ 
        url: "http://devcda.bryant.com/bryant/en/us/CommonSearchHandler.ashx?type=17&blogcategories="+cat,         dataType: "text",     
        success: function(data) {       
         var json = $.parseJSON(data); 
         var e=""; 
         $(".blog_desc").remove(); 
         for(var i in json.ResultPayload){ 


          e+="<div class='blog_desc'>"; 
          e+="<div class='tmpstr_hdr_cntnt container'>"; 
          e+=" <div class='col-lg-9 col-md-10 col-sm-10 col-xs-12'>" ; 
          e+="<h3>"+json.ResultPayload[i].DisplayTitle+"</h3>"; 
          e+="<p><span class='date_blg'>"+json.ResultPayload[i].CreationDate+"</span>Categories: <span class='cat_blg'>"+json.ResultPayload[i].BlogCategories+"</span></p>";       
          e+="<div class='col-lg-3 col-md-3 col-sm-3 col-xs-12'><img src="+json.ResultPayload[i].CategoryImage[i].Path+" class='img-responsive'> </div>"; 

          e+="<div class='col-lg-9 col-md-9 col-sm-9 col-xs-12'><p>"+json.ResultPayload[i].LongDescription+"</p>"; 
          e+="<p class='read_more'><a href="+json.ResultPayload[i].PageURL+">READ MORE</a></p>"; 
          e+="</div>";       
          e+="</div>"; 
          e+="</div>"; 
          e+="</div>"; 
          } 


         $("input").after(e); 

        } 
       }); 


     } 

    </script> 

JSON文件

{ 
    "CurrentPage":0, 
    "Facets":null, 
    "RecordCount":4, 
    "ResultPayload":[ 
     { 
     "Name":null, 
     "URI":"tcm:688-98862", 
     "BlogCategories":[ 
      "Efficiency" 
     ], 
     "CreationDate":"September 01, 2015", 
     "DisplayTitle":"How often should you change your furnace filter?", 
     "LongDescription":"How often should you change your furnace filter? How often should you change your furnace filter?How often should you change your furnace filter?How often should you change your furnace filter?How often should you change your furnace filter?How often should you change your furnace filter?", 
     "PageURL":"" 
     }, 
     { 
     "Name":null, 
     "URI":"tcm:688-98863", 
     "BlogCategories":[ 
      "Air Quality" 
     ], 
     "CreationDate":"September 01, 2015", 
     "DisplayTitle":"Test", 
     "LongDescription":"Test", 
     "PageURL":"" 
     }, 
     { 
     "Name":null, 
     "URI":"tcm:688-98798", 
     "BlogCategories":[ 
      "Air Quality" 
     ], 
     "CreationDate":"September 01, 2015", 
     "DisplayTitle":"Test", 
     "LongDescription":"Test", 
     "PageURL":"\/bryant\/en\/us\/about-bryant\/blog\/blog-details\/Test%20Air%20quality.aspx" 
     }, 
     { 
     "Name":null, 
     "URI":"tcm:688-96383", 
     "BlogCategories":[ 
      "Efficiency" 
     ], 
     "CategoryImage":{ 
      "AltText":"Blog_Detail_12345", 
      "Path":"\/\/files.dev.bryant.com\/Bryant\/en\/us\/contentimages\/Blog_Detail_12345.png" 
     }, 
     "CreationDate":"September 01, 2015", 
     "DisplayTitle":"How often should you change your furnace filter?", 
     "LongDescription":"How often should you change your furnace filter? How often should you change your furnace filter?How often should you change your furnace filter?How often should you change your furnace filter?How often should you change your furnace filter?How often should you change your furnace filter?", 
     "PageURL":"\/bryant\/en\/us\/about-bryant\/blog\/blog-details\/How%20often%20should%20you%20change%20your%20furnace%20filter.aspx" 
     } 
    ], 
    "suggestions":null 
} 
+0

你可以提供同樣的js小提琴嗎? – sunil

回答

2

CategoryImage不是一個數組,以便ResultPayload [I] .CategoryImage.Path應該可以工作

+0

它返回對象object..It沒有爲我工作 – saminathan

+0

請參閱http://jsfiddle.net/c7zznofj/ – calinaadi

+0

謝謝calinaadi ..我厭倦了其他方式,並修復 – saminathan