使用 Jackson JSON 在 Spring MVC 中解析 JSON

发布于 2024-11-07 05:22:05 字数 323 浏览 0 评论 0原文

好吧,我已经研究这个有一段时间了,不再继续了。我有一个 Spring MVC servlet,我需要从 JavaScript 前端 Web 应用程序接受 JSON。为了解析 JSON,我需要使用 Jackson。我需要获取 JSON 中的值并按照它们在 JSON 中出现的顺序将它们存储到列表中。我尝试过将 JsonFactory 与 JsonParser 和 JsonNode 对象一起使用,但完全可以让它工作。我还尝试打开 BufferedReader 并逐行迭代请求正文,但也无法完全理解这一点。我在这里查看了几个相关的问题,但到目前为止没有一个对我有用。

请知情人士为我指出正确的方向,带有示例的网页会很棒!

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. To parse the JSON I need to use Jackson. I need to take the values within the JSON and store them into a List in the order they appear in the JSON. I've tried using the JsonFactory with the JsonParser and JsonNode objects but can quite get it to work. I've also tried to just open a BufferedReader and iterate through the request body line by line but again can't quite get this either. I've looked at a couple of related questions on here, but none so far have worked for me.

Could anyone in the know point me in the right direction here please, a web page with an example would be great!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆梦 2024-11-14 05:22:05

使用像 Jackson 这样的映射技术的全部意义在于您可以使用对象(您不必自己解析 JSON)。

定义一个类似于您期望的 JSON 的 Java 类。

例如这个 JSON:

{
"foo" : ["abc","one","two","three"],
"bar" : "true",
"baz" : 1
}

可以映射到这个类:

public class Fizzle{
    private List<String> foo;
    private boolean bar;
    private int baz;
    // getters and setters omitted
}

现在如果你有一个像这样的 Controller 方法:

@RequestMapping("somepath")
@ResponseBody
public Fozzle doSomeThing(@RequestBody Fizzle input){
    return new Fozzle(input);
}

并且你从上面传入 JSON,Jackson 会自动为你创建一个 Fizzle 对象,并且它将序列化返回的 JSON 视图对象输出为 MIME 类型 application/json 的响应。

有关完整的工作示例请参阅前面的内容我的回答

The whole point of using a mapping technology like Jackson is that you can use Objects (you don't have to parse the JSON yourself).

Define a Java class that resembles the JSON you will be expecting.

e.g. this JSON:

{
"foo" : ["abc","one","two","three"],
"bar" : "true",
"baz" : 1
}

could be mapped to this class:

public class Fizzle{
    private List<String> foo;
    private boolean bar;
    private int baz;
    // getters and setters omitted
}

Now if you have a Controller method like this:

@RequestMapping("somepath")
@ResponseBody
public Fozzle doSomeThing(@RequestBody Fizzle input){
    return new Fozzle(input);
}

and you pass in the JSON from above, Jackson will automatically create a Fizzle object for you, and it will serialize a JSON view of the returned Object out to the response with mime type application/json.

For a full working example see this previous answer of mine.

嘿咻 2024-11-14 05:22:05

我正在使用 http://json-lib.sourceforge.net/
中的 json lib
json-lib-2.1-jdk15.jar

import net.sf.json.JSONObject;
...

public void send()
{
    //put attributes
    Map m = New HashMap();
    m.put("send_to","[email protected]");
    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");
    ...
}

更多示例请参见 http://json-lib.sourceforge.net /usage.html

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","[email protected]");
    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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文