Reputation: 11
Here is the JSON String I have. I have removed the opening and closing brackets from the JSON because I need to use the JSON values in jQuery to actually load the data in a select box:
{
"text": "Pediatric FA, CPR & AED (2015)",
"id": "128177000002431552~Pediatric FA, CPR & AED DVD Set (2015)~YES~117.19"
}, {
"text": "FA, CPR & AED Manual (2015)",
"id": "128177000002431564~FA, CPR & AED Manual (2015)~YES~17.73"
}
here is my Javascript Code
$.post("items.cfm",{"term":request.term})
.done(function(data){
try{
var obj = JSON.parse(data),
values = [];
$.each(data, function(i, obj) {
values.push({"label":obj[x].text, "value":obj[x].id, "price":obj[x].id.split('~')[3]});
})
response(values);
}catch(e){
alert(e);
}
})
.fail(function(e){
alert(e);
});
every time I run it I am getting the error
SyntaxError: Unexpected token , in JSON at position
Upvotes: 1
Views: 1611
Reputation: 156728
You have two JSON objects separated by a comma. If you meant this to be an array, you need to surround it with array brackets:
[
{
"text": "Pediatric FA, CPR & AED (2015)",
"id": "128177000002431552~Pediatric FA, CPR & AED DVD Set (2015)~YES~117.19"
}, {
"text": "FA, CPR & AED Manual (2015)",
"id": "128177000002431564~FA, CPR & AED Manual (2015)~YES~17.73"
}
]
As a side note, if your server is generating invalid JSON, you should take a good look at the code that's producing the JSON. Chances are, there's code trying to generate this string by hand, which is a bad practice. The server should be using a library to convert the returned value into JSON.
Upvotes: 3