json error: SyntaxError: JSON.parse: unexpected character
I have some problem. When I try post json object, I have error: error: SyntaxError: JSON.parse: unexpected character
My javascript object:
var $arr_data = {
title: '',
category: '',
about: '',
sex: 'unisex',
accessories: 'no',
quantity: []
};
I think that a problem are in this function:
function data_quantity($size_input,$quant_input)
{
var $string = "{color:'"+$pattern+"',size:'"+$size_input+"',quantity:'"+$quant_input+"'}";
$arr_data.quantity.push($string);
}
alert(JSON.stringify($arr_data));
returns the following string:
{
"title":"title_test",
"category":"3",
"about":"about_test",
"sex":"woman",
"accessories":"no",
"quantity":[
"{color:'none',size:'xxl',quantity:'5'}",
"{color:'black',size:'xxl',quantity:'1'}",
"{color:'white',size:'s',quantity:'9'}"
]
}
You should add an object instead of a JSON string to your array:
var $string = {
color: $pattern,
size: $size_input,
quantity: $quant_input
};
You are mixing javascript objects with a JSON string. With JSON.stringify
your object will then be converted to a string.
Also just as a sidenote, why are you adding the $
prefix to all your variables? This is javascript not php, so no need for that. You can maybe use it for marking jQuery objects but doesn't really make sense with normal variables.