JSON来在Android的POJO映射 [英] Json to POJO mapping in Android

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

问题描述

什么是JSON处理过的REST框架Android中好的做法。举例来说,如果我有一定的JSON结果如下(或任何其他的,我只是给更复杂的东西):

What are good practice for handling json over a Rest Framework in Android. For instance, if I get a certain json result as follow (or any other, I'm just giving something more complex):

{"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)我应该手动处理的结果,让每个值来填充我的用户界面......(不是真的)

1) Should I handle the result manually and get each value to populate my ui... (Not really)

2)我应该创建一个POJO为每个对象(来处理的映射,用的JSONObject)。在我的例子,我将不得不创建一个处理所有的参数,甚至创造更多的POJO电梯对象,使用例如图像和可能的位置。 (所以基本上,我经常需要检查我的API的REST框架,看看我的目标是在服务器端完成,我重复我的模型从服务器向Android客户端)。

2) Should I create a POJO for each object (to handle the mapping, with JSONObject). In my example, I will have to create a lift object that handle all the parameters and even create more POJO, to use for instance image and probably locations. (so basically, I constantly need to check my api rest framework to see how my object are done on server side, I'm duplicating my models from server to the android client).

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

3) Is there any framework to handle the mapping (serialize and deserialization).

我目前使用的选项号2,但不知道是否有更好的东西在那里。它的工作对我来说,到目前为止,用于接收和发送。

I'm currently using option number 2, but was wondering if there was something better out there. It's working for me so far, for receiving and sending.

推荐答案

我喜欢创造每个API端点响应对象,其中i映射呼叫的响应。

I like to create a response object per api endpoint where i map the response of the call.

对于给定的例子,使用 GSON ,响应对象会是类似以下

For the given example and using GSON, the response object would be something like the following

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;
    }
}

这篇关于JSON来在Android的POJO映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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