将 JSON 数据转换为 Java 对象
我希望能够从 Java 操作方法中的 JSON 字符串访问属性。只需输入 myJsonString = object.getJson()
即可获取该字符串。下面是该字符串的示例:
{
'title': 'ComputingandInformationsystems',
'id': 1,
'children': 'true',
'groups': [{
'title': 'LeveloneCIS',
'id': 2,
'children': 'true',
'groups': [{
'title': 'IntroToComputingandInternet',
'id': 3,
'children': 'false',
'groups': []
}]
}]
}
在此字符串中,每个 JSON 对象都包含一个其他 JSON 对象的数组。目的是提取 ID 列表,其中任何给定对象拥有包含其他 JSON 对象的组属性。我将 Google 的 Gson 视为潜在的 JSON 插件。谁能提供某种形式的指导来指导我如何从这个 JSON 字符串生成 Java?
I want to be able to access properties from a JSON string within my Java action method. The string is available by simply saying myJsonString = object.getJson()
. Below is an example of what the string can look like:
{
'title': 'ComputingandInformationsystems',
'id': 1,
'children': 'true',
'groups': [{
'title': 'LeveloneCIS',
'id': 2,
'children': 'true',
'groups': [{
'title': 'IntroToComputingandInternet',
'id': 3,
'children': 'false',
'groups': []
}]
}]
}
In this string every JSON object contains an array of other JSON objects. The intention is to extract a list of IDs where any given object possessing a group property that contains other JSON objects. I looked at Google's Gson as a potential JSON plugin. Can anyone offer some form of guidance as to how I can generate Java from this JSON string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
Google Gson 支持泛型和嵌套 bean。 JSON 中的
[]
表示一个数组,应映射到 Java 集合,例如List
或只是一个普通的 Java 数组。 JSON 中的{}
表示一个对象,并且应映射到 JavaMap
或只是一些 JavaBean 类。您有一个具有多个属性的 JSON 对象,其中
groups
属性表示相同类型的嵌套对象的数组。这可以用 Gson 来解析,如下所示:相当简单,不是吗?只需拥有一个合适的 JavaBean 并调用
Gson#fromJson()
。另请参阅:
Google Gson supports generics and nested beans. The
[]
in JSON represents an array and should map to a Java collection such asList
or just a plain Java array. The{}
in JSON represents an object and should map to a JavaMap
or just some JavaBean class.You have a JSON object with several properties of which the
groups
property represents an array of nested objects of the very same type. This can be parsed with Gson the following way:Fairly simple, isn't it? Just have a suitable JavaBean and call
Gson#fromJson()
.See also:
Gson 的 Bewaaaaare!这非常酷,非常棒,但是当您想做除简单对象之外的任何事情时,您可能很容易需要开始构建自己的序列化器(这并不难)。
另外,如果您有一个对象数组,并且将一些 json 反序列化到该对象数组中,则真正的类型将丢失!完整的对象甚至不会被复制!使用 XStream.. 如果使用 jsondriver 并设置正确的设置,会将丑陋的类型编码到实际的 json 中,这样你就不会丢失任何东西。真正的序列化要付出很小的代价(丑陋的 json)。
请注意,Jackson 修复了这些问题,并且比 GSON 更快。
Bewaaaaare of Gson! It's very cool, very great, but the second you want to do anything other than simple objects, you could easily need to start building your own serializers (which isn't that hard).
Also, if you have an array of Objects, and you deserialize some json into that array of Objects, the true types are LOST! The full objects won't even be copied! Use XStream.. Which, if using the jsondriver and setting the proper settings, will encode ugly types into the actual json, so that you don't loose anything. A small price to pay (ugly json) for true serialization.
Note that Jackson fixes these issues, and is faster than GSON.
奇怪的是,到目前为止提到的唯一像样的 JSON 处理器是 GSON。
这里有更多不错的选择:
编辑(2013 年 8 月):
还有一个需要考虑的:
Oddly, the only decent JSON processor mentioned so far has been GSON.
Here are more good choices:
EDIT (Aug/2013):
One more to consider:
或者和杰克逊一起:
Or with Jackson:
简单且有效的 Java 代码将
JSONObject
转换为Java 对象
Employee.java
LoadFromJSON.java
Easy and working java code to convert
JSONObject
toJava Object
Employee.java
LoadFromJSON.java
如果通过任何更改,您所在的应用程序已经使用 http://restfb.com/ 那么您可以这样做:
ETC。
If, by any change, you are in an application which already uses http://restfb.com/ then you can do:
etc.
根据输入 JSON 格式(字符串/文件)创建一个 jsonString。 JSON对应的Message类对象示例如下:
Depending on the input JSON format(string/file) create a jSONString. Sample Message class object corresponding to JSON can be obtained as below:
如果您使用任何类型的带有特殊映射键或值的特殊映射,您会发现谷歌的实现没有考虑到这一点。
If you use any kind of special maps with keys or values also of special maps, you will find it's not contemplated by the implementation of google.
标准的东西有什么问题?
What's wrong with the standard stuff?
以这种方式传递 JSON 文件,它将返回对象。
Pass your JSON file in this way, it will return the object.
尝试一下 boon:
https://github.com/RichardHightower/boon
速度非常快:
https://github.com/RichardHightower/json-parsers-benchmark
不要相信我的话为此...查看加特林基准测试。
https://github.com/gadling/json-parsers-benchmark
(最多 4 倍)在某些情况下,经过数百次测试,它还有一个更快的索引覆盖模式。它还很年轻,但已经有了一些用户。)
它可以比任何其他库更快地解析 JSON 到地图和列表 。 JSON DOM,并且没有索引覆盖模式。使用 Boon Index Overlay 模式,速度甚至更快。
它还具有非常快的 JSON lax 模式和 PLIST 解析器模式。 :)(并且内存超低,直接从字节模式使用 UTF-8 编码)。
它还具有最快的 JSON 到 JavaBean 模式。
它是新的,但如果您正在寻找速度和简单的 API,我认为没有更快或更简约的 API。
Give boon a try:
https://github.com/RichardHightower/boon
It is wicked fast:
https://github.com/RichardHightower/json-parsers-benchmark
Don't take my word for it... check out the gatling benchmark.
https://github.com/gatling/json-parsers-benchmark
(Up to 4x is some cases, and out of the 100s of test. It also has a index overlay mode that is even faster. It is young but already has some users.)
It can parse JSON to Maps and Lists faster than any other lib can parse to a JSON DOM and that is without Index Overlay mode. With Boon Index Overlay mode, it is even faster.
It also has a very fast JSON lax mode and a PLIST parser mode. :) (and has a super low memory, direct from bytes mode with UTF-8 encoding on the fly).
It also has the fastest JSON to JavaBean mode too.
It is new, but if speed and simple API is what you are looking for, I don't think there is a faster or more minimalist API.
最简单的方法是您可以使用此 softconvertvalue 方法,这是一种自定义方法,您可以在其中将 jsonData 转换为特定的 Dto 类。
The easiest way is that you can use this softconvertvalue method which is a custom method in which you can convert jsonData into your specific Dto class.
使用雅加达 JSON-P 标准,
Using the Jakarta JSON-P standard,