Filling a collection from a json file

I want to populate a collection from a json file that has data other than an array of data elements. I found out that the parse function in the collection should be used to return an array of data elements, but my collection is not populated from the json file. When I saved a breakpoint in the parsing method in firebug, the control does not come there.

This is my code.

var m1 = Backbone.Model.extend({ title:'Title1', tag:'html', date: 'Today' }) cll = Backbone.Collection.extend({ url:'/combodata.json?uu', model:m1, parse:function(res){ return res.items; } }); ci = new cll(); ci.fetch(); 

The json answer will be like this:

 { 'identifier': 'title', items:[ {title:'A', tag:"htmlcss", date:'today'}, {title:'AA', tag:"htmlcss", date:'today'}, {title:'B', tag:"htmlcss", date:'today'}, {title:'C', tag:"htmlcss1", date:'today'} ]} 

Please indicate to me where I am mistaken.

+7
source share
1 answer

Not an expert in JSON, but I tried to check your JSON at jsonlint.com . It does not work on line 2

 Parse error on line 1: { 'identifier': 'title -----^ Expecting 'STRING', '}' 

Please confirm if your JSON is valid or not.

Just checked out another site for your JSON check http://jsonformatter.curiousconcept.com/ , and it seems that string identifiers should be used with double quotes instead of single quotes. Perhaps replacing single quotes with double quotes will help.

+8
source

All Articles