How to parse Array of JSON to array in Swift

后端 未结 5 675
南方客
南方客 2020-12-05 12:02

I\'m trying to parse JSON which is like below

[
  {
    "People": [
      "Jack",
      "Jones",
      "Rock",
      &         


        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-05 12:27

    I couldn't pasted it in a comment, it is too long or something

    static func photosFromJSONObject(data: Data) -> photosResult {
        do {
            let jsonObject: Any =
                    try JSONSerialization.jsonObject(with: data, options: [])
    
            print(jsonObject)
    
            guard let
                  jsonDictionary = jsonObject as? [NSObject: Any] as NSDictionary?,
                  let trackObject = jsonDictionary["track"] as? [String: Any],
                  let album = trackObject["album"] as? [String: Any],
                  let photosArray = album["image"] as? [[String: Any]]
                    else {
                return .failure(lastFMError.invalidJSONData)
            }
        }
    }
    

    And the json was something like:

    {
      artist: {
        name: Cher,
        track: {
            title: WhateverTitle,
            album: {
              title: AlbumWhatever,
              image: {
                 small: "image.px",
                 medium: "image.2px",
                 large: "image.3px"}
           ....
    

提交回复
热议问题