1

I am getting a JSON String {"status":208,"routes":[1,9,3]} from a Jersey project in my Android app. The JSON String has no flag name for it. Is it possibale to parse it with GSON libarary ? I am just actually intressted just in the ArrayList routes values in it. I have done the following but I dont have a List to get. How can I do that with Gson?

doInbackground method:

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:

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:

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;
    } 

}

1 Answer 1

2

Your problem is that the route attribute in StopsJSON class doesn't match the routes field in the json structure. To solve the problem, you can either change the StopsJSON class's attribute to routes or add an annotation like so

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

This is an easy way on how to do it:

Create class StopsJSON as follow

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 gson = new Gson();
StopsJSON data = gson.fromJson(sb.toString(), StopsJSON.class);

Get your routes

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

Output:

[1, 9, 3]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.