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 に答える