2 qq 25884459 qq_25884459 于 2017.09.14 15:27 提问

JS解析JSon数组,求大神

{
"success": false,
"obj": "{\"phone_number\":\"1\",\"error\":\"0\",\"siStatus\":\"在用\",\"wjs_amount\":\"0.00\",\"ratePlan\":\"升级小灵通包月12元\",\"instanceUsage\":\"普通\",\"acc_Number\":\"\",\"userAddress\":\"让胡路区西苑街-22-4-501\",\"currentPrepay\":\"0.00\",\"lastTradeDate\":\"2017-09-13 15:29:47\",\"origAcctPrepay\":\"0.00\",\"specialBalanceForOne\":\"0.00\",\"prodType\":\"转联通小灵通\",\"chengKongInfo\":\"国内直拨,短信,灵通呼\",\"yearsEndDate\":\"非包年用户或包年资费已到期\",\"sumDueAmount\":\"0.00\",\"phoneNature\":\"私宅\",\"error_message\":\"\",\"acctPrepay\":\"0.00\",\"custName\":\"张军\",\"mcmt\":\" \",\"lastAmount\":\"0.00\"}",
"msgcode": null,
"map": null
}

我想取到obj下面的数据

5个回答

wgh_930708
wgh_930708   2017.09.14 15:30
已采纳

var json = JSON.parse(str); //str为你原json格式的字符串
var obj = json.obj;

qq_25884459
qq_25884459 回复高冷的自卑患者: 呃 获取不到 undefined
2 个月之前 回复
wgh_930708
wgh_930708 回复qq_25884459: obj.phone_number; obj.error;......
2 个月之前 回复
qq_25884459
qq_25884459 那下面的数据应该怎么拿到? obj[0]?
2 个月之前 回复
showbo
showbo   Ds   Rxr 2017.09.14 15:35

    var o = {
        "success": false,
        "obj": "{\"phone_number\":\"1\",\"error\":\"0\",\"siStatus\":\"在用\",\"wjs_amount\":\"0.00\",\"ratePlan\":\"升级小灵通包月12元\",\"instanceUsage\":\"普通\",\"acc_Number\":\"\",\"userAddress\":\"让胡路区西苑街-22-4-501\",\"currentPrepay\":\"0.00\",\"lastTradeDate\":\"2017-09-13 15:29:47\",\"origAcctPrepay\":\"0.00\",\"specialBalanceForOne\":\"0.00\",\"prodType\":\"转联通小灵通\",\"chengKongInfo\":\"国内直拨,短信,灵通呼\",\"yearsEndDate\":\"非包年用户或包年资费已到期\",\"sumDueAmount\":\"0.00\",\"phoneNature\":\"私宅\",\"error_message\":\"\",\"acctPrepay\":\"0.00\",\"custName\":\"张军\",\"mcmt\":\" \",\"lastAmount\":\"0.00\"}",
        "msgcode": null,
        "map": null
    };
    var obj = JSON.parse(o.obj)
    alert(obj.phone_number)
    alert(obj.ratePlan)
xdm15732053766
xdm15732053766   2017.09.14 16:15

d3.json("tourism.json", function(error, valuedata){

    //将读取到的数据存到数组values,令其索引号为各省的名称
    var values = [];
    for(var i=0; i<valuedata.provinces.length; i++){
        var name = valuedata.provinces[i].name;
        var value = valuedata.provinces[i].value;
        values[name] = value;
    }

    //求最大值和最小值
    var maxvalue = d3.max(valuedata.provinces, function(d){ return d.value; });
    var minvalue = 0;
qq_34897745
qq_34897745   2017.09.14 21:14

$.getJSON('data.json', function (data) {

if (data.succeed == "flase") {
$.each(data.obj, function (index, data) {
alert(data.value)
})
}
});

w172087242
w172087242   Ds   Rxr 2017.09.14 15:36
 var data = {
        "success": false,
        "obj": "{\"phone_number\":\"1\",\"error\":\"0\",\"siStatus\":\"在用\",\"wjs_amount\":\"0.00\",\"ratePlan\":\"升级小灵通包月12元\",\"instanceUsage\":\"普通\",\"acc_Number\":\"\",\"userAddress\":\"让胡路区西苑街-22-4-501\",\"currentPrepay\":\"0.00\",\"lastTradeDate\":\"2017-09-13 15:29:47\",\"origAcctPrepay\":\"0.00\",\"specialBalanceForOne\":\"0.00\",\"prodType\":\"转联通小灵通\",\"chengKongInfo\":\"国内直拨,短信,灵通呼\",\"yearsEndDate\":\"非包年用户或包年资费已到期\",\"sumDueAmount\":\"0.00\",\"phoneNature\":\"私宅\",\"error_message\":\"\",\"acctPrepay\":\"0.00\",\"custName\":\"张军\",\"mcmt\":\" \",\"lastAmount\":\"0.00\"}",
        "msgcode": null,
        "map": null
    };
    //首先获取obj字符串
    var objstr = data.obj;
    //如果有json的js序列化插件就用,没有就用eval
    var objJson = eval('(' + objstr + ')');
    console.log(objJson);//输出obj对象
    console.log(objJson.phone_number);//输出1
    console.log(objJson.siStatus);//输出在用

希望对你有帮助

Csdn user default icon
上传中...
上传图片
插入图片