Gson映射Lat / Lng类 [英] Gson mapping Lat/Lng class

查看:224
本文介绍了Gson映射Lat / Lng类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务器返回一个像这样的json对象:

  {
sw:{
latitude:12.283139573692,
longitude:76.623518155689
},
ne:{
latitude:12.30112600581,
longitude:76.651167163598


$ / code>

我的Gson类是

  import com.google.android.gms.maps.model.LatLng; 

/ **
*由Moz于12/10/15创建。
* /
public class Box {
LatLng ne;
LatLng sw;

public LatLng getNorthEast(){
return ne;
}

public LatLng getSouthWest(){
return sw;
}

}

在这种情况下, com.google.android.gms.maps.model 下的LatLng类使用名称作为纬度和经度。但是,我宁愿让我的服务器以lat,lng响应,因为它在发送大量latlng对象时节省了空间。这里的问题是,由于LatLng对象是Google库的一部分,我无法添加字段映射名称注释。我想继续使用原来的谷歌的LatLng类,但同时允许服务器返回一个较小的响应,即lat,lng。这是怎么实现的?

解决方案

您是否尝试编写自己的解串器?你应该尝试像这样(未测试)。

  public class LatLngDeserializer实现JsonDeserializer< LatLng> {
$ b @Override
public LatLngdeserialize(final JsonElement json,final Type typeOfT,final JsonDeserializationContext context)
throws JsonParseException {

JsonObject jobject = json。 getAsJsonObject();

返回新的LatLng(
jobject.get(lat)。getAsDouble(),
jobject.get(lng)。getAsDouble());

$ / code>

你必须配置你的gson使用这个解串器

  GsonBuilder gsonBuilder = new GsonBuilder(); 
gsonBuilder.registerTypeAdapter(LatLng.class,new LatLngDeserializer());
Gson gson = gsonBuilder.create();

现在您可以使用
$解析来自服务器的lat / long对象b $ b

  LatLng latlng = gson.fromJson(jsonInput,LatLng.class); 

其中 jsonInput 看起来像



$
$ lat $:b $ b $ l $

$ b $ / code>

您也可以看到线程


My server returns a json object like this:

{
    "sw": {
        "latitude": 12.283139573692,
        "longitude": 76.623518155689
    },
    "ne": {
        "latitude": 12.30112600581,
        "longitude": 76.651167163598
    }
}

And my Gson class is

import com.google.android.gms.maps.model.LatLng;

/**
 * Created by Moz on 12/10/15.
 */
public class Box {
    LatLng ne;
    LatLng sw;

    public LatLng getNorthEast() {
        return ne;
    }

    public LatLng getSouthWest() {
        return sw;
    }

}

It works fine in this case because the LatLng class under com.google.android.gms.maps.model uses names as latitude, longitude. However, I would rather have my server respond as lat, lng since it saves space when sending large sets of latlng objects. The problem here is that since this LatLng object is part of the Google library I’m unable to add a field map name annotation. I would like to remain using the original google’s LatLng class but at the same time allows the server to return a smaller response i.e. lat, lng. How can this be achieved?

解决方案

Did you try to write your own deserializer ? You should try something like this (not tested).

public class LatLngDeserializer implements JsonDeserializer<LatLng> {

  @Override
  public LatLngdeserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context)
      throws JsonParseException {

    JsonObject jobject = json.getAsJsonObject();

    return new LatLng(
        jobject.get("lat").getAsDouble(), 
        jobject.get("lng").getAsDouble());
}

You have to configure your gson to use this deserializer using

GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(LatLng.class, new LatLngDeserializer());
Gson gson = gsonBuilder.create();

Now you can parse lat/long object from your server using

LatLng latlng = gson.fromJson(jsonInput, LatLng.class); 

where jsonInput looks like

{
  "lat" : 0.0,
  "lng" : 0.0
}

You Can also see this thread

这篇关于Gson映射Lat / Lng类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆