Nodejs jsonArray解析

时间:2014-02-05 13:55:08

标签: json node.js

我有一个json对象,我想从中获取数据! 这是我的json对象

 "[androidVersionName=2.3.3, androidVersionId=10, androidId=fa0bef4b5a48eacb, mobileModel=sdk, mobileManufacturer=unknown, mobileId=GRI34, mobileProduct=sdk, applicationName=com.example.socketclient, applicationVersionName=1.0, applicationVersionCode=1, applicationState=INACTIVE, screenWidth=480, screenHeight=480, screenDensity=240, screenDensityName=hdpi, atdPackages=com.atd.panberes(1)]"

这是我的代码:

 var json = JSON.parse(data);
 var androidVersionName = data.rowsets['androidVersionName'].row;
 console.log(androidVersionName);

我收到此错误: 无法读取未定义的属性'androidVersionName' 我如何从这个jsonObject解析数据?

2 个答案:

答案 0 :(得分:2)

基于您的对象的有效JSON:

{
    "androidVersionName":"2.3.3",
    "androidVersionId":10,
    "androidId":"fa0bef4b5a48eacb",
    "mobileModel":"sdk",
    "mobileManufacturer":"unknown",
    "mobileId":"GRI34",
    "mobileProduct":"sdk",
    "applicationName":"com.example.socketclient",
    "applicationVersionName":1.0,
    "applicationVersionCode":1,
    "applicationState":"INACTIVE",
    "screenWidth":480,
    "screenHeight":480,
    "screenDensity":240, 
    "screenDensityName":"hdpi",
    "atdPackages":"com.atd.panberes(1)"
}

答案 1 :(得分:2)

您无法将其解析为对象。但是你可以将它转换为JS对象。

var data = "[androidVersionName=2.3.3, androidVersionId=10, androidId=fa0bef4b5a48eacb, mobileModel=sdk, mobileManufacturer=unknown, mobileId=GRI34, mobileProduct=sdk, applicationName=com.example.socketclient, applicationVersionName=1.0, applicationVersionCode=1, applicationState=INACTIVE, screenWidth=480, screenHeight=480, screenDensity=240, screenDensityName=hdpi, atdPackages=com.atd.panberes(1)]";
var result = {};
data.replace(/(\w+)=(\w+)/g, function(_, left, right) { result[left] = right; })
console.log(result);