将JSON数据转换为Java对象

11 浏览
0 Comments

将JSON数据转换为Java对象

我想要在我的Java操作方法中能够访问JSON字符串的属性。可以通过myJsonString = object.getJson()获取该字符串。下面是字符串的一个示例:\n

{
    'title': '计算机与信息系统',
    'id': 1,
    'children': 'true',
    'groups': [{
        'title': '一级CIS',
        'id': 2,
        'children': 'true',
        'groups': [{
            'title': '计算机与互联网导论',
            'id': 3,
            'children': 'false',
            'groups': []
        }]
    }]
}

\n在该字符串中,每个JSON对象都包含其他JSON对象的数组。目的是提取具有包含其他JSON对象的组属性的任何给定对象的ID列表。我考虑使用谷歌的Gson作为潜在的JSON插件。有人可以提供一些关于如何从这个JSON字符串生成Java的指导吗?

0
0 Comments

Converting JSON data to Java object is a common task in software development. JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate. Java, being a popular programming language, often needs to convert JSON data to Java objects for further processing.

One of the challenges in converting JSON data to Java objects is finding a suitable JSON processor. In the given content, it is mentioned that GSON is the only decent JSON processor mentioned so far. However, there are other good choices available as well.

One option is Jackson. Jackson is a powerful JSON processor that provides data binding, streaming, and tree model capabilities. It supports converting JSON data to/from Plain Old Java Objects (POJOs) and provides ultra-fast streaming capabilities. It also offers a convenient tree model for untyped access to JSON data.

Another option is Flex-JSON. Flex-JSON is a highly configurable serialization library. It allows developers to customize the serialization process according to their specific needs. This flexibility can be useful in scenarios where fine-grained control over the JSON conversion is required.

In addition, another JSON processor called Genson is mentioned. Genson is similar to Jackson in functionality but aims to be easier to configure for developers. It provides similar capabilities for converting JSON data to Java objects.

To summarize, the challenge of converting JSON data to Java objects can be addressed by using suitable JSON processors like GSON, Jackson, Flex-JSON, or Genson. These processors provide various capabilities for converting JSON data to Java objects, allowing developers to choose the one that best fits their requirements.

0
0 Comments

在使用Gson这个库时,如果涉及到除了简单对象之外的操作,可能需要自己构建自定义的序列化器。此外,如果你有一个对象数组,并且将一些json反序列化到这个对象数组中,那么真正的类型会丢失,完整的对象甚至都不会被复制。使用XStream可以解决这个问题,使用json驱动并设置正确的设置,可以将丑陋的类型编码到实际的json中,这样就不会丢失任何信息。为了真正的序列化,这是一个小小的代价(丑陋的json)。需要注意的是,Jackson库可以解决这些问题,并且比Gson更快。作者还写了一个Gson的分支,解决了这些问题,并且避免了Jackson的所有注解。

0
0 Comments

将JSON数据转换为Java对象的问题是因为需要将一个JSON字符串转换为Java对象。解决方法是使用Google Gson库。以下是一个示例代码,演示了如何使用Gson将JSON字符串解析为Java对象。

首先,需要创建一个适合的JavaBean类,它包含与JSON对象对应的属性。在示例中,Data类表示JSON对象,它包含title、id、children和groups属性。然后,使用Gson的fromJson()方法将JSON字符串转换为Data对象。

import java.util.List;
import com.google.gson.Gson;
public class Test {
    public static void main(String... args) throws Exception {
        String json = 
            "{"
                + "'title': 'Computing and Information systems',"
                + "'id' : 1,"
                + "'children' : 'true',"
                + "'groups' : [{"
                    + "'title' : 'Level one CIS',"
                    + "'id' : 2,"
                    + "'children' : 'true',"
                    + "'groups' : [{"
                        + "'title' : 'Intro To Computing and Internet',"
                        + "'id' : 3,"
                        + "'children': 'false',"
                        + "'groups':[]"
                    + "}]" 
                + "}]"
            + "}";
        Data data = new Gson().fromJson(json, Data.class);
        System.out.println(data);
    }
}
class Data {
    private String title;
    private Long id;
    private Boolean children;
    private List groups;
    // getters and setters
    public String toString() {
        return String.format("title:%s,id:%d,children:%s,groups:%s", title, id, children, groups);
    }
}

上述代码使用了Gson库的fromJson()方法将JSON字符串解析为Data对象,并通过toString()方法打印出对象的属性值。

使用Gson库转换JSON数据到Java对象非常简单,只需要创建一个适合的JavaBean类,并调用Gson的fromJson()方法即可。这使得处理JSON数据变得更加方便和高效。

有关更多关于JSON和Gson的信息,可以参考以下链接:

- [Json.org](http://json.org/):介绍JSON的官方网站

- [Gson User Guide](https://github.com/google/gson/blob/master/UserGuide.md):Gson的用户指南

需要注意的是,虽然Gson是一种简单易用的解决方案,但在性能方面可能不是最佳选择。如果对性能有更高要求,可以考虑使用其他库,如Jackson或dsljson。可以参考以下链接进行性能比较:

- [java-json-benchmark](https://github.com/fabienrenaud/java-json-benchmark):Java JSON库性能比较的GitHub项目

使用Gson库可以方便地将JSON数据转换为Java对象,使得处理JSON数据变得更加简单和高效。

0