Java 中最小(更少的内部代码)、符合规范的 JSON 解析器
我见过Java中有很多JSON解析器。有些目标是速度,有些是目标精度,有些是目标灵活性(流、缓冲等)。有些使用集合 api 的特殊子类,而不是使用已有的子类。这对我来说似乎很臃肿。
所以我想知道,哪一个以最短/最简单的方式处理字符串到对象转换的工作? (如,更少的代码)
编辑:很抱歉我不清楚。我的意思是最小的解析器。因此,如果一个解析器有 600 行,另一个解析器有 400 行。我不在乎哪个更快,哪个更容易理解。不要将其与调用解析器所需的行数混淆。我预计几乎所有的都只是几行代码,如果我必须添加一个方便的方法也没关系。
目的更多的是教育性的事情,它是如何完成的,以及希望使用 API 来轻松了解它是如何工作的。
I have seen there are many JSON parsers in Java. Some target speed, some target accuracy, some target flexibility (streaming, buffering, etc). Some have use special subclasses of the collections api instead of using the ones that are already there. That seems bloated to me.
So I would like to know, which one(s) tackle the job of string to object conversion in the shortest/simplest manner? (as in, less code)
Edit: I am sorry I was not clear. What I mean is the smallest parser. So if one parser was 600 lines and the other was 400 lines. I don't care which is faster so much as which is easier to understand. This is not to be confused with how many lines it takes to invoke the parser. I expect almost all would be just a few lines of code and if I have to add a convenience method that is OK.
Purpose is more of an educational thing, how is it done, and the wish to use APIs where it is easy to see how it works.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我能想到两个。杰克逊或格森。我将添加一些示例。
GSON
杰克逊
杰克逊速度稍快一些,所以这取决于你想要什么。
I can think of two. Jackson or Gson. I'll add some examples.
GSON
Jackson
Jackson is a little faster so its up to you what you want.
http://code.google.com/p/json-smart/ 怎么样?显然,它非常小。似乎是最新的。
What about http://code.google.com/p/json-smart/? It's very small, apparently. Seems to be up to date.
让我在 https://github.com/KasparNagu/plain-java-json< 提到解决方案/a> 解析器类使用 107 行。
当然,如果您打算使用它,那么使用上面提到的经过验证的实现之一会更好。
Let me mention the solution at https://github.com/KasparNagu/plain-java-json using 107 lines for the parser class.
Yet of course, if you plan to use it, you're way better off with one of the proven implementations mentioned above.
Google Gson 是我对此的偏好。您所要做的就是创建一个具有与 JSON 对象匹配的结构的类,并且转换 JSON 可以很简单:
如果您需要做更复杂的事情,您只需要添加一些字段注释或编写自定义序列化器/解串器(这非常简单)并注册一次。实际上,将 JSON 转换为对象始终只需调用 gson.fromJson(...) 即可。
Google Gson is my preference for this. All you have to do is make a class with a structure that matches a JSON object and converting JSON can be as simple as:
If there's something more complex you need to do, you only need to add some field annotations or write a custom serializer/deserializer (which is pretty easy) and register it once. Actually converting JSON to objects is always just a matter of calling
gson.fromJson(...)
.如果您正在寻找一个真正最小的 JSON 解析器,您应该看看这些:
两者都是完整的 JSON 实现,仅由十几个 Java 类组成。
If you're looking for a really minimal JSON parser, you should have a look at these:
Both are complete JSON implementations that consist of only a dozen Java classes.
我的解析器是一个单一类,740行 删除评论后;它高效、功能齐全,并且有许多用于解析“人类可读”JSON 的选项。
My parser is a single class, and 740 lines after stripping comments; it's efficient, fully functional and has a number of options for parsing "human readable" JSON.