Android 无法将JSONObject强制转换为com.parse.ParseGeoPoint
我正在使用以下命令在JsonObject中保存地质点:Android 无法将JSONObject强制转换为com.parse.ParseGeoPoint,android,json,parse-platform,Android,Json,Parse Platform,我正在使用以下命令在JsonObject中保存地质点: JSONObject obj=new JSONObject(); JSONArray jA=new JSONArray(); if(lx.size()==0){ Toast.makeText(ctx, "No location to upload now", Toast.LENGTH_LONG).show(); } else{
JSONObject obj=new JSONObject();
JSONArray jA=new JSONArray();
if(lx.size()==0){
Toast.makeText(ctx, "No location to upload now", Toast.LENGTH_LONG).show();
}
else{
for(int uq=0;uq<lx.size();uq++){
Double latit=lx.get(uq).getLatit();
Double longit=lx.get(uq).getLongit();
ParseGeoPoint pgPoint=new ParseGeoPoint(latit,longit);
jA.put(pgPoint);
}
try {
obj.put("locations",jA);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
现在我试着用这个来找回它:
JSONArray locations;
ParseGeoPoint
locations = obj.getJSONArray("locations");
for (int yx = 0; yx < locations.length(); yx++) {
pg =(ParseGeoPoint)locations.get(yx);
//draw this geopoint on googlemap
}
我得到了类型不匹配的错误。当我将其类型转换为parse对象时,它运行良好;但当我尝试运行apk时,我遇到了以下错误:
Caused by: java.lang.ClassCastException: org.json.JSONObject cannot be cast to com.parse.ParseGeoPoint
尝试转到parse.com数据页面并将JSONObject复制粘贴到jsonlint.com,然后您将看到保存在JSONObject中的pg的确切外观。它本质上是一个字符串值,可以被视为JSONObject,但无法将字符串或JSONObject直接转换为ParseGeoPoint。 您最多可以进入一个窗口,检索lat/lon值:
JSONArray locations;
ParseGeoPoint
locations = obj.getJSONArray("locations");
for (int yx = 0; yx < locations.length(); yx++) {
pg = locations.getJSONObject(yx);
Double lat = pg.getDouble("latitude")
Double lon = pg.getDouble("longitude")
//draw this geopoint on googlemap
}
现在,我还没有像您那样尝试将ParseGeoPoint保存为JSONObject,因此不确定上述内容是否正确,只是一个示例
如果您只需要能够存储和检索位置,那么您只需执行以下操作:
...
for(int uq=0;uq<lx.size();uq++){
Double latit=lx.get(uq).getLatit();
Double longit=lx.get(uq).getLongit();
JSONObject jsonPos = new JsonObject()
json.put("lat", latit);
json.put("lon", longit);
jA.put(jsonPos);
}
...
及
我可以问你为什么要保存一个JSON对象来解析吗?获取它时,locations.getyx只是一个JSONObject,而不是ParseGeoPoint。您可能需要根据JSONObject的字段手动构建一个或使用一个库。顺便说一句,强制转换是在运行时执行的,因此您可以成功编译代码
JSONArray locations;
ParseGeoPoint
locations = obj.getJSONArray("locations");
for (int yx = 0; yx < locations.length(); yx++) {
pg = locations.getJSONObject(yx);
Double lat = pg.getDouble("latitude")
Double lon = pg.getDouble("longitude")
//draw this geopoint on googlemap
}
...
for(int uq=0;uq<lx.size();uq++){
Double latit=lx.get(uq).getLatit();
Double longit=lx.get(uq).getLongit();
JSONObject jsonPos = new JsonObject()
json.put("lat", latit);
json.put("lon", longit);
jA.put(jsonPos);
}
...
JSONArray locations;
ParseGeoPoint
locations = obj.getJSONArray("locations");
for (int yx = 0; yx < locations.length(); yx++) {
pg =locations.getJSONObject(yx);
Double lat = pg.getDouble("lat")
Double lon = pg.getDouble("lon")
//draw this geopoint on googlemap
}