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!

推荐答案

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

定义一个类似于JSON的Java类.

e.g. this 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
}

现在,如果你有这样一个控制器方法:

@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.

Json相关问答推荐

try 将文本标记放置在条形图上的同一高度

Swift解码错误类型与`Bool`type不一致

使用动态和可变JSON的图表多条

在Jolt中将具有嵌套元素的对象数组转换为单独的对象列表

VSCode 为 python 文件添加标尺,但不为 C 文件添加标尺.为什么?

未知的META规范,无法验证.[规范v1.0.1]

如何将具有相同 struct 的多个JSON文件中的对象数组合并成一个数组?

ETCD 导出为 json 并从 base64 解码所有键/值到人类可读

如何 Select 一个值,这是可选的 - 使用 jq

如何在 Dart 中与多个 map (字典)相交

如何将从嵌套 Select 返回的空值转换为空数组?

如何为包含一些固定值并可能具有其他附加值的数组字符串创建数组 json 架构

如何使用 gson 调用默认反序列化

如何从 HttpClient 解析 JSON 字符串?

使用 c# 通用地展平 Json

数据包含连续问号时无法理解的 jQuery $.ajax() 行为

有什么方法可以在 elasticsearch 服务器中导入 json 文件(包含 100 个文档).?

使用 jq 如何用其他名称替换键的名称

如何使用 Json.NET 反序列化可以是两种不同数据类型的 JSON 属性

如何从 github API 解析链接头