简体   繁体   中英

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 output with no '\\'.

i already tried using jQuery.parseJson() but it returns null.

thank you for reading.

: This is my javascript code that produces the above json: :这是产生上述json的我的JavaScript代码:

$('#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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM