java 如何在 Spring 控制器中获取 JSON 数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17545717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:17:15  来源:igfitidea点击:

how to get the JSON data in Spring controller?

javajsonspringjqueryspring-mvc

提问by Human Being

I know it is a simple one . But couldn't find a solution.

我知道这是一个简单的。但找不到解决办法。

My jQuery-ajax will be ,

我的 jQuery-ajax 将是,

var json = {"message":"Message123","time":"time123","name":"test123"}

data : JSON.stringify(json),

My Spring controller will be ,

我的 Spring 控制器将是,

@RequestMapping(value = "chat.html", method=RequestMethod.GET )
public @ResponseBody String getChat() {

System.out.println("Entered in to the controller ");

String name == ???
String msg == ???
String time == ???

//Process the functionality using the msg,name,time 

    return "Json String";
}

How can I get the values of the name, message , time.

如何获取 name、message、time 的值。

Hope our stack members will help me.

希望我们的堆栈成员能帮助我。

回答by JaredYang

var json = {"message":"Message123","time":"time123","name":"test123"}
data : JSON.stringify(json) should have a key , 

data : {json:{"message":"Message123","time":"time123","name":"test123"}},
url:/json/test

Controller

控制器

@RequestMapping(value = {"json/test"},method = RequestMethod.GET)
    @ResponseBody
    public String jsonTest(String json){
       JSONObject jsonObject = JSONObject.fromObject(json);
        String m = jsonObject.get("message").toString();
        String t = jsonObject.get("time").toString();
        String n = jsonObject.get("name").toString();
    }

I use the net.sf.json.JSONObject

我用 net.sf.json.JSONObject

回答by Hitesh Kumar

You can use org.Json jar from this link...

您可以从此链接使用 org.Json jar ...

Then try this code, I have done is in my current project and is working fine and efficiently

然后试试这个代码,我已经完成了在我当前的项目中并且运行良好且高效

 var json = {"message":"Message123","time":"time123","name":"test123"}
  $.ajax({
    type: "POST",
    url: "/chat.html",
    data: "jsonObject="+json,
    success: function(response) {
       // your success code
    },
    error: function(e) {
        // your error code
    }
});

In controller change your code like this

在控制器中像这样改变你的代码

@RequestMapping(value = "/chat.html", method=RequestMethod.POST )
public @ResponseBody String getChat(HttpServletRequest req,HttpServletResponse res) {
    JSONObject jsonObject = null;
    try {
        jsonObject = new JSONObject(req.getParameter("jsonObject"));
    } catch(JSONException _instance) {
        // Exception Handle Message
    }

    System.out.println("Entered in to the controller ");
    String name ="" , msg = "", time = "";
    if(jsonObject.has("name")) {
       name = jsonObject.getString("name");
    }

    ... // Do it for other variables also

    //Process the functionality using the msg,name,time 

    return "Json String";
}