简体   繁体   English

使用GSON解析JSON数组和对象

[英]Using GSON to parse a JSON array and Object

I have a JSON file like this: 我有一个像这样的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
        ]
    }
}

I know if you want to create java objects, well, you have just to understand how JSON works. 我知道如果您想创建Java对象,那么您只需要了解JSON的工作原理即可。

{} -> object

[] -> array

but I could not! 但是我做不到!

How to convert Java object to This json file? 如何将Java对象转换为此json文件?

public class ResultOsrm {
    public Waypoints waypoints;
}

public class Waypoints {
    public List waypoint_index;

}

Main - 主-

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

I need just waypoint_index and location values 我只需要waypoint_index和位置值

I think ResultOsrm should hold list of Waypoint and class Waypoint will hold the data 我认为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_index is a variable in Waypoint , not a list by itself. waypoint_indexWaypoint的变量,而不是列表本身。

Use Kotlin to define the data classes. 使用Kotlin定义数据类。

WayPoint.kt 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 Response.kt

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

)

then to convert the string to JSON and to objects in Java class 然后将字符串转换为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
    );

The response class has all your data converted to the data class values. 响应类将所有数据转换为数据类值。

Do try this solution. 请尝试此解决方案。 this will surely solve your issue. 这肯定会解决您的问题。

Your JSON structure is little incorrect here - 您的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
            ]
        }
    ]
}

You don't have closing array bracket for waypoints . 您没有用于waypoints闭合数组括号。

Also, you need to modify your class structure as per the JSON - 另外,您需要根据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

}

You should map every fields into objects & then use whatever you need out of it. 您应该将每个字段映射到对象中,然后使用其中的任何内容。

Note - Make your instance variable private & provide getters & setters for fields. 注意-将实例变量设为私有,并为字段提供获取器和设置器。 This is a good practice to have in POJO classes. 在POJO类中,这是一个好习惯。

As the Sagar Nayak mentioned above is correct. 正如上面提到的Sagar Nayak是正确的。

Kotlin 科特林

if you want to convert mockup data to ArrayList you could use this. 如果要将模型数据转换为ArrayList,可以使用此方法。 It is the other options in case of you don't want to create a model, which contains ArrayList attribute within. 如果您不想创建其中包含ArrayList属性的模型,这是其他选择。

// when json variable is your mockup data

val list= Gson().fromJson<ArrayList<WayPoint>>(json, Array<WayPoint>::class.java).toCollection(ArrayList())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM