java json 通用对象_java-Gson解析具有通用类型的Json对象

我将以下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&递归地重复数据构造.因此,表示这种重复模式的基本类是通用类,如下所示:

{

"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 {

/**

* Represents a unique identifier

*/

private String identifier;

/**

* Represents the data pertaining to this {@link JsonData#identifier}

*/

private T data;

private static final Set 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, 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 readconfigFile() {

Reader reader = null;

JsonData data = null;

Gson gson = null;

Type confType;

try {

reader = new FileReader("./config.json");

gson = new GsonBuilder().create();

confType = new TypeToken>() {}.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> readconfigFileList() {

Reader reader = null;

JsonData> dataArray = null;

Gson gson = null;

Type confType;

Type confTypeArray;

try {

reader = new FileReader("./config.json");

gson = new GsonBuilder().create();

confType = new TypeToken>>() {}.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;

}

标签:unit-testing,gson,java

来源: https://codeday.me/bug/20191120/2041330.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值