簡體   English   中英

使用GSON解析JSON數組和對象

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

我有一個像這樣的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和位置值

我認為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的變量,而不是列表本身。

使用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
    );

響應類將所有數據轉換為數據類值。

請嘗試此解決方案。 這肯定會解決您的問題。

您的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

}

您應該將每個字段映射到對象中,然后使用其中的任何內容。

注意-將實例變量設為私有,並為字段提供獲取器和設置器。 在POJO類中,這是一個好習慣。

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

科特林

如果要將模型數據轉換為ArrayList,可以使用此方法。 如果您不想創建其中包含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