2

我在构建一个可以解析 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],......);
4

1 回答 1

4

这里的问题是您的 JSON 中有一个浮点数数组数组。你的课应该是

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]
于 2013-09-18T18:59:47.310 回答