简体   繁体   中英

How can I accesss to the elements of array objects?

How Can I get data from this json data?

I get this data from the server, and it sends some database's datas by using sequelize.

Data what I want to access is

{result: "[{"userid":"a","speed":10},{"userid":"b","speed":20},{"userid":"c","speed":30}]"}]" }

And I tried console.log(result[0].userid);

But, I only got errer like this. ' Uncaught TypeError: Cannot read property 'userid' of undefined' .

Could you tell me what I have to fix?

You gave an invalid JSON, the json should be something like this:

Formatted JSON Data

{
  "result":[
    {
      "userid":"a",
      "speed":10
    },
    {
      "userid":"b",
      "speed":20
    },
    {
      "userid":"c",
      "speed":30
    }
  ]
}

And after that you can access it:

 const json = { "result":[ { "userid":"a", "speed":10 }, { "userid":"b", "speed":20 }, { "userid":"c", "speed":30 } ] }; console.log(json.result[0].userid);

Your JSON is badly formatted.

Try this instead...

{"result":[{"userid":"a","speed":10},{"userid":"b","speed":20},{"userid":"c","speed":30}]}

The main issues were...

{result: "[{"userid"

Should be...

{"result":[{"userid"

And

"speed":30}]"}]" }

Should be...

"speed":30}]}

Please note, the whitespace between the items makes no difference, but I have remove to reduce the size of the JSON string

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