Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Spring控制器中获取JSON数据?_Java_Json_Spring_Jquery_Spring Mvc - Fatal编程技术网

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

Java 如何在Spring控制器中获取JSON数据?,java,json,spring,jquery,spring-mvc,Java,Json,Spring,Jquery,Spring Mvc,我知道这很简单。但找不到解决办法 我的jQuery ajax将是 var json = {"message":"Message123","time":"time123","name":"test123"} data : JSON.stringify(json), @RequestMapping(value = "chat.html", method=RequestMethod.GET ) public @ResponseBody String getChat() { System.out.

我知道这很简单。但找不到解决办法

我的jQuery ajax将是

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

data : JSON.stringify(json),
@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";
}
我的Spring控制器将是

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

data : JSON.stringify(json),
@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";
}
如何获取名称、消息和时间的值

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

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
控制器

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

我使用的是
net.sf.json.JSONObject

您可以从以下位置使用org.json jar

然后试试这段代码,我已经在我当前的项目中完成了,并且工作得很好,效率很高

 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
    }
});
在控制器中,像这样更改代码

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

@mthmulders可能重复,所以你是说
@RequestBody
将解决我的问题。对吗?我是说你的问题以前已经被问过了,并且得到了彻底的回答。使用
@RequestBody
确实是答案的一部分。但我不知道答案。所以只问了一个问题。对不起……没问题,很乐意帮忙。查看并了解更多有关提问和重复提问的信息。