2 回答
TA贡献1828条经验 获得超6个赞
确保您的 json 对象格式正确并且与 POJO 兼容,稍后您可以使用下面的代码进行不同类型的转换 -
Gson gson = new Gson();
// 1. JSON file to Java object
Staff staff = gson.fromJson(new FileReader("C:\\projects\\test.json"), Test.class);
// 2. JSON string to Java object
String json = "{'name' : 'test'}";
Staff staff = gson.fromJson(json, Test.class);
// 3. JSON file to JsonElement -> String
JsonElement json = gson.fromJson(new FileReader("C:\\projects\\test.json"), JsonElement.class);
String result = gson.toJson(json);
Maven 依赖 -
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>x.x.x</version>
</dependency>
TA贡献1842条经验 获得超21个赞
你的 json 结构不正确,但是我已经在你的类中尝试了以下 json 并且它有效
{
"Meta Data": {
"1. Information": "Daily Time Series with Splits and Dividend Events",
"2. Symbol": "MSFT",
"3. Last Refreshed": "2019-10-24",
"4. Output Size": "Compact",
"5. Time Zone": "US/Eastern"
},
"timeSeriesDaily": [
{
"2019-10-24": {
"1. open": "139.3900",
"2. high": "140.4100",
"3. low": "138.6700",
"closingStockPrice": "139.9400",
"5. adjusted close": "139.9400",
"6. volume": "34434281",
"7. dividend amount": "0.0000",
"8. split coefficient": "1.0000"
}
}
]
}
添加回答
举报