1

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",
});
3
  • Create JSON using org.json library. github.com/douglascrockford/JSON-java Commented Jul 13, 2012 at 12:45
  • Your string does not appear to be JSON (although it is hard to tell since you are showing us the Java that generates it and not the JSON text itself). As iNan says - don't build JSON by mashing together strings, use a proper serializer. Commented Jul 13, 2012 at 12:49
  • Good point. There is also Jackson, which can be used (inter alia) to map Java objects to JSON and vice versa. Might be too powerful for your use case but I do not know that. Commented Jul 13, 2012 at 13:55

3 Answers 3

2

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.

In jQuery 1.4 the JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. (See json.org for more information on proper JSON formatting.)

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.

Sign up to request clarification or add additional context in comments.

Comments

0

You json string is incorrect

The keys must be surrounded by double quotes.

Read the requirements here

3 Comments

Can you please help me how to write json string.
@KunalVashist See the website.
0

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...

1 Comment

Don't see where ' is used in the question. Generally, this kind of problem should be fixed server-side, so valid JSON is produced and served to the clients.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.