gpt4 book ai didi

java - Gson 解析一个具有泛型类型的 Json 对象

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:42:57 24 4
gpt4 key购买 nike

我将以下 json 写入文件,我想通过 Gson 读取该文件:

{
"identifier": "CONFIG",
"data": [
{
"identifier": "HOTKEY",
"data": {
"hotKey": "testKey1",
"type": "APPLICATION",
"runnableContext": "testContext1"
}
},
{
"identifier": "HOTKEY",
"data": {
"hotKey": "testKey2",
"type": "APPLICATION",
"runnableContext": "testContext2"
}
}
]
}

在上面的Json中,可以看到Identifier & data构造是递归重复的。所以表示这种重复模式的基本类是一个通用类,如下所示:

{
"identifier": "CONFIG",
"data": {

}
}

此模式由 JsonData 类表示如下:

import java.util.Set;

....
import com.google.common.collect.ImmutableSet;

/**
* Class representing a simple {@link JsonData#identifier},
* {@link JsonData#data} format. This class can be used to
* persist application data for example in a Configuration file.
*
* @author SW029693
* @since v1.0
*/
public class JsonData <T>{
/**
* Represents a unique identifier
*/
private String identifier;
/**
* Represents the data pertaining to this {@link JsonData#identifier}
*/
private T data;

private static final Set<String> VALID_JSON_ID_TYPES = ImmutableSet.of("CONFIG","HOTKEYS","HOTKEY","RECOMMENDATIONS");

public JsonData(String identifier, T data) {
super();
this.identifier = identifier;
this.data = data;
}

/**
* Getter for {@link JsonData#identifier}
* @return
*/
public String getIdentifier() {
return identifier;
}

/**
* Sets the {@link JsonData#identifier} to the given value
* @param identifier
* Represents a unique {@link JsonData#identifier}
* @throws VerifyException
* If the argument is {@code null} or {@code empty}
*/
public void setIdentifier(String identifier) throws VerifyException{
Verifier.verifyNotNull(identifier, "identifier : null");
Verifier.verifyNotEmpty(identifier,"identifier : empty");
this.identifier = identifier;
}

/**
* Getter for {@link JsonData}
* @return
*/
public T getData() {
return data;
}

/**
* Sets the {@link JsonData#data} to the given value
* @param identifier
* Represents a unique {@link JsonData#data}
* @throws VerifyException
* If the argument is {@code null}
*/
public void setData(T data) {
Verifier.verifyNotNull(data, "data : null");
this.data = data;
}

@Override
public String toString() {
return "JsonData [identifier=" + identifier + ", data=" + data + "]";
}
}

另外,在上面的Json中,你可以看到每个热键里面的数据。在通过 gson 读取 json 后,此数据将保存在 ConfigurationProperty 类中:

public class ConfigurationProperty implements Comparable<ConfigurationProperty>, Serializable{
....
private final String hotKey;
private final String type;
private final String runnableContext;
....

现在是问题。我正在尝试读取文件并解析 Json 以使用 GSON 将其存储到相应的对象中,但没有成功。

我有一些工作代码可以读取写入文件的单个 JsonData 对象:

    {
"identifier": "HOTKEY",
"data": {
"hotKey": "testKey1",
"type": "APPLICATION",
"runnableContext": "testContext1"
}
}

被成功读取:

private static JsonData<ConfigurationProperty> readconfigFile() {
Reader reader = null;
JsonData<ConfigurationProperty> data = null;
Gson gson = null;
Type confType;
try {
reader = new FileReader("./config.json");
gson = new GsonBuilder().create();
confType = new TypeToken<JsonData<ConfigurationProperty>>() {}.getType();

data = gson.fromJson(reader,confType);
} catch (FileNotFoundException e) {
e.printStackTrace();
fail("Test failed while reading the config.json file: "+e.getMessage()); }
finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
fail("Test failed while reading the config.json file: "+e.getMessage());
}
}
return data;
}

但是如果你看第一个json,这现在是一个JsonData的递归结构。同样在解析时,我需要告诉 Gson 第一个数据对象是 JsonData 对象的数组。而且我还需要告诉 gson 该数组中的每个 JSONData 对象都是 ConfigurationProperty 类型。

我不知道该怎么做。

最佳答案

我想通了。我是这样做的

    private static JsonData<List<ConfigurationProperty>> readconfigFileList() {
Reader reader = null;
JsonData<List<ConfigurationProperty>> dataArray = null;
Gson gson = null;
Type confType;
Type confTypeArray;
try {
reader = new FileReader("./config.json");
gson = new GsonBuilder().create();
confType = new TypeToken<JsonData<List<ConfigurationProperty>>>() {}.getType();

dataArray = gson.fromJson(reader,confType);
System.out.println(dataArray.toString());

} catch (FileNotFoundException e) {
e.printStackTrace();
fail("Test failed while reading the config.json file: "+e.getMessage()); }
finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
fail("Test failed while reading the config.json file: "+e.getMessage());
}
}
return dataArray;
}

关于java - Gson 解析一个具有泛型类型的 Json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31093595/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com