将 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 提供某种形式的指导吗?

原文由 Faiyet 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1k
2 个回答

我将 Google 的 Gson 视为一个潜在的 JSON 插件。任何人都可以就如何从这个 JSON 字符串生成 Java 提供某种形式的指导吗?

Google Gson 支持泛型和嵌套 bean。 JSON 中的 [] 表示一个数组,应该映射到一个 Java 集合,例如 List 或只是一个普通的 Java 数组。 JSON 中的 {} 表示一个对象,应该映射到 Java Map 或只是一些 JavaBean 类。

您有一个带有多个属性的 JSON 对象,其中 groups 属性表示相同类型的嵌套对象数组。这可以通过以下方式使用 Gson 进行解析:

 package com.stackoverflow.q1688099;

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':[]"
                    + "}]"
                + "}]"
            + "}";

        // Now do the magic.
        Data data = new Gson().fromJson(json, Data.class);

        // Show it.
        System.out.println(data);
    }

}

class Data {
    private String title;
    private Long id;
    private Boolean children;
    private List<Data> groups;

    public String getTitle() { return title; }
    public Long getId() { return id; }
    public Boolean getChildren() { return children; }
    public List<Data> getGroups() { return groups; }

    public void setTitle(String title) { this.title = title; }
    public void setId(Long id) { this.id = id; }
    public void setChildren(Boolean children) { this.children = children; }
    public void setGroups(List<Data> groups) { this.groups = groups; }

    public String toString() {
        return String.format("title:%s,id:%d,children:%s,groups:%s", title, id, children, groups);
    }
}

相当简单,不是吗?只要有一个合适的 JavaBean 并调用 Gson#fromJson()

也可以看看:

原文由 BalusC 发布,翻译遵循 CC BY-SA 4.0 许可协议

请注意 Gson!这非常酷,非常棒,但是如果您想做除简单对象以外的任何事情,您可能很容易需要开始构建自己的序列化程序( 并不难)。

此外,如果您有一个对象数组,并且将一些 json 反序列化到该对象数组中,那么真正的类型就会丢失!完整的对象甚至不会被复制!使用 XStream .. 其中,如果使用 jsondriver 并设置适当的设置,会将丑陋的类型编码为实际的 json,这样您就不会丢失任何东西。为真正的序列化付出很小的代价(丑陋的 json)。

请注意, Jackson 修复了这些问题,并且比 GSON 更快

原文由 Jor 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题