1

JSONのようなデータがあるとします。

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

JSONでデータを送信できます

var jsonData = JSON.Stringfy(json);

ではJQueryAjax

data = jsonData ,

スプリング コントローラーで 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
    }
}

複数の人の詳細を同じように送信する必要がある場合JSON、 のを解析するにはどうすればよいですか?Spring controllerJSON

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

スタック メンバーが適切な解決策を提供してくれることを願っています。

4

3 に答える 3

3

これはうまくいくはずです:

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

--編集および追加された例--

私はこれをローカルでテストしましたが、うまくいきました。コード スニペットは次のとおりです。

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

テスト結果は次のとおりです(curlでテストしました):

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

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

PS。JSON リクエストがコントローラーに到達する前に失敗すると、Spring MVC でデバッグ情報を取得するのが難しい場合があります。デバッグ情報を取得するには、Spring MVC のデバッグ レベルを trace に設定する必要がある場合があります。JSON リクエストが失敗した理由を確認する必要がある場合は、通常、これを log4j.properties に追加します。

log4j.logger.org.springframework.web.servlet.mvc.method.annotation=TRACE
于 2013-07-12T15:45:00.143 に答える
0

このコードを試してください

@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
    }
}
于 2013-11-26T13:18:49.947 に答える
0

Json 配列内の JsonObject で各メンバーの詳細を送信し、配列を反復処理して個々の JSON オブジェクトを取得できます。データの取得と設定に使用できるすべてのメソッドについては、JSON のドキュメントを参照してください。

また、メモリに優しい GSON (google -json) を使用することをお勧めします。:)

于 2013-07-12T11:32:50.160 に答える