【问题标题】:Json to POJO mapping in AndroidAndroid 中 Json 到 POJO 的映射
【发布时间】:2012-11-20 01:58:05
【问题描述】:

在 Android 中通过 Rest Framework 处理 json 有哪些好的做法。例如,如果我得到如下的某个 json 结果(或任何其他结果,我只是给出更复杂的东西):

{"lifts": 
[{
   "id":26,
   "time":"2012-11-21T12:00:00Z",
   "capacity":4,
   "price":10,
   "from": { 
            "description":null,
            "city": {
                      "name":"Montreal"
                    }
           },
    "to":{
           "description":"24 rue de la ville",
           "city":{
                   "name":"Sherbrooke"
                  }
          },
    "driver":{
              "first_name": "Benjamin",  
              "image":"https://graph.facebook.com/693607843/picture?type=large"
             }
    }
]}

1) 我是否应该手动处理结果并获取每个值来填充我的用户界面...(不是真的)

2) 我应该为每个对象创建一个 POJO(使用 JSONObject 处理映射)。在我的示例中,我将必须创建一个电梯对象来处理所有参数,甚至创建更多 POJO,以用于示例图像和可能的位置。 (所以基本上,我经常需要检查我的 api rest 框架以查看我的对象在服务器端是如何完成的,我正在将我的模型从服务器复制到 android 客户端)。

3) 是否有任何框架来处理映射(序列化和反序列化)。

我目前正在使用选项 2,但想知道是否有更好的方法。到目前为止,它对我有用,用于接收和发送。

【问题讨论】:

  • 在我的 APP 中,我使用选项 2 并使用 gson 进行序列化和反序列化

标签: android json pojo


【解决方案1】:

我喜欢为每个 api 端点创建一个响应对象,在其中映射调用的响应。

对于给定的示例并使用GSON,响应对象将类似于以下内容

public class Test
{
    static String jsonString = 
    "{\"lifts\":" + 
    "   [{" +
    "      \"id\":26," +
    "      \"time\":\"2012-11-21T12:00:00Z\"," +
    "      \"capacity\":4," +
    "      \"price\":10," +
    "      \"from\": { " +
    "               \"description\":null," +
    "               \"city\": {" +
    "                         \"name\":\"Montreal\"" +
    "                       }" +
    "               }," +
    "        \"to\":{" +
    "               \"description\":\"24 rue de la ville\"," +
    "               \"city\":{" +
    "                       \"name\":\"Sherbrooke\"" +
    "                      }" +
    "              }," +
    "        \"driver\":{" +
    "                  \"first_name\": \"Benjamin\"," +  
    "                  \"image\":\"https://graph.facebook.com/693607843/picture?    type=large\"" +
    "                 }" +
    "        }" +
    "     ]}";


    public static void main( String[] args )
    {
        Gson gson = new Gson();

        Response response = gson.fromJson( jsonString, Response.class );

        System.out.println( gson.toJson( response ) );
    }


    public class Response
    {
        @SerializedName("lifts")
        List<Lift> lifts;
    }

    class Lift
    {
        @SerializedName("id")
        int id;

        @SerializedName("time")
        String time;

        @SerializedName("capacity")
        int capacity;

        @SerializedName("price")
        float price;

        @SerializedName("from")
        Address from;

        @SerializedName("to")
        Address to;

        @SerializedName("driver")
        Driver driver;
    }

    class Address
    {
        @SerializedName("description")
        String description;

        @SerializedName("city")
        City city;
    }

    class City
    {
        @SerializedName("name")
        String name;
    }

    class Driver
    {
        @SerializedName("first_name")
        String firstName;

        @SerializedName("image")
        String image;
    }
}

【讨论】:

  • 有趣。我会看看 GSON,我听说过它,但从未真正花时间检查它,因为它与 JSONObject 一起工作得很好。
  • 你有使用它的示例项目吗?
  • 我没有任何公开可用的东西。但它很容易使用。你基本上是这样做的: Gson gson = new Gson();响应 r = gson.fromJson( jsonString, Response.class );你就完成了:)
  • 我已经用一个完整的 Test 类编辑了答案,该类包含一个将给定字符串解析为 Response 实例并将其写回 json 的 main 方法。
  • GSON 将使用反射来填充字段,因此您应该为自己的理智添加 getter/setter ;)
猜你喜欢
  • 2017-03-31
  • 2017-03-04
  • 1970-01-01
  • 2017-01-31
  • 1970-01-01
  • 2016-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多