Unable to access JSON property with “-” dash

后端 未结 3 1257
小鲜肉
小鲜肉 2020-11-22 08:02

I am unable to retrieve a value from a json object when the string has a dash character:

{
\"profile-id\":1234, \"user_id\":6789
}

If I try

3条回答
  •  感动是毒
    2020-11-22 08:42

    In addition to this answer, note that in Node.js if you access JSON with the array syntax [] all nested JSON keys should follow that syntax

    This is the wrong way

    json.first.second.third['comment']
    

    and will will give you the 'undefined' error.

    This is the correct way

    json['first']['second']['third']['comment'] 
    

提交回复
热议问题