Java 导入由json序列化的json失败
所以我有一个带有一些字段的对象Java 导入由json序列化的json失败,java,json,arraylist,gson,Java,Json,Arraylist,Gson,所以我有一个带有一些字段的对象 protected String name; protected String relativePathAndFileName; protected DateTime next_Run; protected ArrayList<String> hosts; 稍后,当我需要读取此json文件时,我会尝试这样做: public void serialize(){ Gson gson = Converters.registerDateTime(ne
protected String name;
protected String relativePathAndFileName;
protected DateTime next_Run;
protected ArrayList<String> hosts;
稍后,当我需要读取此json文件时,我会尝试这样做:
public void serialize(){
Gson gson = Converters.registerDateTime(new GsonBuilder()).setPrettyPrinting().create();
String json = gson.toJson(this);
try {
FileWriter writer = new FileWriter(this.relativePathAndFileName);
writer.write (json);
writer.close();
} catch (IOException e) {
logger.error("Error while trying to write myAlert to json: ", e);
}
}
try {
for (File f : alertConfigFiles) {
Gson gson = new Gson();
JsonReader reader = new JsonReader(new FileReader(f));
Type type = new TypeToken<Map<String, String>>(){}.getType();
Map<String, String> myMap = gson.fromJson(reader, type);
Alert tempAlert = new Alert(myMap);
myAlerts.add(tempAlert);
logger.debug("Imported: " + f.toString());
}
文件中的JSON具有以下效果:
{
"name": "Logs Per Host - past 24 hours",
"relativePathAndFileName": "./Elk-Reporting/Alerts/logs_per_host24h.json",
"next_Run": "2017-06-07T22:24:56.682-04:00",
"hosts": [
"app-12c",
"app1-18",
"wp-01",
"app-02",
"wp-02",
"cent-04",
"app-06",
"app-05"
]
}
当它试图导入主机的ArrayList时,它似乎被阻塞了,但它能够毫无问题地将它们写出来
有人能就如何让我的导入工作顺利进行提供一些建议吗?这两行似乎是问题所在:
Type type = new TypeToken<Map<String, String>>(){}.getType();
Map<String, String> myMap = gson.fromJson(reader, type);
如果要使用这种方法,可能需要将Java类更改为包含字符串数组,而不是字符串数组列表
也许对你有点帮助。特别是第一种情况适合你的情况
另一个选项是如上所述的自定义反序列化程序。这两行似乎是问题所在:
Type type = new TypeToken<Map<String, String>>(){}.getType();
Map<String, String> myMap = gson.fromJson(reader, type);
如果要使用这种方法,可能需要将Java类更改为包含字符串数组,而不是字符串数组列表
也许对你有点帮助。特别是第一种情况适合你的情况
另一个选项是如上所述的自定义反序列化程序。尽量保持简单。使用地图等,是产生问题的一种方式。 以下是反序列化/序列化的工作代码:
package com.rizze.beans.labs.sof;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import com.google.gson.Gson;
public class SOFGson {
public String json = "{ \"name\": \"Logs Per Host - past 24 hours\", \"relativePathAndFileName\": \"./Elk-Reporting/Alerts/logs_per_host24h.json\", \"next_Run\": \"2017-06-07T22:24:56.682-04:00\", \"hosts\": [ \"bos-qa-app-12c\", \"bos-qa-app1-18\", \"bos-qa-wp-01\", \"bos-lt-app-02\", \"bos-qa-wp-02\", \"bos-dev-cent-04.americanwell.com\", \"bos-qa-app-06\", \"bos-qa-app-05\" ]}";
public class MyObj{
protected String name;
protected String relativePathAndFileName;
protected String next_Run;
protected String[] hosts;
}
@Test
public void test() {
Gson gson = new Gson();
MyObj obj = gson.fromJson(json, MyObj.class);
assertTrue(obj!=null);
assertTrue(obj.hosts.length==8);
System.out.println(gson.toJson(obj));
}
}
下面是要点中的课程:尽量保持简单。使用地图等,是产生问题的一种方式。 以下是反序列化/序列化的工作代码:
package com.rizze.beans.labs.sof;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import com.google.gson.Gson;
public class SOFGson {
public String json = "{ \"name\": \"Logs Per Host - past 24 hours\", \"relativePathAndFileName\": \"./Elk-Reporting/Alerts/logs_per_host24h.json\", \"next_Run\": \"2017-06-07T22:24:56.682-04:00\", \"hosts\": [ \"bos-qa-app-12c\", \"bos-qa-app1-18\", \"bos-qa-wp-01\", \"bos-lt-app-02\", \"bos-qa-wp-02\", \"bos-dev-cent-04.americanwell.com\", \"bos-qa-app-06\", \"bos-qa-app-05\" ]}";
public class MyObj{
protected String name;
protected String relativePathAndFileName;
protected String next_Run;
protected String[] hosts;
}
@Test
public void test() {
Gson gson = new Gson();
MyObj obj = gson.fromJson(json, MyObj.class);
assertTrue(obj!=null);
assertTrue(obj.hosts.length==8);
System.out.println(gson.toJson(obj));
}
}
这是gist中的类:您正在写入文件?如果是这样的话,里面是什么?@AlexFerretti——更新了问题以包含json。您正在写入文件?如果是这样的话,里面是什么?@AlexFerretti——更新了问题,加入了json。我尝试了一下,但似乎也没有成功——试试{for(File f:alertConfigFiles){Gson Gson=new Gson();JsonReader=new JsonReader(new FileReader(f));Alert tempAlert=gson.fromJson(reader,Alert.class);myAlerts.add(tempAlert);logger.debug(“导入:+f.toString());}在尝试导入配置文件时导致了与上面相同的异常未处理的异常:com.google.gson.JsonSyntaxException:java.lang.IllegalStateExceptionI对此进行了尝试,但这似乎也不起作用--请尝试{for(文件f:alertConfigFiles){gson gson=new gson();JsonReader reader=new JsonReader(new FileReader(f));Alert tempAlert=gson.fromJson(reader,Alert.class);myAlerts.add(tempAlert);logger.debug(“导入:+f.toString());}在尝试导入配置文件时导致与上述相同的异常未经处理的异常:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException