如何使用多个jquery解析json数据

时间:2015-11-24 13:58:40

标签: jquery arrays json function

我的样本Json

[  { "id":"Name1", "fname":"Name1","lname":"Test","Teaser":"location1, location2" },
   { "id":"Name2", "fname":"Name2", "lname":"abcd", "Teaser":"location3, location4" },
   { "id":"Name3", "fname":"Name3", "lname":"yxzk","Teaser":"location2, location4"},
   { "id":"Name4", "fname":"Name4", "lname":"zxya", "Teaser":"location3"}
]

下面的代码读取json数据并选择所有唯一的Teaser。我们能够获得Json独特的预告片形式。

    var locarr = [];
    var loc = [];
    var jsonString = "["
    var test = [];
    var loca = []
    var map = new Object(); 
    $.ajax({
        url: "test.json",
        dataType: "json",
        success: function(result) {
            $(result).each(function(index, listItem) {
                loc = $.trim(result[index].Teaser).split(",");
                for (var i = 0; i < loc.length; i++)
                    locarr.push($.trim(loc[i]));
            });

            test = $.unique(locarr);

        $(result).each(function(index2, listItem2) 
        {
            var docname = result[index2].fname;
            var teaser = result[index2].Teaser;
            var locar = [];
            locar = result[index2].Teaser.split(",");               
             testfunc(docname,teaser);                         
        });
        //console.log(map); 
    }
});

这是我读取数据的新功能

    var val = 0;
    function testfunc(doc,teas){
            //console.log(map);
            for(var j=0;j<test.length;j++){
                if(teas.indexOf(test[j])> -1){                    
                    map[test[j]] = doc+"j";                       
                }
                val++;
            }                       
    }

我想要的是基于独特的Teaser需要在Teaser下添加fname。我的期望如下。是否有可能从Json获取数据。任何帮助。

location1
Name1

location2
Name1
Name3

location3
Name2
Name4

location4
Name2
Name3

0 个答案:

没有答案