使用GSON解析JSON数组和Object

时间:2018-01-15 08:06:48

标签: java json object gson

我有一个像这样的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和位置值

4 个答案:

答案 0 :(得分: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中的变量,而不是单独的列表。

答案 1 :(得分:0)

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

}

您应该将每个字段映射到对象&amp;然后使用你需要的任何东西。

  

注意 - 将您的实例变量设为私有&amp;提供吸气剂和制定者   对于领域。这是POJO课程中的一个好习惯。

答案 2 :(得分:0)

使用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 :(得分:0)

因为上述 Sagar Nayak 是正确的。

  

科特琳

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

// when json variable is your mockup data

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