简体   繁体   English

如何将JSon对象转换为Java对象?

[英]How to convert JSon Object to Java Object?

Can Anyone help me on this, im trying to convert complex json object send through ajax into a object. 有人可以帮我吗,我试图将通过ajax发送的复杂json对象转换为对象。 so that i can use this object to pass into my model. 这样我就可以使用该对象传递给我的模型。

The JSP code is: JSP代码是:

function callRemovefilter()
{
    var jsonObjects = {
        address1: "Address_1",
        city: "City",
        pin: "PIN"
        };
    var jsonObjects2 = {
        locality:"Loc1",
        shippingType:"Regular",
        shippingCost:20
    };

    var cust= JSON.stringify(jsonObjects);
    var sales=JSON.stringify(jsonObjects2);
    jQuery.ajax({
              url: "http://localhost:8080/OnlineStore/kmsg/grocery/SaveSalesOrder",
              type: "GET",
              data: {CustomerInfo:cust,SalesModel:sales},
              dataType: "json",
              beforeSend: function(x) {
                if (x && x.overrideMimeType) {
                  x.overrideMimeType("application/j-son;charset=UTF-8");
                }
              },
              success: function(result) {
             //Write your code here
              }
    });
}

// The controller code is //控制器代码为

  @RequestMapping(value = "/SaveSalesOrder", method = RequestMethod.GET)
            @ResponseStatus(value=HttpStatus.OK)
            public @ResponseBody String SaveCustomerOrder(@RequestParam Map<String,String> requestParams) throws Exception 
            {       
                ObjectMapper objectMapper = new ObjectMapper();
                SalesCommandObject salesCommandObject= new SalesCommandObject();
                salesCommandObject = objectMapper.readValue(body, SalesCommandObject .class);


                return "Success";
            }

// Code of JSP to send object to controller //将对象发送到控制器的JSP代码

var salesCommandObject = {};                      salesCommandObject.CustomerInfo =
            {
            "address1": "Address_1",
            "city": "City",
            "pin": "PIN"
            };
salesCommandObject.SalesModel = 
            {
            "locality":'Loc1',
            "shippingType":'Regular',
            "shippingCost":20
            };

           $.ajax
           ({
              type: "POST",
              dataType : 'json',
              async : true,     
              url: "http://localhost:8080/OnlineStore/kmsg/grocery/SaveSalesOrder",
              data : JSON.stringify(salesCommandObject),
              }).done(function(data,type,xml)
                        {
                                                  console.log(data);
                        }).fail(function()
                                  {
                            alert("Something Bad Happened, Service failed");
                      })

Send objects, not jsonstrigs. 发送对象,而不是jsonstrigs。 And in controller in your method SaveCustomerOrder get an object, not Map, like: 并在您的方法SaveCustomerOrder的控制器中获取一个对象,而不是Map,例如:

@RequestMapping(value = "/SaveSalesOrder", method = RequestMethod.GET) @ResponseStatus(value=HttpStatus.OK) public @ResponseBody String SaveCustomerOrder(@RequestParam CustomerInfo ci, @RequestParam SalesModel sm) throws Exception {
//your logic here return "Success"; }
@RequestMapping(value = "/SaveSalesOrder", method = RequestMethod.GET) @ResponseStatus(value=HttpStatus.OK) public @ResponseBody String SaveCustomerOrder(@RequestParam CustomerInfo ci, @RequestParam SalesModel sm) throws Exception {
//your logic here return "Success"; }
And add getters and setters to appropriate classes(ie CustomerInfo, SalesModel) like:
@RequestMapping(value = "/SaveSalesOrder", method = RequestMethod.GET) @ResponseStatus(value=HttpStatus.OK) public @ResponseBody String SaveCustomerOrder(@RequestParam CustomerInfo ci, @RequestParam SalesModel sm) throws Exception {
//your logic here return "Success"; }
并添加getter和setter方法,以适当的类(即CustomerInfo,SalesModel),如:

`public class SalesModel{
    private String sale_id;//or whatever field or property you need
    public String getSale_Id() {
        return sale_id;
    }
    public void setSale_Id(String si) {
        this.sale_id = si;
    }

}`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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