Reputation: 1866
I have following code for reading a JSON file.It is giving no error but i am getting null in the variable:
var myData = null;
$.ajax({
type: 'GET',
async: false,
url: 'myJson.json',
dataType: 'json',
success: function (r) {
myData = r;
}
});
Below is my JSON file:
{items:[{value:"1",name:"John"},{value:"2",name:"Henry"}]};
Upvotes: 2
Views: 1703
Reputation: 276306
JSON strings must be escaped. You're missing the "
s.
A correct JSON would be:
{"items":[{"value":"1","name":"John"},{"value":"2","name":"Henry"}]}
Even if I hadn't remembered or looked up the specific JSON rules, you can always produce the JSON from the JS variable assuming it's serializable (in your case it is) by:
var a =
and paste your object literalJSON.stringify(a)
;.json
file. JavaScript's JSON.stringify
produces valid JSON.Upvotes: 13