首页 新闻 会员 周边 捐助

js处理json的问题

0
悬赏园豆:50 [已解决问题] 解决于 2013-02-28 14:47

有这样一组数据
{"a":"0","b":"OK","c":[{"Account":"81614031","Time":"2012-11-26"},{"Account":"81614032","Time":"2012-11-26"},{"Account":"81614033","Time":"2012-11-26"},{"Account":"81614034","Time":"2012-11-26"},{"Account":"816140315","Time":"2012-11-26"},{"Account":"816140311","Time":"2012-11-26"},{"Account":"816140312","Time":"2012-11-26"},{"Account":"816140311","Time":"2012-11-26"}]}
我想将Account对应的号码提出来显示列表中,相同的只显示一次。

如:816140311,816140312,816140313,816140314,816140315这样显示。

请问用js怎么写呢?

jq2727的主页 jq2727 | 初学一级 | 园豆:98
提问于:2013-02-26 14:19
< >
分享
最佳答案
0
var json_data={....};//你上面的数据
var data={};
for(var i=0; i<json_data.c.length; i++){
  data[json_data.c[i].Account]="";
}
var result="";
for(var p in data){
 result+=p+",";
}
alert(result);
收获园豆:30
向往-SONG | 老鸟四级 |园豆:4853 | 2013-02-26 15:25
能看到结果了
for(var i=0; i<json_data.c.length; i++){ data[json_data.c[i].Account]=""; }
请问这里为什么要这么写呢,有什么用处
jq2727 | 园豆:98 (初学一级) | 2013-02-27 17:39

@jq2727: 

这里把data这个对象当作字典,把Account作为键存起来,这样每个键都不会重复(重复的自动覆盖了)。

最终data={"81614032":"","81614033":"",...},

然后用for(...in..)来遍历所有键(对象属性)。

向往-SONG | 园豆:4853 (老鸟四级) | 2013-02-27 18:01
其他回答(2)
0
Array.prototype.noRepeated=function(){   

var newArray=[], flags={};

 for (var i=0;i<this.length;i++){     

    var item= this[i];

    if (!flags[item]) {   
    
    newArray.push(item);   
    
    flags[item] = true;   
    
    }   
}   

 return newArray;   

} 
var a={"a":"0","b":"OK","c":[{"Account":"81614031","Time":"2012-11-26"},{"Account":"81614031","Time":"2012-11-26"}]}

var cItems=a.c;
var sourceArray= [];
$(cItems).each(function(index){
    
    sourceArray.push(cItems[index].Account)
    
});

sourceArray=sourceArray.noRepeated();
alert(sourceArray.join(",")); 
收获园豆:3
Yu | 园豆:12990 (专家六级) | 2013-02-26 15:02
0
 1     var str = '{"a":"0","b":"OK","c":[{"Account":"816140311","Time":"2012-11-26"},{"Account":"81614032","Time":"2012-11-26"},{"Account":"81614033","Time":"2012-11-26"},{"Account":"81614034","Time":"2012-11-26"},{"Account":"816140315","Time":"2012-11-26"},{"Account":"816140311","Time":"2012-11-26"},{"Account":"816140312","Time":"2012-11-26"},{"Account":"816140311","Time":"2012-11-26"}]}';
 2     var arr = eval('(' + str + ')').c;
 3     var obj = {};
 4     for (var i = 0, len = arr.length; i < len; i++) {
 5         if (obj[arr[i].Account] == undefined) {
 6             obj[arr[i].Account] = 1;
 7         }
 8     }
 9     var array = new Array();
10     for (var i in obj) {
11         array[array.length] = i;
12     }    
13     var result = array.join(',');
14     alert(result);
收获园豆:17
John.Young | 园豆:219 (菜鸟二级) | 2013-02-26 15:37

相同所有的

Account取出来放一个数组中,去掉书中重复的数
支持(0) 反对(0) 小何人家 | 园豆:329 (菜鸟二级) | 2013-02-27 15:56
能看到结果了
for
(var i = 0, len = arr.length; i < len; i++) { if (obj[arr[i].Account] == undefined) { obj[arr[i].Account] = 1; } }
请问这里为什么要这么写呢,有什么用处?
支持(0) 反对(0) jq2727 | 园豆:98 (初学一级) | 2013-02-27 17:37

@jq2727: 

你可以去了解下JavaScript的Array。把值当作索引,存在则舍弃。

支持(0) 反对(0) John.Young | 园豆:219 (菜鸟二级) | 2013-03-07 07:39
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册