简体   繁体   中英

Parsing JSON Array using GSON

I have a basic JSON with all data contained in an array. One would think that it would be simple to retreive a value out of the array, but after multiple hours of trying every different method of parsing I could think of I'm completely lost. Any help would be greatly appreciated. Sorry for the horrid wording of this question.

I know I've attempted reading the JSON as an object using JsonReader and then parsing for the ID field. That would be my latest attempt, the code for the other attempts has already been deleted I'm afraid and I can't provide much information on said attempts

JsonReader reader = new JsonReader(new FileReader(Constants.VersJson));
        reader.beginObject();

        while (reader.hasNext()) {

          String name = reader.nextName();

          reader.beginArray();

          if (name.equals("id")) {

            System.out.println(reader.nextString());

Below I'll include a snippet of the JSON Array.

"versions": [
{
  "id": "2.7",
  "time": "2012-10-25T15:00:00+02:00",
  "releaseTime": "2013-10-25T15:00:00+02:00",
  "type": "Release"
},
{
  "id": "2.6.4",
  "time": "2011-12-2T14:01:07+02:00",
  "releaseTime": "2013-12-2T14:01:07+02:00",
  "type": "Develop"
},
{
  "id": "2.5",
  "time": "2010-11-24T21:05:00+02:00",
  "releaseTime": "2013-11-25T01:04:05+02:00",
  "type": "Develop"

Your json format is not correct which you have posted here correct it for example

{ 
   "versions":[
      {
         "id":"2.7",
         "time":"2012-10-25T15:00:00+02:00",
         "releaseTime":"2013-10-25T15:00:00+02:00",
         "type":"Release"
      },
      {
         "id":"2.6.4",
         "time":"2011-12-2T14:01:07+02:00",
         "releaseTime":"2013-12-2T14:01:07+02:00",
         "type":"Develop"
      }
   ]
} 

First Define Classes you will get everything

public class Version {

       private List<Versions> versions;

       public List<Versions> getVersions() {
           return versions;
       }

       public void setVersions(List<Versions> versions) {
           this.versions = versions;
       }

       @Override
       public String toString() {
           return "Version [versions=" + versions + "]";
       }
}

public class Versions {

    private String  id;
    private String  time;
    private String  releaseTime;
    private String  type;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getTime() {
        return time;
    }
    public void setTime(String time) {
        this.time = time;
    }
    public String getReleaseTime() {
        return releaseTime;
    }
    public void setReleaseTime(String releaseTime) {
        this.releaseTime = releaseTime;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    @Override
    public String toString() {
        return "Versions [id=" + id + ", time=" + time + ", releaseTime="
                + releaseTime + ", type=" + type + "]";
    }
}

Finally you can parse the JSON as like here

JsonReader reader = new JsonReader(new FileReader(Constants.VersJson));
Gson gson = new Gson();
Version version = gson.fromJson(reader, Version.class);

i have also faced json array parsing using gson here is my code solved it

this is my reader class functions

 JsonReader reader = new JsonReader(new InputStreamReader(new FileInputStream(myFile))); System.out.println( reader); Gson gson = new Gson(); JsonParser parser = new JsonParser(); JsonArray jArray = parser.parse(reader).getAsJsonArray(); ArrayList<JsonOperations> lcs = new ArrayList<JsonOperations>(); for(JsonElement obj : jArray ) { JsonOperations cse = gson.fromJson( obj , JsonOperations.class); lcs.add(cse); } for ( JsonOperations tUser : lcs) { System.out.println(tUser); } 

my json operation class is

 public class JsonOperations { String match_id, pool_name, team1_name, team1_image, team2_name, team2_image, match_date, match_country, match_venue, predicted; public JsonOperations() { } public JsonOperations(String match_id, String pool_name, String team1_name, String team1_image, String team2_name, String team2_image, String match_date, String match_country, String match_venue, String predicted) { this.match_id = match_id; this.pool_name = pool_name; this.team1_name = team1_name; this.team1_image = team1_image; this.team2_name = team2_name; this.team2_image = team2_image; this.match_date = match_date; this.match_country = match_country; this.match_venue = match_venue; this.predicted = predicted; } public void set_team1(String team1_name) { this.team1_name = team1_name; } public void set_team2(String team2_name) { this.team2_name = team2_name; } public String get_team1() { return team1_name; } public String get_team2() { return team2_name; } @Override public String toString() { // TODO Auto-generated method stub return this.get_team1() + this.get_team2(); } } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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