Xcode插件LCJSONFormatter:JSON 轉(zhuǎn)模型 swift版

LCJSONFormatter

這是一個(gè)純粹的swift版本的JSON轉(zhuǎn)模型工具。就像MJExtension一樣,在swift中,我們用Argo.

樣例JSON1,以字典開(kāi)始:

{
    "id": 1, 
    "name": "JohnSnow", 
    "dic": [
        {
            "key": "hello world", 
            "value": "where are you"
        }, 
        {
            "key": "hello world", 
            "value": "where are you"
        }
    ], 
    "commentsInfo": {
        "id": 100, 
        "value": "good man"
    }, 
    "price": 4.39, 
    "woman": false, 
    "optionalString": null, 
    "optionalArray": [ ]
}

樣例JSON2,以數(shù)組開(kāi)始:

[
    {
        "id": 1, 
        "name": "JohnSnow", 
        "dic": [
            {
                "key": "hello world", 
                "value": "where are you"
            }, 
            {
                "key": "hello world", 
                "value": "where are you"
            }
        ]
    }, 
    {
        "id": 1, 
        "name": "JohnSnow", 
        "dic": [
            {
                "key": "hello world", 
                "value": "where are you"
            }, 
            {
                "key": "hello world", 
                "value": "where are you"
            }
        ]
    }
]

使用

Alcatraz中搜索LCJSONFormatter,重啟Xcode.

在swift文件中,呼出LCJSONFormatter窗口。將上面的JSON粘貼進(jìn)去。一路回車(chē)就行了。

最后,轉(zhuǎn)換后的文件如下:

//對(duì)于樣例1,以字典開(kāi)始
import UIKit

import Argo
import Curry

struct LCJSONFormatterDemo: Decodable { 
 
    let optionalString: String?
    let id: Int
    let price: Double
    let optionalArray: [String]?
    let dic: [Dic]
    let commentsInfo: Commentsinfo
    let woman: Bool
    let name: String 

    static func decode(json: JSON) -> Decoded<LCJSONFormatterDemo> {
          return curry(self.init)
        <^> json <|? "optionalString"
        <*> json <| "id"
        <*> json <| "price"
        <*> json <||? ["optionalArray"]
        <*> json <|| ["dic"]
        <*> json <| "commentsInfo"
        <*> json <| "woman"
        <*> json <| "name"
     }
}
 

struct Dic: Decodable { 
 
    let key: String
    let value: String 

    static func decode(json: JSON) -> Decoded<Dic> {
          return curry(self.init)
        <^> json <| "key"
        <*> json <| "value"
     }
}
 

struct Commentsinfo: Decodable { 
 
    let id: Int
    let value: String 

    static func decode(json: JSON) -> Decoded<Commentsinfo> {
          return curry(self.init)
        <^> json <| "id"
        <*> json <| "value"
     }
}

//對(duì)于樣例2,以字典開(kāi)始
import UIKit

import Argo
import Curry

struct LCJSONFormatterDemo: Decodable { 
 
    let lcArray: [Lcarray] 

    static func decode(json: JSON) -> Decoded<LCJSONFormatterDemo> {
          return curry(self.init)
        <^> json <|| ["lcArray"]
     }
}
 

struct Lcarray: Decodable { 
 
    let id: Int
    let name: String
    let dic: [Dic] 

    static func decode(json: JSON) -> Decoded<Lcarray> {
          return curry(self.init)
        <^> json <| "id"
        <*> json <| "name"
        <*> json <|| ["dic"]
     }
}
 

struct Dic: Decodable { 
 
    let key: String
    let value: String 

    static func decode(json: JSON) -> Decoded<Dic> {
          return curry(self.init)
        <^> json <| "key"
        <*> json <| "value"
     }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • WebSocket-Swift Starscream的使用 WebSocket 是 HTML5 一種新的協(xié)議。它實(shí)...
    香橙柚子閱讀 24,779評(píng)論 8 183
  • 轉(zhuǎn)載自:https://github.com/Tim9Liu9/TimLiu-iOS 目錄 UI下拉刷新模糊效果A...
    袁俊亮技術(shù)博客閱讀 12,147評(píng)論 9 105
  • 什么是感冒? 感冒,總體上分為普通感冒和流行性感冒。 普通感冒,是由多種病毒引起的一種呼吸道常見(jiàn)病,其中30%-5...
    陳嬌育兒閱讀 609評(píng)論 0 0
  • (二)想象與概念 一、想象 真的需要充分發(fā)揮利用,特點(diǎn)是新穎、形象,用于處理圖像 1、作用:1)預(yù)見(jiàn)未來(lái),2)補(bǔ)充...
    沅芷醴蘭閱讀 264評(píng)論 0 0
  • 放不放炮? 這問(wèn)題源于我買(mǎi)了車(chē)。 不放的話(huà),也許有人問(wèn):有沒(méi)有親戚?有沒(méi)有朋友?有沒(méi)有同事? 回答,都有呢。 那么...
    一老碗面閱讀 450評(píng)論 0 1

友情鏈接更多精彩內(nèi)容