Parsing JSON in Spring MVC using Jackson JSON

前端 未结 2 507
南方客
南方客 2020-11-28 21:44

Ok, so I\'ve been looking at this for a little while now and am no further on. I\'ve got a Spring MVC servlet that I need to accept JSON from a JavaScript front end web app

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 22:37

    I'm using json lib from http://json-lib.sourceforge.net/
    json-lib-2.1-jdk15.jar

    import net.sf.json.JSONObject;
    ...
    
    public void send()
    {
        //put attributes
        Map m = New HashMap();
        m.put("send_to","my@admin.lan");
        m.put("email_subject","this is a test email");
        m.put("email_content","test email content");
    
        //generate JSON Object
        JSONObject json = JSONObject.fromObject(content);
        String message = json.toString();
        ...
    }
    
    public void receive(String jsonMessage)
    {
        //parse attributes
        JSONObject json = JSONObject.fromObject(jsonMessage);
        String to = (String) json.get("send_to");
        String title = (String) json.get("email_subject");
        String content = (String) json.get("email_content");
        ...
    }
    

    More samples here http://json-lib.sourceforge.net/usage.html

提交回复
热议问题