Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用Gson在Java中用混合数据类型表示Json对象_Java_Json_Gson - Fatal编程技术网

如何用Gson在Java中用混合数据类型表示Json对象

如何用Gson在Java中用混合数据类型表示Json对象,java,json,gson,Java,Json,Gson,将一些Json序列化为Java对象有点困难。如您所见,Json位包含顶级元素args,其中包含一个对象列表,其中包含一个字符串作为键和混合的值包。我在Java源文件中尝试了一些创造性的东西: Json { "args": { "all": { "expectCliArgs": true }, "Prog3a": { "expectedNumberOfCliArgs": 2,

将一些Json序列化为Java对象有点困难。如您所见,Json位包含顶级元素
args
,其中包含一个对象列表,其中包含一个
字符串作为键和混合的值包。我在Java源文件中尝试了一些创造性的东西:

Json

{
    "args": {
        "all": {
            "expectCliArgs": true
        },
        "Prog3a": {
            "expectedNumberOfCliArgs": 2,
            "expectedErrorMessage": "Usage: java Prog3a infilename searchword"
        },
        "Prog3b": {
            "expectedNumberOfCliArgs": 2,
            "expectedErrorMessage": "Usage: java Prog3b infileName outfileName"
        },
        "Prog3c": {
            "expectedNumberOfCliArgs": 1,
            "expectedErrorMessage": "Usage: java Prog3c infileName"
        }
    }
}
Java

protected class Args {
    public Map<String, Set<Map<String, Object>>> map = new HashMap<>(); // no dice
}

protected class Args {
    public Map<String, Set<Map<String, ?>>> map = new HashMap<>(); // no dice
}
我在Json文件中设置了一些断点,看起来“args”对象就是引起胃灼热的对象。作为参考,这里是,和。看起来Gson把我的对象和数组搞混了

第847列为:

searchword"},"Prog3b":{
             ^
有什么想法吗

更新2 我的变量签名不正确:

private Map<String, Set<ExpectedArgs>> args = new HashMap<>();  // wrong
private Map<String, ExpectedArgs> args = new HashMap<>(); // right
private Map args=new HashMap();//错误的
私有映射args=newhashmap();//正确的

我将为包含两组字段的内部对象定义一个类,如下所示:

public static class ExpectedArgs {
    public Boolean expectCliArgs;
    public Integer expectedNumberOfCliArgs;
    public String expectedErrorMessage;
}
public static class Data {
    public Map<String, ExpectedArgs> args;
}
(注意:我在这里特意使用了
布尔值
整数
而不是
布尔值
整数
,这样您就可以通过测试
空值来判断字段是否设置了。)

然后您可以像这样定义外部类:

public static class ExpectedArgs {
    public Boolean expectCliArgs;
    public Integer expectedNumberOfCliArgs;
    public String expectedErrorMessage;
}
public static class Data {
    public Map<String, ExpectedArgs> args;
}
此代码为您的示例JSON输出以下内容:

all
true
null
null

Prog3a
null
2
Usage: java Prog3a infilename searchword

Prog3b
null
2
Usage: java Prog3b infileName outfileName

Prog3c
null
1
Usage: java Prog3c infileName

这是一个伟大的解决方案!昨晚我做了同样的事情,但我在Gson上遇到了一个错误。有什么想法吗?
all
true
null
null

Prog3a
null
2
Usage: java Prog3a infilename searchword

Prog3b
null
2
Usage: java Prog3b infileName outfileName

Prog3c
null
1
Usage: java Prog3c infileName