将这个json数组解析为列表的最快方法是什么?
[
["FundRequest"],
["nOS"],
["NEX"],
["DREP"],
["ChromaWay"],
["Jura"],
["Origo"],
["Phantasma"],
["NuCypher"],
["Oasis Labs"]
]
它是通过以下代码生成的:
private void getNames(String spreadsheetUrl) {
JSONObject json = readJsonFromUrl(spreadsheetUrl);
String result = json.get("values").toString();
log.debug("Found: {}", result);
}
输出来自以下json响应:
{
"range": "Frontpage!E6:E15",
"majorDimension": "ROWS",
"values": [
[
"FundRequest"
],
[
"nOS"
],
[
"NEX"
],
[
"DREP"
],
[
"ChromaWay"
],
[
"Jura"
],
[
"Origo"
],
[
"Phantasma"
],
[
"NuCypher"
],
[
"Oasis Labs"
]
]
}
答案 0 :(得分:1)
您可以使用GSON之类的库:
使用maven安装它:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
编辑:
如果您要解析此内容:
{
"range": "Frontpage!E6:E15",
"majorDimension": "ROWS",
"values": [
[
"FundRequest"
],
[
"nOS"
],...
作为Java对象,然后为您的json实体创建一个包装器类:
public class Wrapper {
private String range;
private String majorDimension;
private List<?> values;
/**
* @return the range
*/
public String getRange() {
return range;
}
/**
* @return the values
*/
public List<?> getValues() {
return values;
}
/**
* @param values the values to set
*/
public void setValues(List<?> values) {
this.values = values;
}
/**
* @return the majorDimension
*/
public String getMajorDimension() {
return majorDimension;
}
/**
* @param majorDimension the majorDimension to set
*/
public void setMajorDimension(String majorDimension) {
this.majorDimension = majorDimension;
}
/**
* @param range the range to set
*/
public void setRange(String range) {
this.range = range;
}
}
然后使用GSON,您可以将Json字符串解析为包装对象:
Gson gson = new GsonBuilder().create();
Wrapper w = gson.fromJson(jsonString, Wrapper.class);
检查以下内容:http://www.javacreed.com/simple-gson-example/
编辑:
如果您要解析此内容:
[
["FundRequest"],
["nOS"],
["NEX"],
["DREP"],
["ChromaWay"],
["Jura"],
["Origo"],
["Phantasma"],
["NuCypher"],
["Oasis Labs"]
]
作为一个数组数组,然后使用gson可以做到:
List<?> arr = gson.fromJson("[[\"FundRequest\"],[\"nOS\"],...]", List.class);
System.out.println(arr);
println将打印: [[FundRequest],[nOS],...]
数组的json数组应解析为列表列表
希望这会有所帮助