Reputation: 2471
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",
});
Upvotes: 1
Views: 1377
Reputation: 1647
If it's creating a problem because of the single-inverted comma ('), then just do:
jQuery.parseJSON(data.replace(/'/g, '"'))
If that is the case then it should work for you...
Upvotes: 0
Reputation: 8052
Note that the jQuery setting is dataType
with a capital T. To do the JSON parsing manually, use the parseJSON
function. However, if you set the Content-Type
of your servlet response to application/json
, the datatype should be auto-detected.
After you fixed this: Does it work? What is the value of the data
argument of your success
handler?
console.debug(data);
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.
Upvotes: 2
Reputation: 146310
You json string is incorrect
The keys must be surrounded by double quotes.
Upvotes: 0