parse json jquery
this perhaps a simple question for some of you here. But i just don't how to do this and I need help BADLY. Here's my json:
{"name":"cust_num","comparison":"starts_with","value":"01"},
{"name":"cust_name","comparison":"starts_with","value":"ad"},
{"name":"cust_age","comparison":"=","value":"20"}
my program JSON alerts like this:
{
"ID":"TBsKI7",
"dataType":"data",
"filters":[
"{\"name\":\"cust_num\",\"comparison\":\"starts_with\",\"value\":\"01\"}",
"{\"name\":\"cust_name\",\"comparison\":\"starts_with\",\"value\":\"ad\"}",
"{\"name\":\"cust_age\",\"comparison\":\"=\",\"value\":\"20\"}
],
"recordLimit":50,
"recordOffset":0,
.
.
.
}
this is obviously an error. It seems to me that the first json was stringified twice (if I am right with using the term). Now, what i want is how to correct my first json, or how to parse it, so that it will开发者_Go百科 output with no '\'.
i already tried using jQuery.parseJson() but it returns null.
thank you for reading.
EDIT: This is my javascript code that produces the above json:
$('#table tr').each(function(){
var td = '';
if ($(this).find("td:first").length > 0) { // used to skip table header (if there is)
$(this).find('option:selected').each(function(){
td = td + $(this).text()+ ',';
});
td = td + $(this).find('input').val();
filt[ctr]=td;
ctr += 1;
}
});
for (var i = 0;i<filt.length;i++) {
b = filt[i].split(',');
if (b[0] == "no"){
b[0] = "cust_num";
}
if (b[0] == "name"){
b[0] = "cust_name";
}
if (b[0] == "age"){
b[0] = "cust_age";
}
jsonobj.name = b[0];
jsonobj.comparison = b[1];
jsonobj.value = b[2];
c[i] = JSON.stringify(jsonobj); //if json.stringify, it will display the json with '\'
//if not, its only the last filter will be read for all the tr.
}
i have a dynamic table that will accept data that i will used to create json.
I hope you are defining your vars somewhere. Outside of the for loop, you should be doing
var c = [];
Then try
c.push({
name: b[0],
comparison: b[1],
value: b[2]
});
instead of
jsonobj.name = b[0];
jsonobj.comparison = b[1];
jsonobj.value = b[2];
c[i] = JSON.stringify(jsonobj);
and then you should be able to do
JSON.stringify(c);
精彩评论