Jquery -Ajax - 如何阅读下面的嵌套JSON

时间:2015-10-18 17:02:26

标签: javascript jquery json

我在下面有以下JSON,这里是fiddle and actual code。我试图读取过滤器中的所有值,以便我可以单独显示所有值。  示例 " 0.494765097719035"和 "住宿"等等。

" azIndexDescPages"

示例2 azIndexDescPages

guid- 3B5470AA-4683-11DF-A523-8C6895261AFF

name - 有关影响树的信息

shortURL-关于影响树的信息

我有点不确定如何获得这些价值

如何获取值过滤器,"字母组"和 azIndexDescPages

// JSON

{
    "view": "search-category",
    "keyword": "Services",
    "filters": [{
        "0.494765097719035": "Accommodations"
    }, {
        "0.7564062477523095": "Support Services"
    }, {
        "OATH": "Administrative"
    }],
    "num-results": 20,
    "data": [{
        "alphabeticalGroup": "Services and Bags",
        "azIndexDescPages": [{
            "guid": "8A5C58E6-BECE-11DC-8E9F-96DAE110FEB8",
            "name": "General Vendor ",
            "shortURL": "general-vendor",
            "parenthicalCategory1": null,
            "parenthicalCategory2": null,
            "keyword": null,
            "shortDesc": "General Vendor blah blah.",
            "isRenew": true,
            "isApply": true,
            "descAmbiguousPages": null
        }]
    }, {
        "alphabeticalGroup": "Bages",
        "azIndexDescPages": [{
            "guid": "3B5470AA-4683-11DF-A523-8C6895261AFF",
            "name": "Information About Impacting Lorem",
            "shortURL": "information-about-lorem",
            "parenthicalCategory1": null,
            "parenthicalCategory2": null,
            "keyword": null,
            "shortDesc": "Lorem IPSUM Lorem IPSUM, etc.",
            "isRenew": null,
            "isApply": null,
            "descAmbiguousPages": null
        }, {
            "guid": "F560F636-4682-11DF-91AF-EA6C84051BF5",
            "name": "lorem ipsum 3",
            "shortURL": "information-about-lorem-ipsum-3",
            "parenthicalCategory1": null,
            "parenthicalCategory2": null,
            "keyword": null,
            "shortDesc": "New new new lorem ipsum.",
            "isRenew": null,
            "isApply": null,
            "descAmbiguousPages": null
        }]
    }]
}

// JavaScript的

 function ajaxProcess() {

        var searchTerm = $("#term").val(); // get the user-entered search term on Enter key
        var URL2 = '/json/search-category-view.json';
        var tags = "&tags=" + searchTerm;
        //var tagmode="&tagmode=any";
        var jsonFormat = "&format=json";
        var ajaxURL = URL2 + "?" + tags + jsonFormat;


        $.ajax({
            url: ajaxURL,
            dataType: "json",
            //jsonp:"jsoncallback",
            success: function(data, textStatus, jqXHR) {

                console.log("Data " + data);
                //var jsonData = $.parseJSON(data);
                console.log("data, textStatus, jqXHR " + data + textStatus + jqXHR)

                $.each(data, function(i, result) {
                    console.log(" i" + [i] + " --" + result);
                    console.log(" <br\>" + [i] + " --" + result.filters);
                    //if (result.filters) {
                        // html += '<div class="quote">';
                        $.each(data.filters, function(i, filter) {
                            console.log("<br>  filter " + [i] + " --" + filter['i']);
                            //html += '<div class="quote-line">' + line + '</div>';
                        });

                        $.each(data.data, function(k, data) {
                            console.log("<br> Data alphabeticalGrou " + [k] + " --" + data.alphabeticalGroup);
                            console.log("<br> Data azIndexDescPages" + [k] + " --" + data.azIndexDescPages);
                            //html += '<div class="quote-line">' + line + '</div>';
                        });


                   // }



                    var searchResult = "";
                    searchResult += "<br>" + result[i] + "ss " + i;
                    searchResult += "<br> keyword-- " + this['keyword'];

                    $("#gallery").append(searchResult).fadeIn(200);



                });

            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.log('FAILED to get  JSON from AJAX call' + jqXHR + textStatus + errorThrown);

            }
        });


    }

1 个答案:

答案 0 :(得分:0)

这是一个使用json循环和Object.keyseval以及一些条件和嵌套循环的组合的解决方案..我不认为这是一个最佳解决方案,但它确实满足需求....我认为你可以更好地重构你的JSON对象,以获得更好的性能和结果

&#13;
&#13;
//JSON
var jsonObject = {
    "view": "search-category",
    "keyword": "Services",
    "filters": [{
        "0.494765097719035": "Accommodations"
    }, {
        "0.7564062477523095": "Support Services"
    }, {
        "OATH": "Administrative"
    }],
    "num-results": 20,
    "data": [{
        "alphabeticalGroup": "Services and Bags",
        "azIndexDescPages": [{
            "guid": "8A5C58E6-BECE-11DC-8E9F-96DAE110FEB8",
            "name": "General Vendor ",
            "shortURL": "general-vendor",
            "parenthicalCategory1": null,
            "parenthicalCategory2": null,
            "keyword": null,
            "shortDesc": "General Vendor blah blah.",
            "isRenew": true,
            "isApply": true,
            "descAmbiguousPages": null
        }]
    }, {
        "alphabeticalGroup": "Bages",
        "azIndexDescPages": [{
            "guid": "3B5470AA-4683-11DF-A523-8C6895261AFF",
            "name": "Information About Impacting Lorem",
            "shortURL": "information-about-lorem",
            "parenthicalCategory1": null,
            "parenthicalCategory2": null,
            "keyword": null,
            "shortDesc": "Lorem IPSUM Lorem IPSUM, etc.",
            "isRenew": null,
            "isApply": null,
            "descAmbiguousPages": null
        }, {
            "guid": "F560F636-4682-11DF-91AF-EA6C84051BF5",
            "name": "lorem ipsum 3",
            "shortURL": "information-about-lorem-ipsum-3",
            "parenthicalCategory1": null,
            "parenthicalCategory2": null,
            "keyword": null,
            "shortDesc": "New new new lorem ipsum.",
            "isRenew": null,
            "isApply": null,
            "descAmbiguousPages": null
        }]
    }]
}
//CODE

jQuery.each(jsonObject, function(key, value) {
    
    if(key == "filters")
    {
        jQuery.each(value, function(nestedKey, nestedValue) 
        {
            var objectKey = Object.keys(nestedValue)[0];
            var objectVal = eval("nestedValue['"+objectKey+"']");
            //console.log(eval("nestedValue['"+objectKey+"']"));
            $("#filters").append(objectKey + '------------' +  objectVal + '<br/>' );
        });
        
    }
    
    if(key == "data")
    {
        jQuery.each(value, function(nestedKey, nestedValue) 
        {
            jQuery.each(nestedValue.azIndexDescPages, function(nestedKey, nestedValue) 
        	{
                jQuery.each(nestedValue, function(nestedKey, nestedValue) 
				{	
                     console.log(nestedValue);
           		 $("#data").append(nestedKey + '------------' +  nestedValue + '<br/>' );
                     
                });
           		
			});
            
            $("#data").append('<br /><br />');
             
        });
        
    }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="filters">
    
    Filters : <br />
</div>
<br />
<div id="data">
    
    Data : <br />
</div>
&#13;
&#13;
&#13;