0

Hello i want a json object without 2 quotes the current output is not valid. How i can make this ?

I want exactly this:

{"format":{"id":"ivf","author":"","title":"","copyright":"","comment":"","album":"","year":"","track":"","genre":""}}

This is the Bad Output not Valid Json:

{"format":"{"id":"ivf","author":"","title":"","copyright":"","comment":"","album":"","year":"","track":"","genre":""}"}

video_format_serialized Data:

{"id":"ivf","author":"","title":"","copyright":"","comment":"","album":"","year":"","track":"","genre":""}

video_format_serialized = JSON.stringify($('#addprofile_FORM_video_format').serializeObject());
alert(video_format_serialized);
var format_object = {
    format:video_format_serialized
};
console.log(format_object);
var string = JSON.stringify(format_object);
tring.substring(1);
string.substring(0, string.length-1);
console.log(string);
string = string.replace(/\\/g, "");
console.log(string);

UPDATE Here is the right code to make valid output.

var format_object = {
    format:$('#addprofile_FORM_video_format').serializeObject()
};
console.log(format_object);
var string = JSON.stringify(format_object);
string.substring(1);
string.substring(0, string.length-1);
console.log(string);
string = string.replace(/\\/g, "");
console.log(string);
2
  • im not sure what the question is...do you need to convert the second to the first? Commented Jan 27, 2012 at 14:00
  • on the second bad output json object you find 2 double quotes that need to be removed but JSON.stringify does nothing on the output Commented Jan 27, 2012 at 14:02

1 Answer 1

1

You should use JSON.stringify on an object literal (not on a string, which is what I think you are doing) to create json -- that way you know it will be valid. For example, if you have

var toConvert = {format: {id: "ivf", "author": ""}};

and you type

JSON.stringify(toConvert);

you will get

"{"format":{"id":"ivf","author":""}}"

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

3 Comments

i need without first quote and end quote
my point is if you use stringify on an object literal, you don't need to worry ...
Note that this isn't supported without a shim in IE7-

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.