下面是我的Java代码
public Test parseTest(String test) {
Testresult = null;
try {
result = gson.fromJson(test, Test.class);
if (CAT.isDebugEnabled()) {
CAT.debug(result);
}
} catch (JsonSyntaxException e) {
CAT.warn(e.getMessage() + "\nCan't parse\n" + test);
}
return result;
}
解析Json,我在下面的jar中使用
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.2</version>
</dependency>
下面是我的JSON:
Test": [
{
"A": "X;DOS533",
"B": "FCA BANK SPAEUR1.5BN21MAR2019",
"C": null,
"D": "AA BB EUR1.5BN",
"E": "€1.5BN Test LN BNK €100M 12M",
"Ccy": "EUR",
"TypeCode": "TML "
}
下面是在Unix框中生成的.csv文件中的行。
4243842|Test:ABC|Active||6||FFTIAIT||Internal|X;DOS5KT|FCA BANK SPAEUR1.5BN21MAR2019|?1.5BN Test LN NWM ?100M 12M|TML|
这里的€符号被替换为?(问号)。
我将英镑(£)从.csv转换为.bcp文件时遇到相同的问题。
我尝试传递字节流而不是字符串,并且有效。
下面是更新的代码
public Test parseTest(String test) {
Testresult = null;
try {
result = gson.fromJson(new InputStreamReader(new ByteArrayInputStream(test.getBytes("UTF-8"))), Test.class);
if (CAT.isDebugEnabled()) {
CAT.debug(result);
}
} catch (JsonSyntaxException e) {
CAT.warn(e.getMessage() + "\nCan't parse\n" + test);
}catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}