简体   繁体   中英

Not able to parse the JSON object returned from servlet in AJAX

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()+ "}";


response.setContentType("application/json");
                response.getWriter().write(webVisitorDetails);

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"
});

you forgot the comma's and you should quote your values

String  webVisitorDetails = "{
    \"companyName\": \"" + webVisitor.getCompanyName() + "\",
    \"address\": \"" + webVisitor.getProfessionalAddress() + "\",
    \"city\": \"" + webVisitor.getCity() + "\",
    \"zipCode\": \"" + webVisitor.getZipCode() + "\",
    \"clientId\": \"" + webVisitor.getCustomerAccountNumber() + "\"
}";

EDIT: indeed => quote your keys to (just in case) I'm not a real java expert but yes if there is a class for that use it.

Also... You should not reinvent the wheel. Java has a perfect way of creating "working" JSON.

  Map obj=new LinkedHashMap();
  obj.put("name","foo");
  obj.put("num",new Integer(100));
  obj.put("balance",new Double(1000.21));
  obj.put("is_vip",new Boolean(true));
  obj.put("nickname",null);
  String jsonText = JSONValue.toJSONString(obj);
  System.out.print(jsonText);

  // Result: {"name":"foo","num":100,"balance":1000.21,"is_vip":true,"nickname":null}

将您的键和值放在引号中,并用逗号分隔成对的对!

Apart from the fact that you JSoN is incorrect (see other answers), you are looping through a response object whereas you should loop through data object that you have used as parameter of success function

In addition to that, in order to loop through data, you must return an array from server ie enclose your json object in square brackets ( [ ] ).

You can see from VDP's answer, your json is not formed correctly. You need to have a comma after each parameter.

A few more pointers which might be helpful:

  • You can validate if your json is correctly formed or not by pasting it at http://jsonlint.com/

  • If you are doing a good amount of json processing in your application, I would recommend using a standard JSON library such as json-simple or gson .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM