我需要查找我的JSON输出:
[ { masterCardNo:90363.01 }, { masterCardNo:90363.02 }, { masterCardNo:90363004 }, { masterCardNo:90363004 }, { masterCardNo:90363004 }, { masterCardNo:90363004 }, { masterCardNo:90363004 }, { masterCardNo:90363004 }, { masterCardNo:90363004 }, { masterCardNo:2906.01 } ]
To like like:
[“90363004”,“90363001”,“90363003”]
我的输出名为“value”......
感谢您的帮助..
答案 0 :(得分:5)
使用.map()将对象数组转换为所需结构的数组
var value = [{
masterCardNo: 90363.01
}, {
masterCardNo: 90363.02
}, {
masterCardNo: 90363004
}, {
masterCardNo: 90363004
}, {
masterCardNo: 90363004
}, {
masterCardNo: 90363004
}, {
masterCardNo: 90363004
}, {
masterCardNo: 90363004
}, {
masterCardNo: 90363004
}, {
masterCardNo: 2906.01
}]
value = $.map(value, function(obj){
return obj.masterCardNo
});
console.log(value)
演示:Fiddle
答案 1 :(得分:1)
var json = '[numbers { masterCardNo: 90363.01 }, { masterCardNo: 90363.02 }, { masterCardNo: 90363004 }, { masterCardNo: 90363004 }, { masterCardNo: 90363004 }, { masterCardNo: 90363004 }, { masterCardNo: 90363004 }, { masterCardNo: 90363004 }, { masterCardNo: 90363004 }, { masterCardNo: 2906.01 } ]';
var json_parsed = $.parseJSON(json);
var no = "[";
for (var i = 0; i < json_parsed.numbers.length; i++){
no += json_parsed.numbers[i].masterCardNo+",";
}
no+="]";
试试这个
您也可以根据需要进行编辑
答案 2 :(得分:0)
将其转换为字符串&amp;将'{'
替换为""
使用替换关键字
http://www.dotnetperls.com/replace
或
你可以在javascript中使用相同的替换关键字
为了更好地理解,请查看此链接:jsfiddle.net/Mrw8k/2
var data1 = "[{ masterCardNo: 90363.01 }, { masterCardNo: 90363.02 }, { masterCardNo: 90363004 }, { masterCardNo: 90363004 }, { masterCardNo: 90363004 }, { masterCardNo: 90363004 }, { masterCardNo: 90363004 }, { masterCardNo: 90363004 }, { masterCardNo: 90363004 }, { masterCardNo: 2906.01 }]";
for (i = 0; i < data1.split(',').length; i++) {
data1 = data1.replace(" masterCardNo:", "");
data1 = data1.replace("{", '"');
data1 = data1.replace("}", '"');
}
document.getElementById("demo").innerHTML = data1;
答案 3 :(得分:0)
尝试使用@MatYa答案
检查这个小提琴http://jsfiddle.net/Mh22s/
data1 = data1.replace("{ masterCardNo:",'');
data1 = data1.replace("}",'');