java 将 JSON 发送并解析到 spring 控制器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17613927/
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:28:14  来源:igfitidea点击:

Send and parse the JSON to spring controller?

javajsonspringjqueryspring-mvc

提问by Human Being

Suppose , If I have the JSONdata like ,

假设,如果我有这样的JSON数据,

var json = {"name":"kite Player","age":"25","hobby":"footbal"}

I can Send the JSONdata by

我可以JSON通过以下方式发送数据

var jsonData = JSON.Stringfy(json);

In JQueryAjax,

JQueryAjax,

data = jsonData ,

I can parse the JSON data in the spring controller by,

我可以通过以下方式解析弹簧控制器中的 JSON 数据,

public class TestController {
    @RequestMapping(method = RequestMethod.POST, value = "personDetails.html")
    public @ResponseBody Result math(@RequestBody final Persons persons) {

         String name = person.getName();
         String age = persons.getAge();
         String hobby = persons.getHobby();
         // Other process
    }
}

How can I parse the JSONin the Spring controller, If I need to send multiple person details in JSONlike ,

我怎么可以解析JSONSpring controller,如果我需要在发送多个细节的人JSON一样,

var json = [ {"name":"kite Player","age":"25","hobby":"footbal"},  
             {"name":"Steve","age":"40","hobby":"fishing"},
             {"name":"Marker","age":"28","hobby":"cricket"}
           ]

Hope our stack members will give a good solution.

希望我们的堆栈成员会给出一个好的解决方案。

回答by Kre?imir Nesek

This should work:

这应该有效:

@RequestMapping(method = RequestMethod.POST, value = "personDetails.html")
public @ResponseBody Result math(@RequestBody List<Persons> personList) { ... }

--EDITED AND ADDED EXAMPLE--

--编辑和添加示例--

I've tested this locally and it works for me. Here's code snippet:

我已经在本地进行了测试,它对我有用。这是代码片段:

public class TestController {
    public static class Test {                                                    
        String name;                                                                 

        public String getName() {                                                    
            return name;                                                                
        }                                                                            

        public void setName(String name) {                                           
            this.name = name;                                                           
        }                                                                            
    }                                                                             


    @RequestMapping(value = "/debug/test1.json", method = RequestMethod.POST)     
    @ResponseBody                                                                 
    public Test[] testList1(@RequestBody Test[] test) {                           
        return test;                                                                 
    }                                                                             

    @RequestMapping(value = "/debug/test2.json", method = RequestMethod.POST)     
    @ResponseBody                                                                 
    public List<Test> testList2(@RequestBody List<Test> test) {                   
        return test;                                                                 
    }                                                                     
}        

Here are the test results (I tested it with curl):

以下是测试结果(我用 curl 测试过):

    Request:
    curl --header "Content-type: application/json" --header "Accept: application/json"  --data '[{"name": "John"}, {"name": "Hyman"}]' http://localhost:8080/app/debug/test1.json
    Response:
    [{"name":"John"},{"name":"Hyman"}]

    Request:
    curl --header "Content-type: application/json" --header "Accept: application/json"  --data '[{"name": "John"}, {"name": "Hyman"}]' http://localhost:8080/app/debug/test2.json
    Response:
    [{"name":"John"},{"name":"Hyman"}]

PS. Somethime it's hard to get any debug information in spring MVC when JSON request fails before it reaches controller. To get the debug info, in some cases you need to set debug level of spring MVC to trace. I usually add this to my log4j.properties when I need to verify why a JSON request failed:

附注。有时,当 JSON 请求在到达控制器之前失败时,很难在 Spring MVC 中获得任何调试信息。要获取调试信息,在某些情况下,您需要设置 spring MVC 的调试级别以进行跟踪。当我需要验证 JSON 请求失败的原因时,我通常会将它添加到我的 log4j.properties 中:

log4j.logger.org.springframework.web.servlet.mvc.method.annotation=TRACE

回答by John

You can send the each member details in a JsonObject within a Json Array and then you can iterate through the array and get the individual JSON objects. You can see the documentation of JSON for all the available methods to get and set the data.

您可以在 Json 数组中的 JsonObject 中发送每个成员的详细信息,然后您可以遍历该数组并获取各个 JSON 对象。您可以查看 JSON 文档,了解获取和设置数据的所有可用方法。

Also I would suggest you to use GSON (google -json) they are Memory friendly. :)

另外我建议你使用 GSON (google -json) 它们是内存友好的。:)

回答by omjaijagdish

Try this code

试试这个代码

@RequestMapping(method = RequestMethod.POST, value = "personDetails.html")
public @ResponseBody Result math(@RequestBody List< Persons > persons) {
    for (Persons person : persons) {
        String name = person.getName();
        String age = person.getAge();
        String hobby = person.getHobby();
        // Process the data
    }
}