简体   繁体   English

使用GSON解析不包含数组名称的JSON字符串

[英]Parse JSON String without array name with GSON

I am getting a JSON String {"status":208,"routes":[1,9,3]} from a Jersey project in my Android app. 我从Android应用程序的Jersey项目中获取了JSON字符串{"status":208,"routes":[1,9,3]} The JSON String has no flag name for it. JSON字符串没有标志名称。 Is it possibale to parse it with GSON libarary ? 用GSON库解析它是可能的吗? I am just actually intressted just in the ArrayList routes values in it. 实际上,我只是在ArrayList中routes了它的值。 I have done the following but I dont have a List to get. 我已经完成了以下操作,但没有列表。 How can I do that with Gson? 我该如何与Gson做到这一点?

doInbackground method: doInbackground方法:

StringBuilder sb = new StringBuilder();
            reader = new BufferedReader(new InputStreamReader(
                    conn.getInputStream()));
            String line;

            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");

            }

            Gson gson = new Gson();
            StopsJSONList obj = gson.fromJson(sb.toString(), StopsJSONList.class);
            List<StopsJSON> stopsjson = obj.getStopsList();
            Iterator<StopsJSON> iterator = stopsjson.iterator();
            while(iterator.hasNext()){
                StopsJSON  stopsElement = (StopsJSON) iterator.next();
                ArrayList<Integer> routeList = stopsElement.getRoute();

            }

StopsList class: StopsList类:

import java.util.ArrayList;


public class StopsJSONList {
    private ArrayList<StopsJSON> stops;

    public ArrayList<StopsJSON> getStopsList() {
        return stops;
    }

    public void setStopsList(ArrayList<StopsJSON> stopsList) {
        this.stops = stopsList;
    }
}

StopsJSON: StopsJSON:

import java.util.ArrayList;

public class StopsJSON {
    private int status;
    private ArrayList<Integer> route ;


    public StopsJSON(int status, ArrayList<Integer> route) {
        //super();
        this.status = status;
        this.route = route;
    }
    public int getStatus() {
        return status;
    }
    public ArrayList<Integer> getRoute() {
        return route;
    }
    public void setStatus(int status) {
        this.status = status;
    }
    public void setRoute(ArrayList<Integer> route) {
        this.route = route;
    } 

}

Your problem is that the route attribute in StopsJSON class doesn't match the routes field in the json structure. 您的问题是StopsJSON类中的route属性与json结构中的routes字段不匹配。 To solve the problem, you can either change the StopsJSON class's attribute to routes or add an annotation like so 要解决此问题,您可以将StopsJSON类的属性更改为routes或添加一个注释,如下所示

@SerializedName("routes")
private ArrayList<Integer> route ;

This is an easy way on how to do it: 这是一种简单的方法:

Create class StopsJSON as follow 创建类StopsJSON如下

class StopsJSON {
    private int status;
    private ArrayList<Integer> routes;

    public StopsJSON(int status, ArrayList<Integer> route) {
        // super();
        this.status = status;
        this.routes = route;
    }

    public int getStatus() {
        return status;
    }

    public ArrayList<Integer> getRoutes() {
        return routes;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public void setRoutes(ArrayList<Integer> routes) {
        this.routes = routes;
    }

}

Use Gson API to convert json String to a StopsJSON object 使用Gson API将JSON字符串转换为StopsJSON对象

Gson gson = new Gson();
StopsJSON data = gson.fromJson(sb.toString(), StopsJSON.class);

Get your routes 获取routes

ArrayList<Integer> routes = data.getRoutes();
System.out.println(routes);

Output: 输出:

[1, 9, 3]

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

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