GSON读入数组class元素
GSON read into array class element
我想将 topojson
个对象读入 Java class。 Topojson 是 JSON 的特殊 class,定义对象在查找中有弧的地方 table 如下:
{
"type":"Topology",
"objects":{
"collection":{
"type":"GeometryCollection",
"geometries":[
{"type":"LineString","properties":{"id":842296681,"start":892624561,"end":892624564,"class":5,"lanes":null,"direction":"B","length":0.000485},"arcs":[0]},
{"type":"LineString","properties":{"id":842296682,"start":892624563,"end":892624564,"class":5,"lanes":null,"direction":"B","length":0.000351},"arcs":[1]},
]
}
},
"arcs":[
[[4816,1007],[262,2281],[183,738]],
[[4397,3892],[341,-268],[235,0],[288,402]]
],
"transform":{
"scale":[3.8203658765624953e-7,1.4901510251040002e-7],
"translate":[-87.63437999816,41.86725999012999]},
"bbox":[-87.63437999816,41.86725999012999,-87.63056001432003,41.86874999213999]
}
我正在尝试按照建议 in this answer 进行操作,并将对象直接读入 class,我将其定义为
import java.util.ArrayList;
import java.util.Map;
/**
* Created by gregmacfarlane on 7/11/17.
*/
public class TopoJsonNetwork {
String type;
Map<String, GeometryCollection> objects;
ArrayList<Double[][]> arcs;
Transform transform;
}
class GeometryCollection{
String type;
Geometry[] geometries;
}
class Geometry{
String type;
Map<String, String> properties;
Integer[] arcs; // lines will have only a single arc
}
class Transform{
Double[] scale;
Double[] translate;
Double[] bbox;
}
调用 gson 时一切正常 reader
reader = new JsonReader(new FileReader(topoFile));
Gson gson = new Gson();
TopoJsonNetwork topoNet = gson.fromJson(reader, TopoJsonNetwork.class);
意味着我的 class 的所有对象都已填充 --- 除了 arcs
数组。该元素在那里,但具有 null
值。我应该如何更改此元素的定义方式以使其正确填充?
使用您的代码我可以获得预期的结果。我修改了您的主程序并使用嵌套 for 循环将 ArrayList 作为二维数组循环。结果是 class.
中 arcs
列表中所有数字的列表
JsonReader reader = new JsonReader(new FileReader("topojson.json"));
Gson gson = new Gson();
TopoJsonNetwork topoNet = gson.fromJson(reader, TopoJsonNetwork.class);
for (Double[][] d : topoNet.arcs) {
for (int i = 0; i < d.length; i++) {
for (int j = 0; j < d[i].length; j++) {
System.out.println(d[i][j]);
}
}
}
在这种情况下,topoNet.arcs
是一个大小为 2 的列表,如您所料,它包含几个二维 Double
数组。
我想将 topojson
个对象读入 Java class。 Topojson 是 JSON 的特殊 class,定义对象在查找中有弧的地方 table 如下:
{
"type":"Topology",
"objects":{
"collection":{
"type":"GeometryCollection",
"geometries":[
{"type":"LineString","properties":{"id":842296681,"start":892624561,"end":892624564,"class":5,"lanes":null,"direction":"B","length":0.000485},"arcs":[0]},
{"type":"LineString","properties":{"id":842296682,"start":892624563,"end":892624564,"class":5,"lanes":null,"direction":"B","length":0.000351},"arcs":[1]},
]
}
},
"arcs":[
[[4816,1007],[262,2281],[183,738]],
[[4397,3892],[341,-268],[235,0],[288,402]]
],
"transform":{
"scale":[3.8203658765624953e-7,1.4901510251040002e-7],
"translate":[-87.63437999816,41.86725999012999]},
"bbox":[-87.63437999816,41.86725999012999,-87.63056001432003,41.86874999213999]
}
我正在尝试按照建议 in this answer 进行操作,并将对象直接读入 class,我将其定义为
import java.util.ArrayList;
import java.util.Map;
/**
* Created by gregmacfarlane on 7/11/17.
*/
public class TopoJsonNetwork {
String type;
Map<String, GeometryCollection> objects;
ArrayList<Double[][]> arcs;
Transform transform;
}
class GeometryCollection{
String type;
Geometry[] geometries;
}
class Geometry{
String type;
Map<String, String> properties;
Integer[] arcs; // lines will have only a single arc
}
class Transform{
Double[] scale;
Double[] translate;
Double[] bbox;
}
调用 gson 时一切正常 reader
reader = new JsonReader(new FileReader(topoFile));
Gson gson = new Gson();
TopoJsonNetwork topoNet = gson.fromJson(reader, TopoJsonNetwork.class);
意味着我的 class 的所有对象都已填充 --- 除了 arcs
数组。该元素在那里,但具有 null
值。我应该如何更改此元素的定义方式以使其正确填充?
使用您的代码我可以获得预期的结果。我修改了您的主程序并使用嵌套 for 循环将 ArrayList 作为二维数组循环。结果是 class.
中arcs
列表中所有数字的列表
JsonReader reader = new JsonReader(new FileReader("topojson.json"));
Gson gson = new Gson();
TopoJsonNetwork topoNet = gson.fromJson(reader, TopoJsonNetwork.class);
for (Double[][] d : topoNet.arcs) {
for (int i = 0; i < d.length; i++) {
for (int j = 0; j < d[i].length; j++) {
System.out.println(d[i][j]);
}
}
}
在这种情况下,topoNet.arcs
是一个大小为 2 的列表,如您所料,它包含几个二维 Double
数组。