The following sentence will directly parse the JSON string returned by the controller directly into a JSON object, without having to parse it two times.
$.ajax ({
Type: ' Post ',
dataType: ' JSON ',
Url:ctx + '/login/purchaselist?memberid= ' + MemberID,
Success:function (data) {
if (0 = = data.status) {
Alert (' No data! ‘);
} else {
var purchasearr = Data.jsonvalue;
for (var i = 0; i < purchasearr.length; i + +) {
alert (purchasearr[i].product_name);
}
}
}
});
@ResponseBody
@RequestMapping ("/purchaselist")
Public String purchaselist (Modelandview mav, long MemberID) {
String purchaseliststr = rdubbopropurchaseservice.findmemberpurchase (MemberID);
return purchaseliststr;
}
1) The first method
Data is a JSON string
var dataobj=eval ("(" +data+ ")");//Convert to JSON object
var jsonobj = eval (' (' + data + ') ');
var result = Jsonobj.result;
var MemberID = Jsonobj.memberid;
2) The second method
Parsing is done using a function object, the typical application of which is the success of the Ajax method in jquery and the parsing of the returned data.
var json= ' {' name ': ' CJ ', ' Age ': 18} ';
data = (New Function ("", "Return" +json)) ();
The data at this point is the one that will parse into a JSON object.
JS parsing JSON string as an object