java构造和生成json格式(geojson为例)
一、所要构造的json格式
二、思路和步骤
1.题外说明
本文是先解析读入的txt文件,然后建立对应的java类来接受解析的某些值,用了自己封装的TxtToPolygon(jsonpath, shpPath)方法。因此大家不必在意本代码中所用到的Attributes、Feature类(实际是json中本人打算put的数据),只需知道什么样的json格式该怎么“搭架子”和填值即可。
我需要填入的值中有coordinates坐标,因此需要提前准备:
String jsonpath="E:\\河南省乡镇点\\12.txt";
String shpPath = "E:/河南省乡镇点/GIS/";
makeDir(shpPath);
List<Point> pointsList = TxtToPolygon(jsonpath, shpPath);
具体思路及代码如下(示例):
1.采用“从外到内,层层递进剥皮”的思想分析,首先看上面geojson的格式,最外面是一层“{}”,因此需要定义一个最外层的JSON对象,方便往里面放子JSON对象或者JSON数组。
2.再看里面第二层,其含有的JSON对象有:crs(用{}包裹的),另外还有一个JSON数组:features(用[]包裹的)。type和name的键值对既不属于JSON对象也不属于JSON数组,直接put填入最外层的JSIN对象。因此还需要定义一个crs的JSON对象和一个features的JSON数组。
3.再看crs的下一层即第三层(也就是crs里面的),crs对象中又嵌套了一个对象properties,此对象中包含一个键值对,至此,crs递进到底了。因此又需要定义一个properties的JSON对象。
4.再看features的下一层,features数组中又包含两个JSON对象,第一个JSON对象中又包含一个对象properties。第二个JSON对象geometry中又包含了一个coordinates的JSON数组(用[]包裹)。
5.综上所述,需要多少个JSON对象及数组就一目了然了,然后往对应的JSON对象或数组中put对应层级的键值对或对象、数组即可,采用“从内到外,层层概化”的思想填值即可。
2.实现代码
需要构建的JSON对象或数组(装数据的”袋子“)代码如下:
JSONObject object = new JSONObject();
JSONObject crsObject = new JSONObject();
JSONObject properties1Object = new JSONObject();
JSONArray featuresArray = new JSONArray();
JSONObject featuresObj1 = new JSONObject();
JSONObject properties2Object = new JSONObject();
JSONObject geometryObj = new JSONObject();
JSONArray coordinatesArray= new JSONArray();
2.1 crs对象的处理
crs中properties对象的处理
properties1Object.put("name", "urn:ogc:def:crs:OGC:1.3:CRS84");
向crs中填数据
crsObject.put("type","name");
crsObject.put("properties",properties1Object);
2.2 features对象的处理
2.2.1 features对象中第一个对象中的properties对象的处理(注意有3层)
test.entity.Feature feature=new test.entity.Feature();
feature = ReadTxtFile(jsonpath);
Attributes attributes=feature.getAttributes();
properties2Object.put("userId",attributes.getUserId());
properties2Object.put("farmCode",attributes.getFarmCode());
properties2Object.put("name",attributes.getName());
properties2Object.put("radius",attributes.getRadius());
properties2Object.put("job",attributes.getJob());
properties2Object.put("content",attributes.getContent());
最后向features对象中第一个对象中填数据
featuresObj1.put("id",0);
featuresObj1.put("type","Feature");
featuresObj1.put("properties",properties2Object);
2.2.2 features对象中第二个对象中的coordinates数组的处理
List<List<Double>> coordinates=new ArrayList<>();
List<Double> coordtemp=new ArrayList<>();
for (int i = 0; i < pointsList.size(); i++) {
coordtemp.add(pointsList.get(i).getX());
coordtemp.add(pointsList.get(i).getY());
coordinates.add(coordtemp);
coordtemp.clear();
}
coordtemp.add(pointsList.get(0).getX());
coordtemp.add(pointsList.get(0).getY());
coordinates.add(coordtemp);
coordinatesArray.put(coordinates);
向features对象中第二个对象中填数据
geometryObj.put("coordinates",coordinatesArray);
geometryObj.put("type","Polygon");
最后将第三层两个对象填入features对象
featuresArray.put(featuresObj1);
featuresArray.put(geometryObj);
2.3 最外层对象(整个geojson对象)的处理
object.put("features",featuresArray);
object.put("crs",crsObject);
object.put("name",attributes.getName()+"_polygon");
object.put("type","FeatureCollection");
完整代码如下(不可运行,供代码方便复制)
public static void main(String[] args) throws IOException {
String jsonpath="E:\\河南省乡镇点\\12.txt";
String shpPath = "E:/河南省乡镇点/GIS/";
makeDir(shpPath);
List<Point> pointsList = TxtToPolygon(jsonpath, shpPath);
JSONObject object = new JSONObject();
JSONObject crsObject = new JSONObject();
JSONObject properties1Object = new JSONObject();
JSONArray featuresArray = new JSONArray();
JSONObject featuresObj1 = new JSONObject();
JSONObject properties2Object = new JSONObject();
JSONObject geometryObj = new JSONObject();
JSONArray coordinatesArray= new JSONArray();
properties1Object.put("name", "urn:ogc:def:crs:OGC:1.3:CRS84");
crsObject.put("type","name");
crsObject.put("properties",properties1Object);
test.entity.Feature feature=new test.entity.Feature();
feature = ReadTxtFile(jsonpath);
Attributes attributes=feature.getAttributes();
properties2Object.put("userId",attributes.getUserId());
properties2Object.put("farmCode",attributes.getFarmCode());
properties2Object.put("name",attributes.getName());
properties2Object.put("radius",attributes.getRadius());
properties2Object.put("job",attributes.getJob());
properties2Object.put("content",attributes.getContent());
featuresObj1.put("id",0);
featuresObj1.put("type","Feature");
featuresObj1.put("properties",properties2Object);
List<List<Double>> coordinates=new ArrayList<>();
List<Double> coordtemp=new ArrayList<>();
for (int i = 0; i < pointsList.size(); i++) {
coordtemp.add(pointsList.get(i).getX());
coordtemp.add(pointsList.get(i).getY());
coordinates.add(coordtemp);
coordtemp.clear();
}
coordtemp.add(pointsList.get(0).getX());
coordtemp.add(pointsList.get(0).getY());
coordinates.add(coordtemp);
System.out.println(coordinates);
coordinatesArray.put(coordinates);
geometryObj.put("coordinates",coordinatesArray);
geometryObj.put("type","Polygon");
featuresArray.put(featuresObj1);
featuresArray.put(geometryObj);
object.put("features",featuresArray);
object.put("crs",crsObject);
object.put("name",attributes.getName()+"_polygon");
object.put("type","FeatureCollection");
System.out.println(object.toString());
}
总结
用{}包裹的属于JSON对象,用[]包裹属于JSON数组,对应好层级put相应的值即可。
对象中put”键值对“,其中的”值“可以是基本类型的值,也可以是一个JSON对象或数组(嵌套情况下)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)