nixzhu / Baby

Create models from a JSON file, even a Baby can do it.
https://apps.apple.com/cn/app/ducky-model-editor/id1525505933
MIT License
222 stars 21 forks source link

某些字符串值被错误的映射为 URL #3

Closed cszwdy closed 7 years ago

cszwdy commented 7 years ago

Briefly describe your problem when use Baby.

其中 content_id, img_width, label_title 等值,被转化为 URL。以下是转换的 JSON 和转换后的 Swift 结果。

JSON String

{
  "ret": 200,
  "result": {
    "code": "1",
    "msg": "获取内容成功",
    "info": [
      {
        "content_id": "174",
        "content_title": "",
        "content_imgsrc": "http://oq8t6idvw.bkt.clouddn.com/29c015a4569845bcae1529fe69f5b62b.jpg",
        "content_smallImgSrc": "",
        "img_width": "4000",
        "img_height": "2667",
        "imageAve": "0x6e5f56",
        "labels": [
          {
            "label_id": "126",
            "label_title": "Angelica",
            "label_likeNum": "0"
          },
          {
            "label_id": "127",
            "label_title": "AV",
            "label_likeNum": "0"
          },
          {
            "label_id": "128",
            "label_title": "Russia",
            "label_likeNum": "0"
          },
          {
            "label_id": "129",
            "label_title": "Sexy",
            "label_likeNum": "0"
          }
        ]
      },
      {
        "content_id": "173",
        "content_title": "",
        "content_imgsrc": "http://oq8t6idvw.bkt.clouddn.com/b065883925a84a15aa2d7efbeb4fdf2e.jpg",
        "content_smallImgSrc": "",
        "img_width": "4000",
        "img_height": "2667",
        "imageAve": "0x70645b",
        "labels": [
          {
            "label_id": "126",
            "label_title": "Angelica",
            "label_likeNum": "0"
          },
          {
            "label_id": "127",
            "label_title": "AV",
            "label_likeNum": "0"
          },
          {
            "label_id": "128",
            "label_title": "Russia",
            "label_likeNum": "0"
          },
          {
            "label_id": "129",
            "label_title": "Sexy",
            "label_likeNum": "0"
          }
        ]
      }
    ],
    "pageModel": {
      "page": "1",
      "pagesize": "2"
    }
  },
  "msg": ""
}

结果

struct ItemModel {
    let msg: String
    let ret: Int
    struct Result {
        let msg: String
        let code: URL
        struct Item {
            let contentSmallImgSrc: String
            struct Label {
                let labelId: URL
                let labelTitle: URL
                let labelLikeNum: URL
                init(labelId: URL, labelTitle: URL, labelLikeNum: URL) {
                    self.labelId = labelId
                    self.labelTitle = labelTitle
                    self.labelLikeNum = labelLikeNum
                }
                init?(json: [String: Any]) {
                    guard let labelIdString = json["label_id"] as? String else { return nil }
                    guard let labelId = URL(string: labelIdString) else { return nil }
                    guard let labelTitleString = json["label_title"] as? String else { return nil }
                    guard let labelTitle = URL(string: labelTitleString) else { return nil }
                    guard let labelLikeNumString = json["label_likeNum"] as? String else { return nil }
                    guard let labelLikeNum = URL(string: labelLikeNumString) else { return nil }
                    self.init(labelId: labelId, labelTitle: labelTitle, labelLikeNum: labelLikeNum)
                }
            }
            let labels: [Label]
            let imgWidth: URL
            let imgHeight: URL
            let imageAve: URL
            let contentTitle: String
            let contentId: URL
            let contentImgsrc: URL
            init(contentSmallImgSrc: String, labels: [Label], imgWidth: URL, imgHeight: URL, imageAve: URL, contentTitle: String, contentId: URL, contentImgsrc: URL) {
                self.contentSmallImgSrc = contentSmallImgSrc
                self.labels = labels
                self.imgWidth = imgWidth
                self.imgHeight = imgHeight
                self.imageAve = imageAve
                self.contentTitle = contentTitle
                self.contentId = contentId
                self.contentImgsrc = contentImgsrc
            }
            init?(json: [String: Any]) {
                guard let contentSmallImgSrc = json["content_smallImgSrc"] as? String else { return nil }
                guard let labelsJSONArray = json["labels"] as? [[String: Any]] else { return nil }
                let labels = labelsJSONArray.map({ Label(json: $0) }).flatMap({ $0 })
                guard let imgWidthString = json["img_width"] as? String else { return nil }
                guard let imgWidth = URL(string: imgWidthString) else { return nil }
                guard let imgHeightString = json["img_height"] as? String else { return nil }
                guard let imgHeight = URL(string: imgHeightString) else { return nil }
                guard let imageAveString = json["imageAve"] as? String else { return nil }
                guard let imageAve = URL(string: imageAveString) else { return nil }
                guard let contentTitle = json["content_title"] as? String else { return nil }
                guard let contentIdString = json["content_id"] as? String else { return nil }
                guard let contentId = URL(string: contentIdString) else { return nil }
                guard let contentImgsrcString = json["content_imgsrc"] as? String else { return nil }
                guard let contentImgsrc = URL(string: contentImgsrcString) else { return nil }
                self.init(contentSmallImgSrc: contentSmallImgSrc, labels: labels, imgWidth: imgWidth, imgHeight: imgHeight, imageAve: imageAve, contentTitle: contentTitle, contentId: contentId, contentImgsrc: contentImgsrc)
            }
        }
        let items: [Item]
        struct PageModel {
            let pagesize: URL
            let page: URL
            init(pagesize: URL, page: URL) {
                self.pagesize = pagesize
                self.page = page
            }
            init?(json: [String: Any]) {
                guard let pagesizeString = json["pagesize"] as? String else { return nil }
                guard let pagesize = URL(string: pagesizeString) else { return nil }
                guard let pageString = json["page"] as? String else { return nil }
                guard let page = URL(string: pageString) else { return nil }
                self.init(pagesize: pagesize, page: page)
            }
        }
        let pageModel: PageModel
        init(msg: String, code: URL, items: [Item], pageModel: PageModel) {
            self.msg = msg
            self.code = code
            self.items = items
            self.pageModel = pageModel
        }
        init?(json: [String: Any]) {
            guard let msg = json["msg"] as? String else { return nil }
            guard let codeString = json["code"] as? String else { return nil }
            guard let code = URL(string: codeString) else { return nil }
            guard let itemsJSONArray = json["info"] as? [[String: Any]] else { return nil }
            let items = itemsJSONArray.map({ Item(json: $0) }).flatMap({ $0 })
            guard let pageModelJSONDictionary = json["pageModel"] as? [String: Any] else { return nil }
            guard let pageModel = PageModel(json: pageModelJSONDictionary) else { return nil }
            self.init(msg: msg, code: code, items: items, pageModel: pageModel)
        }
    }
    let result: Result
    init(msg: String, ret: Int, result: Result) {
        self.msg = msg
        self.ret = ret
        self.result = result
    }
    init?(json: [String: Any]) {
        guard let msg = json["msg"] as? String else { return nil }
        guard let ret = json["ret"] as? Int else { return nil }
        guard let resultJSONDictionary = json["result"] as? [String: Any] else { return nil }
        guard let result = Result(json: resultJSONDictionary) else { return nil }
        self.init(msg: msg, ret: ret, result: result)
    }
}
nixzhu commented 7 years ago

上次的修复引发的问题,已修复,请再试试 https://shared-baby.herokuapp.com

cszwdy commented 7 years ago

好快! 没有问题了 : ]