首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我不能使用Gson将Json文件解析为java对象。

我不能使用Gson将Json文件解析为java对象。
EN

Stack Overflow用户
提问于 2018-01-29 18:16:21
回答 2查看 752关注 0票数 2

我有一个这样的Json文件:

代码语言:javascript
复制
{
 "airports": [
  {
   "fs": "VGO",
   "iata": "VGO",
   "icao": "LEVX",
   "name": "Vigo Airport",
   "city": "Vigo",
   "cityCode": "VGO",
   "stateCode": "SP",
   "countryCode": "ES",
   "countryName": "Spain and Canary Islands",
   "regionName": "Europe",
   "timeZoneRegionName": "Europe/Madrid",
   "localTime": "2018-01-29T08:59:15.661",
   "utcOffsetHours": 1,
   "latitude": 42.224551,
   "longitude": -8.634025,
   "elevationFeet": 860,
   "classification": 4,
   "active": true,
   "weatherUrl": "https://api.flightstats.com/flex/weather/rest/v1/json/all/VGO?codeType=fs",
   "delayIndexUrl": "https://api.flightstats.com/flex/delayindex/rest/v1/json/airports/VGO?codeType=fs"
  }
 ]
}

我想用来创建一个airport对象。

代码语言:javascript
复制
public class Airport {

    String iata;
    String name;
    String city;
    String countryName;
    String regionName;
    String timeZoneRegionName;
    double utcOffsetHours;

    double latitude;
    double longitude;
    int elevationFeet;

    @Override
    public String toString() {
        return "Airports{" +
                "iata='" + iata + '\'' +
                ", name='" + name + '\'' +
                ", city='" + city + '\'' +
                ", countryName='" + countryName + '\'' +
                ", regionName='" + regionName + '\'' +
                ", timeZoneRegionName='" + timeZoneRegionName + '\'' +
                ", utcOffsetHours=" + utcOffsetHours +
                ", latitude=" + latitude +
                ", longitude=" + longitude +
                ", elevationFeet=" + elevationFeet +
                '}';
    }
}

我是这样读的:

代码语言:javascript
复制
public void imprimirJson(String fileName) {

        String filePath = getCacheDir() + "/" + fileName + ".json";
        Gson gson = new Gson();
        Airport airport = null;

        try {
            airport = gson.fromJson(new FileReader(filePath), Airport.class);
        } catch (FileNotFoundException e) {
        }
        Log.i("MSG", airport.toString());
    }

但如果我执行此代码,日志将打印一个空数组

代码语言:javascript
复制
public void printJson(String fileName) {

        String filePath = getCacheDir() + "/" + fileName + ".json";
        Gson gson = new Gson();
        Airport airport = null;

        try {
            airport = gson.fromJson(new FileReader(filePath), Airport.class);
        } catch (FileNotFoundException e) {
        }
        Log.i("MSG", airport.toString());
    }

我认为问题在于第一个属性有一个我想要的信息数组。但我不知道怎么获取信息。你能给我带路吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-01-29 18:23:40

创建一个类MyAirports.java。

代码语言:javascript
复制
public class MyAirports{
    List<Airport> airports;
    public List<Airport> getAirportList()
    {
        return this.airports;
    }
}

而且确实如此,

代码语言:javascript
复制
 public void printJson(String fileName) {

    String filePath = getCacheDir() + "/" + fileName + ".json";
    Gson gson = new Gson();
    MyAirports airports = null;

    try {
        //airport = gson.fromJson(new FileReader(filePath), Airport.class);
        airports = gson.fromJson(new FileReader(filePath), MyAirports.class);
    } catch (FileNotFoundException e) {
    }
    Log.i("MSG", airports.getAirportList().get(0).toString());
}
票数 1
EN

Stack Overflow用户

发布于 2018-01-29 18:30:24

json文件中"airports“的值是一个JsonArray。因此,您可以像这样实现:

代码语言:javascript
复制
public void imprimirJson(String fileName) {

        String filePath = getCacheDir() + "/" + fileName + ".json";
        Gson gson = new Gson();
        Airport[] airport = null;

        try {
            airport = gson.fromJson(new FileReader(filePath), Airport[].class);
        } catch (FileNotFoundException e) {
        }
        Log.i("MSG", airport.toString());
    }

稍后,airport就是您要打印的内容。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48498879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档