0

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 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.

2
  • That's not your entire JSON. It seems that you're nesting objects.. Can we have the entire code snippet please? Commented May 10, 2011 at 7:43
  • I think your first problem is whatever is generating that JSON. Rather than try and hack to fix it, I would try and make sure its correct in the first place. Where does the JSON come from? Commented May 10, 2011 at 7:49

1 Answer 1

3

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Hey, thanks... you save me today. that was a a very good help. it's working now.
@jayAnn: you are welcome. as a side note, it would be worth running your javascript through jslint.com :)

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.