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)結果を手動で処理し、各値を取得してUIに入力する必要があります...(そうではありません)

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 に答える