JSON string to JS object

后端 未结 5 1025
情歌与酒
情歌与酒 2020-11-30 01:16

I am using a JS object to create graphs with Google visualization. I am trying to design the data source. At first, I created a JS object client-side.

var          


        
5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 01:33

    the string in your question is not a valid json string. From json.org website:

    JSON is built on two structures:

    * A collection of name/value pairs. In various languages, this is 
      realized as an object, record, struct, dictionary, hash table, keyed list, or
      associative array.
    * An ordered list of values. In most languages, this is realized as an
      array, vector, list, or sequence.
    

    Basically a json string will always start with either { or [.

    Then as @Andy E and @Cryo said you can parse the string with json2.js or some other libraries.

    IMHO you should avoid eval because it will any javascript program, so you might incur in security issues.

提交回复
热议问题