6

在 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,但想知道那里是否有更好的东西。到目前为止,它对我有用,用于接收和发送。

4

1 回答 1

9

我喜欢为每个 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;
    }
}
于 2012-11-20T03:08:46.793 回答