gpt4 book ai didi

java - Gson用数组解析Json

转载 作者:行者123 更新时间:2023-11-30 07:13:57 24 4
gpt4 key购买 nike

我在构建一个将按照我的预期解析 gson 的类时遇到了一些问题。

我创建了一个类。

public class JsonObjectBreakDown {
public String type;
public List<String> coordinates = new ArrayList<String>();
}

然后调用

JsonObjectBreakDown p = gson.fromJson(withDup, JsonObjectBreakDown.class);

下面是我的json

  {
"type":"Polygon",
"coordinates":[
[
[
-66.9,
18.05
],
[
-66.9,
18.05
],
[
-66.9,
18.06
],
[
-66.9,
18.05
]
]
]
}

我以前成功地使用过 gson,但从来没有使用过这样的数组。我不应该使用 List/ArrayList 吗?

我收到错误信息;

Exception in thread "main" com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 31

OpenCSV 代码

CSVReader reader = new CSVReader(new FileReader("c:\\Json.csv"));
String tmp = reader.readNext();
CustomObject tmpObj = CustomObject(tmp[0], tmp[1],......);

最佳答案

这里的问题是您的 JSON 中有一个 float 数组的数组。你的类(class)应该是

public class JsonObjectBreakDown {
public String type;
public List<List<float[]>> coordinates = new ArrayList<>();
}

用上面的解析并尝试

System.out.println(p.coordinates.size());
System.out.println(p.coordinates.get(0).size());
System.out.println(Arrays.toString(p.coordinates.get(0).get(0)));

产量

1
2
[-66.9, 18.05]

关于java - Gson用数组解析Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18880066/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com