简体   繁体   中英

Accessing data from json in Angularjs

I have a JSON object Like this-

[
{
    "user": "A220",
    "shorttext": "shanghai",
    "reportedBy": "S,A",
    "questions": "

[{\"question\":\"Q1\",\"is_mand\":\"0\",\"type\":\"text\",\"answer\":\"w\",\"ansYesOrNo\":false,\"ansDetails\":\"\"},{\"question\":\"Q2\",\"is_mand\":\"0\",\"type\":\"text\",\"answer\":\"ed\",\"ansYesOrNo\":false,\"ansDetails\":\"\"}]",
        "notifno": "20143995",
        "error": "",
        "createdOn": "2015-09-09 13:08:36",
        "Id": 0,
        "$$hashKey": "object:89"
    }
]

I need to access the 1st question of questions .Please i am not able to access it like this alert(obj.questions[0].question); Here is a jsFiddle Link- LINK to Fiddle

The main problem is that the questions array inside the object, It is not an array. Is a Array convert to string, you must parse the questions to get the respective Json object with the data.

Here is your fiddle updated: http://jsfiddle.net/marduke182/w02ck9uw/1/

And the part of code important:

var questonObj = JSON.parse($scope.a[0].questions);

通过这种方式使用fromJson方法:

angular.fromJson(a[0].questions)[0].question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM