【问题标题】:How can I parse JSON Array Using Retrofit and GSON?如何使用 Retrofit 和 GSON 解析 JSON 数组?
【发布时间】:2015-12-05 05:39:01
【问题描述】:

我已经学会了如何通过 Retrofit Gson 解析 JSON 对象,但我需要通过 Retrofit Gson 解析完整的 JSON 数组。

我需要解析以下内容:

"{\n" +
        "  \"snappedPoints\": [\n" +
        "    {\n" +
        "      \"location\": {\n" +
        "        \"latitude\": -35.2784167,\n" +
        "        \"longitude\": 149.1294692\n" +
        "      },\n" +
        "      \"originalIndex\": 0,\n" +
        "      \"placeId\": \"ChIJoR7CemhNFmsRQB9QbW7qABM\"\n" +
        "    },\n" +
        "    {\n" +
        "      \"location\": {\n" +
        "        \"latitude\": -35.280321693840129,\n" +
        "        \"longitude\": 149.12908274880189\n" +
        "      },\n" +
        "      \"originalIndex\": 1,\n" +
        "      \"placeId\": \"ChIJiy6YT2hNFmsRkHZAbW7qABM\"\n" +
        "    },\n" +
        "    {\n" +
        "      \"location\": {\n" +
        "        \"latitude\": -35.280960897210818,\n" +
        "        \"longitude\": 149.1293250692261\n" +
        "      },\n" +
        "      \"originalIndex\": 2,\n" +
        "      \"placeId\": \"ChIJW9R7smlNFmsRMH1AbW7qABM\"\n" +
        "    },\n" +
        "    {\n" +
        "      \"location\": {\n" +
        "        \"latitude\": -35.28142839817933,\n" +
        "        \"longitude\": 149.1298619971291\n" +
        "      },\n" +
        "      \"originalIndex\": 3,\n" +
        "      \"placeId\": \"ChIJy8c0r2lNFmsRQEZUbW7qABM\"\n" +
        "    },\n" +
        "    {\n" +
        "      \"location\": {\n" +
        "        \"latitude\": -35.28193988170618,\n" +
        "        \"longitude\": 149.13001013387623\n" +
        "      },\n" +
        "      \"originalIndex\": 4,\n" +
        "      \"placeId\": \"ChIJ58xCoGlNFmsRUEZUbW7qABM\"\n" +
        "    },\n" +
        "    {\n" +
        "      \"location\": {\n" +
        "        \"latitude\": -35.282819705480151,\n" +
        "        \"longitude\": 149.1295597114644\n" +
        "      },\n" +
        "      \"originalIndex\": 5,\n" +
        "      \"placeId\": \"ChIJabjuhGlNFmsREIxAbW7qABM\"\n" +
        "    },\n" +
        "    {\n" +
        "      \"location\": {\n" +
        "        \"latitude\": -35.283139388422363,\n" +
        "        \"longitude\": 149.12895618087012\n" +
        "      },\n" +
        "      \"originalIndex\": 6,\n" +
        "      \"placeId\": \"ChIJ1Wi6I2pNFmsRQL9GbW7qABM\"\n" +
        "    },\n" +
        "    {\n" +
        "      \"location\": {\n" +
        "        \"latitude\": -35.284728724835304,\n" +
        "        \"longitude\": 149.12835061713685\n" +
        "      },\n" +
        "      \"originalIndex\": 7,\n" +
        "      \"placeId\": \"ChIJW5JAZmpNFmsRegG0-Jc80sM\"\n" +
        "    }\n" +
        "  ]\n" +
        "}"

我只需要经纬度。

这是我知道的通过 Retrofit GSON 解析 JSON 对象的方法

package com.example.akshay.retrofitgson;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

/**
 * Created by Akshay on 9/6/2015.
 */
public class gitmodel {
    @SerializedName("latitude")
    @Expose
    public String latitude;
    @SerializedName("longitude")
    @Expose
    public String longitude;

    public void setLatitude(String latitude)
    {
        this.latitude = latitude;
    }
    public String getLatitude()
    {
        return latitude;
    }
    public void setLongitude(String longitude)
    {
        this.longitude = longitude;
    }

    public String getlatitude()
    {
        return  latitude;
    }
}

【问题讨论】:

    标签: java android json gson retrofit


    【解决方案1】:

    您可能只需要纬度/经度,但最简单的方法是设置您的 POJO 以获取所有内容,然后从 POJO 中提取经度。如果需要,您甚至可以设计反序列化对象以隐藏内部对象。对于您的 JSON,这很容易,只需执行以下操作:

    public static class SnappedPoints {
      private List<Point> snappedPoints;
    
      public String toString() {
        return snappedPoints.toString();
      }
    }
    
    public static class Point {
      private Location location;
    
      public double getLatitude() {
        return location.getLatitude();
      }
    
      public double getLongitude() {
        return location.getLongitude();
      }
    
      public String toString() {
        return "{" + location.getLatitude() + "," + location.getLongitude() + "}";
      }
    }
    
    public static class Location {
      double latitude;
      double longitude;
    
      public double getLatitude() {
        return latitude;
      }
      public double getLongitude() {
        return longitude;
      }
    }
    

    您只需执行以下操作即可看到这一点:

    public static void main(String[] args) {
      System.out.println(new Gson().fromJson(json, SnappedPoints.class));
    }
    

    或者,在改造的情况下,是这样的:

    public interface MyRetrofitAPI {
      @GET("/path/to/json")
      void getSnappedPoints(/* arguments */, Callback<SnappedPoints> callback);
    }
    

    【讨论】:

      【解决方案2】:

      Retrofit 使用 Gson library 序列化 Json 响应。只要您正确设置了模型类,就可以告诉 Gson 尝试将 json 序列化为该模型类的实例。您的代码应如下所示:

      String json; // retrieved from file/server
      MyObject myObject = new Gson().fromJson(json, MyObject.class)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-07
        • 2021-01-17
        • 2019-09-25
        • 2023-04-01
        相关资源
        最近更新 更多