0

I have a json object i want to get data from it! here is my json object

 "[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)]"

and here is my code :

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

and i get this error : Cannot read property 'androidVersionName' of undefined how can i parse data from this jsonObject?

2
  • Your JSON object isn't actually valid JSON, if it is supposed to be object and not string. Commented Feb 5, 2014 at 13:57
  • how can i parse this kind of object? Commented Feb 5, 2014 at 14:01

2 Answers 2

2

A valid JSON based on your object:

{
    "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)"
}
Sign up to request clarification or add additional context in comments.

7 Comments

i have changed my object to exactly what you said! now how can i parse data from it?
JSON.parse(data); should do it. If you want to be sure that everything is ok, you can use one of those online parser: json.parser.online.fr
Note also that you can't access a JSON property with "rowset". In your example, it will be "json.androidVersionName".
look at my code and output. code : console.log(data); var json = JSON.parse(data); console.log(json.toString()); var androidVersionName = data.androidVersionName; console.log(androidVersionName);
the object you have to use after the parsing is "json". Not "data". Therefore, the correct way to get AndroidVersionId is: var androidVersionName = json.androidVersionName;
|
2

You can't parse it to object. But you can transform it to JS object.

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.