将Geopoint转换为json字符串问题

时间:2012-10-14 23:56:54

标签: android json gps location gson

我正在使用Gson将包含两个GeoPoints的类转换为JSON字符串。下面的代码在我的类中实现LocationListener ...

lat = (float) loc.getLatitude();
lng = (float) loc.getLongitude();
alt = (float) loc.getAltitude();

GeoPoint nextGeoPoint = new GeoPoint((int)(lat * 1E6), (int)(lng * 1E6));
MapPlot mapleg = new MapPlot();
mapleg.fromPoint = LastGeoPoint;
mapleg.toPoint = nextGeoPoint;              

Gson gson = new Gson();
String jsonString = gson.toJson(mapleg); //convert the mapleg class to a json string

我期望JSON字符串有两组纬度和经度但是生成了以下JSON字符串......

{"fromPoint":{"mMapPoint":{"latitudeE6":33736724,"longitudeE6":-118101837,"pixelCoordX":92309232,"pixelCoordY":214935878}},"toPoint":{"mMapPoint":{"latitudeE6":37422004,"longitudeE6":-122084091,"pixelCoordX":86370464,"pixelCoordY":208176089}}}

像素坐标是什么? GeoPoint上的文档与像素无关。当我稍后获取这些数据并将其绘制在mapView上但我尚未到达时,我解决了这个问题。奇怪。

1 个答案:

答案 0 :(得分:0)

GSON查看对象上的字段而不是getter方法,GeoPoints实际上具有那些私有字段以及您感兴趣的字段。请参阅此处以获取在GeoPoint上访问这些字段的请求:http://code.google.com/p/android/issues/detail?id=21327

所以GSON基本上尝试序列化对象的整个状态,但是你只想要外部可见的东西。您可以通过创建Gson对象来更改行为:

GsonBuilder().excludeFieldsWithModifiers(Modifier.PRIVATE, Modifier.PROTECTED).create();