0

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'}"
    ]
}
1
  • where the $pattern came from (in data_quantity function)? Commented Dec 25, 2013 at 21:14

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.