Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

    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.

Note: It has nothing to do with JSON.parse or $._parseJSON functions. I need to use the angular one for some reasons.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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

  '[{"title":"hi"}, {"title":"what"}]'

See doc

Property names must be double-quoted strings; trailing commas are forbidden.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.