迅速无法挖掘通过alamofire获得的JSONDecoder数据

时间:2019-05-18 14:59:38

标签: json swift alamofire

无法读取通过Almofire获取的JSONDecoder文件的“天气”部分中的数据

控制台中打印的数据:

  

{“ coord”:{“ lon”:-0.13,“ lat”:51.51},“ weather”:[{“ id”:521,“ main”:“ Rain”,“ description”:“ Shower   雨”,“图标”:“ 09d”}],“基本”:“站点”,“主”:{“温度”:289.64,“压力”:1006,“湿度”:48,“温度_分钟”:286.48, “ temp_max”:292.59},“可见性”:10000,“风”:{“速度”:1},“云”:{“ all”:85},“ dt”:1558190870,“ sys”:{“ type “:1,” id“:1414,”消息“:0.009,”国家“:” GB“,”日出“:1558152298,”日落“:1558208948},” id“:2643743,”名称“:”伦敦“ ,“鳕鱼”:200}

private void mergeSort(LinkedList<Integer> list) {
        if (list.size() < 2){
            return;
        }
        int middleindex  = list.size()/2;
        LinkedList<Integer> [] listArr = split(list, middleindex);

        LinkedList<Integer> leftList = listArr[0];
        mergeSort(leftList);

        LinkedList<Integer> rightList = listArr[1];
        mergeSort(rightList);

        sortedMerge(rightList, leftList, list);

    }

    private void sortedMerge(LinkedList<E> right, LinkedList<E> left, LinkedList<E> list){
        //sort and merge
    }


    @SuppressWarnings("unchecked") private static <T> LinkedList<T>[] split(LinkedList<T> list, int index) {
        LinkedList<T> left = new LinkedList<>();

        for (int i = 0; i < index; i++) {
            T t = list.removeFirst();
            left.addLast(t);
        }
        return new LinkedList[] {
                left, list
        };
    }

3 个答案:

答案 0 :(得分:1)

请使用首字母大写来命名您的班级/型号。

问题在于weather是MyWeatherData上的一个数组,因此它变为:

struct MyWeatherData: Codable {
    let coord : Coord
    let weather : [Weather]
}

struct Coord: Codable {
    let lon: Double
    let lat: Double
}

struct Weather: Codable {
    let id : Int
    let main: String
    let description : String
    let icon : String
}

答案 1 :(得分:1)

MyWeatherData中,weather属性的类型应为[weather],因为JSON使用weather键返回一个数组:

{
  "coord": {
    "lon": -0.13,
    "lat": 51.51
  },
  "weather": [{
    "id": 521,
    "main": "Rain",
    "description": "shower rain",
    "icon": "09d"
  }],
  "base": "stations",
  "main": {
    "temp": 289.64,
    "pressure": 1006,
    "humidity": 48,
    "temp_min": 286.48,
    "temp_max": 292.59
  },
  "visibility": 10000,
  "wind": {
    "speed": 1
  },
  "clouds": {
    "all": 85
  },
  "dt": 1558190870,
  "sys": {
    "type": 1,
    "id": 1414,
    "message": 0.009,
    "country": "GB",
    "sunrise": 1558152298,
    "sunset": 1558208948
  },
  "id": 2643743,
  "name": "London",
  "cod": 200
}

因此您的类型应如下所示:

struct MyWeatherData: Codable {
    let coord: coord
    let weather: [weather]
    let base: String
}

struct coord: Codable {
    let lon: Double
    let lat: Double
}

struct weather : Codable {
    let id : Int
    let main: String
    let description : String
    let icon : String
}

然后您可以通过weather来获得myWeatherData.weather.first实例

答案 2 :(得分:0)

不,问题不是这是我的问题,问题出在MyWeatherData

阅读 JSON。很简单密钥weather的值包装在[]中,因此该对象是一个数组。

并使用大写字母命名所有结构,以免引起混淆,例如let weather : weather

struct MyWeatherData : Decodable {
    let coord : Coord
    let weather : [Weather]
}

struct Coord : Decodable {
    let lon: Double
    let lat: Double
}

struct Weather : Decodable {
    let id : Int
    let main: String
    let description : String
    let icon : String
}