Json到Android中的POJO映射

时间:2012-11-20 01:58:06

标签: android json pojo

在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)我应该手动处理结果并获取每个值以填充我的ui ...(不是真的)

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

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

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

1 个答案:

答案 0 :(得分:7)

我喜欢为每个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;
    }
}
相关问题