【问题标题】:I can't parse a Json file to a java object using Gson我无法使用 Gson 将 Json 文件解析为 java 对象
【发布时间】:2018-01-29 10:16:20
【问题描述】:

我有一个这样的 Json 文件:

{
 "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"
  }
 ]
}

我想用它来创建一个机场对象。

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

我是这样读的:

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

但是如果我执行这段代码,日志会打印一个空数组

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

我认为问题在于第一个属性包含我想要的信息数组。但我不知道如何访问信息。你能给我指路吗?

【问题讨论】:

  • edit你的问题在这里“y lo leo de la siguiente forma”它必须是完整的英文。
  • 对不起,我忘记翻译了
  • 你能把“y lo leo de la siguiente forma”换成“and I read it in the following way”吗?谢谢。
  • 抱歉,我的手机找不到编辑按钮

标签: java android json object gson


【解决方案1】:

创建一个 MyAirports.java 类。

public class MyAirports{
    List<Airport> airports;
    public List<Airport> getAirportList()
    {
        return this.airports;
    }
}

然后做,

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

【讨论】:

  • 您的解决方案有效,但我不得不输入 airports.airports.get(0),因为 Android Studio 在 MyAirports 类中看不到 airports 变量
【解决方案2】:

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

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]。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多