android中频繁创建对象,如何通过在android中重复循环来创建json对象?

该博客讲述了在Android环境中遇到的问题,即尝试从Shape文件读取点数据并将其转换为JSON格式时,JSON对象只存储了最后一个记录点73次。作者使用了ShapeReader库来读取Shape文件,并在循环中处理点数据,但循环逻辑似乎存在错误,导致了重复存储。博客中提供了详细的代码片段以展示问题所在。
摘要由CSDN通过智能技术生成

我想通过得到的形状文件的点,而loop.The形状文件包含73个points.I我存储形状文件的点JSON oject并添加对象的ArrayList在一个循环,while循环下面创建JSON对象即时检索ArrayList的对象,并将其存储在JsonArray中,但这里的问题只是shapefile的最后一个记录点在arraylist中存储73次。如何解决这个问题请帮助我。如何通过在android中重复循环来创建json对象?

这里是我的代码

try {

File file= new File(Environment.getDataDirectory().getAbsolutePath()+"/ne_50m_admin_0_tiny_countries.shp");

Long fileLength=file.length();

Log.d("FileLength is","file"+fileLength);

String shpFile = Environment.getDataDirectory().getAbsolutePath().toString()+"/ne_50m_admin_0_tiny_countries.shp";

//String shpFile = Environment.getDataDirectory().getAbsolutePath().toString()+"/ne_50m_admin_0_breakaway_disputed_areas.shp";

ShapeReader reader = new ShapeReader(shpFile, true);

ShapeType shpType=reader.getHeader().getShapeType();

JSONObject jObject = new JSONObject();

JSONObject wkidJson = new JSONObject();

jObject.put("objectIdFieldName","OBJECTID");

jObject.put("globalIdFieldName","");

jObject.put("geometryType",shpType);

wkidJson.put("wkid", new Integer(102100));

jObject.put("spatialReference",wkidJson);

JSONArray fieldsList = new JSONArray();

JSONObject idJson = new JSONObject();

JSONObject yJson = new JSONObject();

JSONObject xJson = new JSONObject();

idJson.put("name","OBJECTID");

idJson.put("alias", "OBJECTID");

idJson.put("type", "esriFieldTypeOID");

yJson.put("name","y");

yJson.put("alias", "y");

yJson.put("type", "esriFieldTypeDouble");

xJson.put("name","x");

xJson.put("alias", "x");

xJson.put("type", "esriFieldTypeDouble");

fieldsList.put(idJson);

fieldsList.put(yJson);

fieldsList.put(xJson);

jObject.put("fields",fieldsList);

JSONObject geomJson = new JSONObject();

JSONObject attJson = new JSONObject();

JSONObject featuresJson = new JSONObject();

JSONArray featuresList = new JSONArray();

Log.d("Header info is","Header"+reader.getHeader().getFileLength());

recordshape=reader.new Record();

ArrayList a= new ArrayList();

while (reader.hasNext())

{

if(recordshape.end < fileLength)

{

if(shpType==ShapeType.POINT)

{

recordshape=reader.nextRecord();

int recordNumber=recordshape.number;

double Xcoordinate= recordshape.minX;

double Ycoordinate=recordshape.minY;

Log.d("Xcoordinate info is","Xcoordinate"+Xcoordinate);

Log.d("Ycoordinate info is","Ycoordinate"+Ycoordinate);

geomJson.put("x", Xcoordinate);

geomJson.put("y", Ycoordinate);

attJson.put("OBJECTID", recordNumber);

attJson.put("y", Ycoordinate);

attJson.put("x", Xcoordinate);

}

}

else

break;

featuresJson.put("geometry", geomJson);

featuresJson.put("attributes", attJson);

a.add(featuresJson);

Log.d("featuresJson info is","featuresJson"+featuresJson);

}

Log.d("featuresJson info is","featuresJson"+featuresJson);

Log.d("size info is","size"+a.size());

for(int i=0;i

{

featuresList.put(a.get(i));

}

Log.d("featuresList info is","featuresList"+featuresList);

jObject.put("features",featuresList);

String jsonString = jObject.toString();

jsonString = jsonString.replace("\\" , "");

FileWriter writeFile = new FileWriter(Environment.getDataDirectory().getAbsolutePath().toString()+"/Test.json");

BufferedWriter out = new BufferedWriter(writeFile);

out.write(jsonString);

out.flush();

out.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

Log.d("Error","Message"+e.getMessage());

}catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

Log.d("Exception","Message"+e.getMessage());

}

的JSON格式的,我想是

{

"objectIdFieldName": "OBJECTID",

"globalIdFieldName": "",

"geometryType": "esriGeometryPoint",

"spatialReference": {

"wkid": 102100

},

"fields": [

{

"name": "OBJECTID",

"alias": "OBJECTID",

"type": "esriFieldTypeOID"

},

{

"name": "y",

"alias": "y",

"type": "esriFieldTypeDouble"

},

{

"name": "x",

"alias": "x",

"type": "esriFieldTypeDouble"

}

],

"features": [

{

"geometry": {

"x": -13021472.272599999,

"y": 4046325.7190999985

},

"attributes": {

"OBJECTID": 212,

"y": 4046325.7191,

"x": -13021472.2726

}

},

{

"geometry": {

"x": -13021124.9134,

"y": 4046747.3976000026

},

"attributes": {

"OBJECTID": 232,

"y": 4046747.3976,

"x": -13021124.9134

}

}

]

}

请帮我谢谢 。

+0

TL;博士。你有什么尝试,什么失败,这会给你带来什么样的错误? –

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值