I am not able to parse tje Json object returned from the servlet in ajax,
I need to put json object values in there relative field
From my java code i am sending the below String in the form of JSON
String webVisitorDetails = "{"+"companyName : \""+webVisitor.getCompanyName()+ "\","+
"address : \""+webVisitor.getProfessionalAddress()+ "\","+
"city : \""+webVisitor.getCity()+ "\","+
"zipCode : \""+webVisitor.getZipCode()+ "\","+
"clientId : \""+webVisitor.getCustomerAccountNumber()+ "\"}";
In ajax
$.ajax({
url: "ships",
data: {
email: email.toString()
},
success: function(data) {
$.each(data, function(k, v) {
console.log(k + " Value " + v);
$("#city").text(v.city);
$("#zipcode").text(v.getZipCode);
$("#Adress").text(v.getProfessionalAddress);
});
},
error: function(data) {
console.log("error:", data);
},
type: "post",
datatype:"json",
});
Note that the jQuery setting is
dataTypewith a capital T. To do the JSON parsing manually, use theparseJSONfunction. However, if you set theContent-Typeof your servlet response toapplication/json, the datatype should be auto-detected.After you fixed this: Does it work? What is the value of the
dataargument of yoursuccesshandler?As Neal already said, JSON parsing expects valid JSON strings starting with jQuery 1.4. You can validate your JSON jsonlint.com.
To avoid the manual building of JSON strings, use something like the JSON-java processor (from iNan’s comment) or other Java implementations listed on json.org.