【问题标题】:Using GSON to parse a JSON array and Object使用 GSON 解析 JSON 数组和 Object
【发布时间】:2018-01-15 08:06:48
【问题描述】:

我有一个这样的 JSON 文件:

{
 "waypoints": [
    {
        "waypoint_index": 0,
        "trips_index": 0,
        "hint": "u_FYj4uKI=",
        "name": "",
        "location": [
            28.068655,
            41.180774
        ]
    },
    {
        "waypoint_index": 4,
        "trips_index": 0,
        "hint": "KiKhg4uKI=",
        "name": "",
        "location": [
            20.75179,
            29.031869
        ]
    }
}

我知道如果您想创建 java 对象,那么您只需了解 JSON 的工作原理即可。

{} -> object

[] -> array

但我做不到!

如何将 Java 对象转换为这个 json 文件?

public class ResultOsrm {
    public Waypoints waypoints;
}

public class Waypoints {
    public List waypoint_index;

}

主要 -

Gson gson = new GsonBuilder().create();                             
ResultOsrm resultOsrm=gson.fromJson(JsonFile,ResultOsrm.class); 
System.out.println(resultOsrm);    

我只需要 waypoint_index 和位置值

【问题讨论】:

    标签: java json object gson


    【解决方案1】:

    我认为ResultOsrm 应该保存Waypoint 的列表,而Waypoint 类将保存数据

    public class ResultOsrm
    {
        public List<Waypoint> waypoints;
    }
    
    public class Waypoint
    {
        public int waypoint_index;
        public int trips_index;
        public String hint;
        public String name;
        public List<float> location;
    }
    

    waypoint_indexWaypoint 中的一个变量,而不是一个列表。

    【讨论】:

    • 谢谢@Guy!它运行了。
    【解决方案2】:

    使用 Kotlin 定义数据类。

    WayPoint.kt

    data class WayPoint(
        @SerializedName("waypoint_index") var wayPointIndex: String,
        @SerializedName("trips_index") var tripIndex: String,
        @SerializedName("hint") var hint: String,
        @SerializedName("name") var name: String,
        @SerializedName("location") var location: ArrayList<String>
    

    )

    Response.kt

    data class Response(
        @SerializedName("waypoints") var wayPoints: ArrayList<WayPoint>
    

    )

    然后将字符串转换为 JSON 和 Java 类中的对象

        String data = "{\n" +
                " \"waypoints\": [\n" +
                "    {\n" +
                "        \"waypoint_index\": 0,\n" +
                "        \"trips_index\": 0,\n" +
                "        \"hint\": \"u_FYj4uKI=\",\n" +
                "        \"name\": \"\",\n" +
                "        \"location\": [\n" +
                "            28.068655,\n" +
                "            41.180774\n" +
                "        ]\n" +
                "    },\n" +
                "    {\n" +
                "        \"waypoint_index\": 4,\n" +
                "        \"trips_index\": 0,\n" +
                "        \"hint\": \"KiKhg4uKI=\",\n" +
                "        \"name\": \"\",\n" +
                "        \"location\": [\n" +
                "            20.75179,\n" +
                "            29.031869\n" +
                "        ]\n" +
                "    }\n" +
                "\t]\n" +
                "}";
    
        Response response = new Gson().fromJson(
                data,
                Response.class
        );
    

    响应类将您的所有数据转换为数据类值。

    请尝试此解决方案。这肯定会解决您的问题。

    【讨论】:

      【解决方案3】:

      您的 JSON 结构在这里有点不正确 -

      {
           "waypoints": [
              {
                  "waypoint_index": 0,
                  "trips_index": 0,
                  "hint": "u_FYj4uKI=",
                  "name": "",
                  "location": [
                      28.068655,
                      41.180774
                  ]
              },
              {
                  "waypoint_index": 4,
                  "trips_index": 0,
                  "hint": "KiKhg4uKI=",
                  "name": "",
                  "location": [
                      20.75179,
                      29.031869
                  ]
              }
          ]
      }
      

      waypoints 没有右数组括号。

      另外,您需要根据 JSON 修改您的类结构 -

      public class ResultOsrm {
          private List<Waypoints> waypointsList;
          // getter setter
      }
      
      public class Waypoints {
          private Integer waypoint_index;
          private List<Double> location;
          // other fields & all getter setters
      
      }
      

      您应该将每个字段映射到对象中,然后使用您需要的任何内容。

      注意 - 将您的实例变量设为私有并提供 getter 和 setter 对于字段。这是 POJO 类中的一个好习惯。

      【讨论】:

        【解决方案4】:

        正如上面提到的 Sagar Nayak 是正确的。

        科特林

        如果您想将模型数据转换为 ArrayList,您可以使用它。如果您不想创建包含 ArrayList 属性的模型,这是其他选项。

        // when json variable is your mockup data
        
        val list= Gson().fromJson<ArrayList<WayPoint>>(json, Array<WayPoint>::class.java).toCollection(ArrayList())
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-08-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-12-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多