我希望能够从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对象的array.其目的是提取一个ID列表,其中任何给定对象都拥有包含其他JSON对象的group属性.我认为谷歌的Gson是一个潜在的JSON插件.有人能提供一些形式的指导,告诉我如何从这个JSON字符串生成Java吗?

推荐答案

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?

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

您有一个JSON对象,它有几个属性,其中groups属性表示一个类型相同的嵌套对象array.这可以通过以下方式使用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().

另请参阅:

Java相关问答推荐

在Spring Boot中测试时出现SQL语法错误

如何从片段请求数据到活动?在主要活动中单击按钮请求数据?

无法在Java中将hhmmss格式的时间解析为LocalTime

如何使用CSS为选定但未聚焦的表格行设置背景 colored颜色 ?

如何在SystemiccationRetryListenerSupport中获得类级别的spring retryable annotation中指定的标签?

FALSE:它应该在什么时候使用?

如何调整工作时间日历中的时间

使用GridBagLayout正确渲染

放气总是压缩整个街区吗?

Jolt变换JSON数组问题

Kotlin Val是否提供了与Java最终版相同的可见性保证?

SpringBoot:在条件{Variable}.isBlank/{Variable}.isEmpty不起作用的情况下进行路径变量验证

为什么有两种实现来检索数组类的组件类型?

JFree Chart从图表中删除边框

判断重复的两个二维表算法?

如何使用Java对随机生成的字母数字优惠券代码进行过期设置

OAuth:登录后无法查看Google邮箱地址

具有多个分析模式的复杂分隔字符串的正则表达式

在WHILE()循环初始化部分中声明和初始化变量的Java语法?

如何在特定关键字后提取与模式匹配的多个值?