我使用gson(Java 1.7中的2.3.1版)来序列化一个对象。虽然它在大多数情况下都能正常工作,但反序列化漂亮的打印字符串会失败,并出现以下异常:
com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected name at line 2 column 1 path $.
at com.google.gson.Gson.fromJson(Gson.java:825)
at de.ais.fileexchange.interfaceimplementations.FEProcess.load(FEProcess.java:70)
我的Java代码:
public static IFEProcess load(File file) throws FileNotFoundException, IOException, ClassNotFoundException
{
Gson gson = new Gson();
String json = FileHelper.getStringFromFile(file);
JsonReader reader = new JsonReader(new StringReader(json));
reader.setLenient(true);
return gson.fromJson(reader, FEProcess.class);
}
public static void save(File file, IFEProcess process) throws IOException
{
//Gson gson = new Gson();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(process);
FileHelper.writeStringToFile(json, file);
}
JSON-String:
{
"description": "",
"altMailAdress": "dfgsfsdg",
"mailOnError": true,
"stopOnError": false,
"operationList": [
{
"lConfig": {
"password": "",
"username": "",
"port": 1212,
"additionalLoginInformations": "",
"hostname": "",
"isToRemoteTransfer": false,
"domain": "",
"sambaShare": ""
},
"operationConfig": {
"remoteDirectory": "",
"localDirectory": "321",
"sourceSemaphoreSuffix": "",
"sourceSemaphoreType": "NONE",
"targetSemaphoreSuffix": "",
"targetSemaphoreType": "NONE",
"sourceFilePattern": "ztju675",
"lock": false,
"delete": false,
"backup": true,
"noOverwrite": false,
"fileRenameRegex": "ztju675",
"fileRenameContent": ""
},
"fileOperationType": "FILESYSTEM",
"fopConfiguration": {
"fopPath": "",
"fopTime": "NONE"
}
},
{
"lConfig": {
"password": "",
"username": "",
"port": 0,
"additionalLoginInformations": "",
"hostname": "",
"isToRemoteTransfer": false,
"domain": "",
"sambaShare": ""
},
"operationConfig": {
"remoteDirectory": "",
"localDirectory": "",
"sourceSemaphoreSuffix": "",
"sourceSemaphoreType": "NONE",
"targetSemaphoreSuffix": "",
"targetSemaphoreType": "NONE",
"sourceFilePattern": "fdsfas",
"lock": false,
"delete": true,
"backup": false,
"noOverwrite": false,
"fileRenameRegex": "fdsfas",
"fileRenameContent": ""
},
"fileOperationType": "FILESYSTEM",
"fopConfiguration": {
"fopPath": "",
"fopTime": "NONE"
}
}
]
}
即使我使用宽松的解析,它也不起作用......
答案 0 :(得分:0)
我已经尝试过您的代码,它适用于我,使用以下代码:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringReader;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.stream.JsonReader;
public class Main {
public static void main(String[] args) throws Exception {
IFEProcess fe=load(new File("jason.json"));
save(new File("txt.json"),fe);
}
public static IFEProcess load(File file) throws FileNotFoundException,
IOException, ClassNotFoundException {
Gson gson = new Gson();
String json = FileHelper.getStringFromFile(file);
JsonReader reader = new JsonReader(new StringReader(json));
reader.setLenient(true);
return gson.fromJson(reader, FEProcess.class);
}
public static void save(File file, IFEProcess process) throws IOException {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(process);
FileHelper.writeStringToFile(json, file);
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileHelper {
public static String getStringFromFile(File file) {
StringBuilder sb = new StringBuilder();
try {
BufferedReader buff = new BufferedReader(new FileReader(file));
String line;
while ((line = buff.readLine()) != null)
sb.append(line + "\n");
buff.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
public static void writeStringToFile(String json, File file) {
FileWriter fw;
try {
fw = new FileWriter(file);
fw.write(json);
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.util.ArrayList;
public class FEProcess implements IFEProcess {
String description;
String altMailAdress;
boolean mailOnError;
boolean stopOnError;
ArrayList<Operation> operationList;
}
public class Operation {
LConfig lConfig;
OperationConfig operationConfig;
String fileOperationType;
class fopConfiguration {
String fopPath;
String fopTime;
}
}
public class LConfig {
String password;
String username;
int port;
String additionalLoginInformations;
String hostname;
boolean isToRemoteTransfer;
String domain;
String sambaShare;
}
public class OperationConfig {
String remoteDirectory;
String localDirectory;
String sourceSemaphoreSuffix;
String sourceSemaphoreType;
String targetSemaphoreSuffix;
String targetSemaphoreType;
String sourceFilePattern;
boolean lock;
boolean delete;
boolean backup;
boolean noOverwrite;
String fileRenameRegex;
String fileRenameContent;
}