使用 GSON 解析 JSON 数组 [英] Parsing JSON Array using GSON

查看:37
本文介绍了使用 GSON 解析 JSON 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的 JSON,所有数据都包含在一个数组中.有人会认为从数组中检索一个值很简单,但在尝试了多种不同的解析方法后,我觉得我完全迷失了.任何帮助将不胜感激.很抱歉这个问题的措辞很糟糕.

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.

我知道我已经尝试使用 JsonReader 将 JSON 作为对象读取,然后解析 ID 字段.那将是我的最新尝试,其他尝试的代码已经被删除了,我无法提供有关所述尝试的太多信息

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());

下面我将包含 JSON 数组的片段.

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"

推荐答案

您在此处发布的 json 格式不正确例如纠正它

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 + "]";
    }
}

最后你可以像这里一样解析 JSON

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

这篇关于使用 GSON 解析 JSON 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆