-8

我有以下 JSON:

[{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557704","mobile":"400089151"},
{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557705","mobile":"400089151"},
{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557706","mobile":"400089151"},
{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557707","mobile":"400089151"}]

我需要使用 js 函数提取所有“phoneNumber”。我正在使用 html 进行测试,但我的功能不太好:

function getNumbers(strJSON)
{   
            strJSON = "[{\"errorMessage\":\"success\",\"mobile\":\"400089151\",\"phoneNumber\":\"400557704\",\"returnCode\":\"0\"},{\"errorMessage\":\"success\",\"mobile\":\"400089151\",\"phoneNumber\":\"400557705\",\"returnCode\":\"0\"},{\"errorMessage\":\"success\",\"mobile\":\"400089151\",\"phoneNumber\":\"400557706\",\"returnCode\":\"0\"}]";

        var len = strJSON.length;

        var begin_index = strJSON.indexOf("returnCode") - 2;
        var last_index = len - 1;
        var string_toSplit = strJSON.substring(begin_index, last_index);
        var string_splitted = string_toSplit.split("{");
        var out="";

        alert(strJSON);
        alert("string_splitted");
        alert(string_splitted);
        for ( var i = 0; i < string_splitted.length; i++) 
        {
            if (string_splitted[i].charAt(string_splitted[i].length - 1) === ",") 
            {
                string_splitted[i] = string_splitted[i].slice(0, -1);
            }
            var json = "{" + string_splitted[i];
            var obj = JSON.parse(json);
            if (i == string_splitted.length) 
            {
                out = out + obj.phoneNumber;
            } 
            else
            {
                out = out + obj.phoneNumber + ",";
            }
        }
        return out;     

}
4

3 回答 3

3

对于现代浏览器,您可以使用该.map()方法

var j = [{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557704","mobile":"400089151"},
{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557705","mobile":"400089151"},
{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557706","mobile":"400089151"},
{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557707","mobile":"400089151"}];

var phones = j.map(function(item){return item.phoneNumber});

更新

看到你的代码后(不要尝试手动拆分/解析json字符串..使用JSON.parse方法)你应该使用

function getNumbers(strJSON)
{   
    var myJson = JSON.parse( strJSON );
    return myJson.map(function( item ){ return item.phoneNumber}).join(',');   
}
于 2013-02-21T00:06:45.107 回答
1

更新:更好的方法:

function getNumbers(strJSON)
{
    var obj = JSON.parse(strJSON);   

    return obj.map(x => x.phoneNumber).join(", ")
}

原帖:

一个直接的方法是迭代数组中的每个对象并单独取出值。

var info = [{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557704","mobile":"400089151"},
{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557705","mobile":"400089151"},
{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557706","mobile":"400089151"},
{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557707","mobile":"400089151"}];


var phoneNumbers = [];

for (var i = 0; i < info.length; i++)
{
    phoneNumbers.push(info[i].phoneNumber);
}

console.log(phoneNumbers);

http://jsfiddle.net/hX69r/

更新:

http://jsfiddle.net/hX69r/1/

var info = [{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557704","mobile":"400089151"},
{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557705","mobile":"400089151"},
{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557706","mobile":"400089151"},
{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557707","mobile":"400089151"}];

var infoString = JSON.stringify(info); //this just turns the object array 'info' into a string

var numbers = getNumbers(infoString);
console.log(numbers);

function getNumbers(strJSON)
{
    var obj = JSON.parse(strJSON);

    var phoneNumbers = [];

    for (var i = 0; i < obj.length; i++)
    {
        phoneNumbers.push(obj[i].phoneNumber);
    }

    return phoneNumbers.join(", ");
}

附加更新:

var info = [{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557704","mobile":"400089151"},
{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557705","mobile":"400089151"},
{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557706","mobile":"400089151"},
{"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557707","mobile":"400089151"}];

var infoSingle = {"returnCode":"0","errorMessage":"success","Code":{},"phoneNumber":"400557704","mobile":"400089151"};

console.log(info.length); // prints 4; so you know it has the []
console.log(infoSingle.length); // prints undefined; so you know it doesn't have []
于 2013-02-21T00:08:06.857 回答
0

不要试图重新发明轮子。

解析 JSON 的方法有很多:

于 2013-02-21T05:14:50.813 回答