I am using angular's fromJson function to parse a json string. If the json is a simple array, like "[1, 2]", then the following code is working. But what I need is an array of dictionaries.
我正在使用angular的fromJson函数来解析json字符串。如果json是一个简单的数组,如“[1,2]”,则以下代码正常工作。但我需要的是一系列字典。
var str = "[{'title':'hi'}, {'title':'what'}]"
alert(str) //1
alert(str.length) //2
var j = angular.fromJson(str)
alert(j) //3
alert(j.length) //4
1 and 2 are alerted out, which are string representation 3 and 4 are not, which means fromJson has error.
1和2被警告,这是字符串表示3和4不是,这意味着fromJson有错误。
Note: It has nothing to do with JSON.parse or $._parseJSON functions. I need to use the angular one for some reasons.
注意:它与JSON.parse或$ ._ parseJSON函数无关。出于某些原因,我需要使用角度的。
1 个解决方案
#1
1
That is because you have invalid JSON. For json to be valid you must wrap properties in double quotes not single quotes. So try using
那是因为你有无效的JSON。要使json有效,必须用双引号括起属性而不是单引号。所以尝试使用
'[{"title":"hi"}, {"title":"what"}]'
See doc
Property names must be double-quoted strings; trailing commas are forbidden.
属性名称必须是双引号字符串;尾随逗号是禁止的。
#1
1
That is because you have invalid JSON. For json to be valid you must wrap properties in double quotes not single quotes. So try using
那是因为你有无效的JSON。要使json有效,必须用双引号括起属性而不是单引号。所以尝试使用
'[{"title":"hi"}, {"title":"what"}]'
See doc
Property names must be double-quoted strings; trailing commas are forbidden.
属性名称必须是双引号字符串;尾随逗号是禁止的。