简体   繁体   中英

Angular Json Property Undefined

I'm getting a Json object which contains other json object as properties .

[
    {
     "prop": "\{"p" : "1\"}"
    }
]

I'm parsing it this way :

 this.Auth.process().subscribe((x: any[]) => {

        console.log(JSON.parse(x[0].prop)); /// Works

         console.log('res: ' + JSON.parse(x[0].prop).p); /// Undefined.
        });

when i try to retreive the json property i get undefined as result .

JSON.parse(x[0].prop).p is undefined because value of x[0].prop is string and not a JSON.

You can parse x[0] first and then fetch the value of p from the derived object -

JSON.parse(x)[0].prop

Your JSON is invalid. Try removing the quotes like this example.

[
    {
     "prop": {"p" : "1"}
    }
]

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