【问题标题】:Parsing comlpex JSON with SwiftyJSON使用 SwiftyJSON 解析复杂的 JSON
【发布时间】:2019-04-19 01:08:54
【问题描述】:

我一次又一次地尝试从这个 JSON 解析缩略图路径地址

"data":{ 
        "offset":0,
        "limit":20,
        "total":1491,
        "count":20,
        "results":[  
            {  
                "id":1011334,
                "name":"3-D Man",
                "description":"",
                "modified":"2014-04-29T14:18:17-0400",
                "thumbnail":{  
                    "path":"http://i.annihil.us/u/prod/marvel/i/mg/c/e0/535fecbbb9784",
                    "extension":"jpg"
                },

但我似乎无法找到我的问题的答案。我正在这样做:

 for (key, subJson) in json["data", "results", "thumbnail", "path"] {
                    if let imagePath = subJson["path"].string {
                        print(imagePath)
                        let finalImagePath = imagePath + "somestring with my priv key and pub key"
                        print(finalImagePath)
                    }
                }

我这样做是为了减少字符的名称和 ID,如下所示:

for (key, subJson) in json["data"]["results"] {
                    if let name = subJson["name"].string, let id = subJson["id"].int {
                        cardInfo.append(CardInfo(id: id, name: name))
//                        print(cardInfo)
                    }
                }

它工作得非常完美,但在缩略图的情况下,我一直在网上搜索并试图自己弄清楚

如果有人给我答案,我将不胜感激,我已经挣扎了这么久。

提前致谢

【问题讨论】:

    标签: swift swifty-json


    【解决方案1】:

    你可以试试

    if let item = json["data"]["results"].array?.first , let path = item["thumbnail"]["path"].string  { 
       print(path) 
    }
    

    所有人

    for (_, subJson) in json["data"]["results"] {
       if let path = subJson["thumbnail"]["path"].string {
          print(path) 
       }
    }
    

    【讨论】:

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