如何用多行解析json

时间:2013-02-21 00:03:00

标签: javascript json

我有以下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;     

}

3 个答案:

答案 0 :(得分:3)

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

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(',');   
}

答案 1 :(得分: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 []

答案 2 :(得分:0)

不要试图重新发明轮子。

有许多方法可以解析JSON: