Convert JSON string to array of JSON objects in Javascript

前端 未结 6 1677
感情败类
感情败类 2020-11-27 12:14

I would like to convert this string

{\"id\":1,\"name\":\"Test1\"},{\"id\":2,\"name\":\"Test2\"}

to array of 2 JSON objects. How should I do

6条回答
  •  孤城傲影
    2020-11-27 12:57

    Append extra an [ and ] to the beginning and end of the string. This will make it an array. Then use eval() or some safe JSON serializer to serialize the string and make it a real JavaScript datatype.

    You should use https://github.com/douglascrockford/JSON-js instead of eval(). eval is only if you're doing some quick debugging/testing.

提交回复
热议问题