saminathan saminathan - 1 month ago 10
Ajax Question

How to parse the below JSON response

I am getting the below JSON response after a ajax call.

{"head":{"json":"{ 'rows': [ {'CustName' : 'JAMES', 'CustId' : 'Gans Communications'} , {'CustName' : 'JAMES', 'CustId' : 'GANNETTW'} , {'CustName' : 'JAMES', 'CustId' : 'GANNONCO'} , {'CustName' : 'JAMES', 'CustId' : 'GANSTIRE'} , {'CustName' : 'JAMES', 'CustId' : 'GANTABSW'} , {'CustName' : 'JAMES', 'CustId' : 'GANTONMI'} , {'CustName' : 'JAMES', 'CustId' : 'GANDALF'} , {'CustName' : 'JAMES', 'CustId' : 'GANDALF2'} , {'CustName' : 'JAMES', 'CustId' : 'GANDK'} , {'CustName' : 'JAMES', 'CustId' : 'GANDMTOW'} , {'CustName' : 'JAMES', 'CustId' : 'GANDOD'} , {'CustName' : 'JAMES', 'CustId' : 'GANDTOWI'} , {'CustName' : 'JAMES', 'CustId' : 'GANDY.LD'} , {'CustName' : 'JAMES', 'CustId' : 'GANE .LD'} , {'CustName' : 'JAMES', 'CustId' : 'GANICATL'} , {'CustName' : 'JAMES', 'CustId' : 'GANKEMA'} , {'CustName' : 'JAMES', 'CustId' : 'GANKEM4'} , {'CustName' : 'JAMES', 'CustId' : 'GANLYWAL'} , {'CustName' : 'JAMES', 'CustId' : 'GANN ETT'} , {'CustName' : 'JAMES', 'CustId' : 'GANNETTT'} ]}"},"body":null,"responseTime":null,"leftPanel":null}


I have used tried to take CustName and CustId to display in textbox using Jquery.But i couldnot able to fetch the values properly.

Below is my sample code:

var results_customer=data.head.json;
var new_val = JSON.stringify(results_customer);
var newresult = eval(results_customer);
alert(newresult.length);
console.log(newresult);


I am getting error with above values.

Could any please help me in taking the values? Thanks in advance

Answer

You have a bad encoded Json string in response. Json strings must be delimited by double quotes " and not with singles quotes '. So you have to convert it before using JSON.parse() method:

var response = {"head":{"json":"{ 'rows': [ {'CustName' : 'JAMES', 'CustId' : 'Gans Communications'} , {'CustName' : 'JAMES', 'CustId' : 'GANNETTW'} , {'CustName' : 'JAMES', 'CustId' : 'GANNONCO'} , {'CustName' : 'JAMES', 'CustId' : 'GANSTIRE'} , {'CustName' : 'JAMES', 'CustId' : 'GANTABSW'} , {'CustName' : 'JAMES', 'CustId' : 'GANTONMI'} , {'CustName' : 'JAMES', 'CustId' : 'GANDALF'} , {'CustName' : 'JAMES', 'CustId' : 'GANDALF2'} , {'CustName' : 'JAMES', 'CustId' : 'GANDK'} , {'CustName' : 'JAMES', 'CustId' : 'GANDMTOW'} , {'CustName' : 'JAMES', 'CustId' : 'GANDOD'} , {'CustName' : 'JAMES', 'CustId' : 'GANDTOWI'} , {'CustName' : 'JAMES', 'CustId' : 'GANDY.LD'} , {'CustName' : 'JAMES', 'CustId' : 'GANE .LD'} , {'CustName' : 'JAMES', 'CustId' : 'GANICATL'} , {'CustName' : 'JAMES', 'CustId' : 'GANKEMA'} , {'CustName' : 'JAMES', 'CustId' : 'GANKEM4'} , {'CustName' : 'JAMES', 'CustId' : 'GANLYWAL'} , {'CustName' : 'JAMES', 'CustId' : 'GANN ETT'} , {'CustName' : 'JAMES', 'CustId' : 'GANNETTT'} ]}"},"body":null,"responseTime":null,"leftPanel":null};

var json = response.head.json.replace(/'/g, '"');

var data = JSON.parse(json);
console.log(data);

Assuming your data does not contains singles quotes...

Comments