SexyJson:超性感 JSON 解析开源库(Swift)

栏目: Swift · 发布时间: 6年前

内容简介:SexyJson:超性感 JSON 解析开源库(Swift)

SexyJson

SexyJson is Swift3.+ json parse open source library quickly and easily, perfect supporting class and struct model, support the KVC model, fully oriented protocol architecture, support iOS and MAC OS X

Objective-c version :point_right: WHC_Model

Note

  • The definition of model must implement SexyJson protocol
  • If you have any enumeration type must be specified in the definition of model data type and implementation SexyJsonEnumType protocol
  • To KVC model must inherit NSObject and NSCoding protocol implementation

Require

  • iOS 8.0+ / Mac OS X 10.11+ / tvOS 9.0+
  • Xcode 8.0 or later
  • Swift 3.0+

Install

  • CocoaPods: pod 'SexyJson'

Model

This is an example of json:

let json = "{\"age\":25,\"enmuStr\":\"Work\",\"url\":\"https:\\/\\/www.baidu.com\",
            \"subArray\":[{\"test3\":\"test3\",\"test2\":\"test2\",\"cls\":{\"age\":10,
            \"name\":\"swift\"},\"test1\":\"test1\"},{\"test3\":\"test3\",\"test2\":\"test2\",
            \"cls\":{\"age\":10,\"name\":\"swift\"},\"test1\":\"test1\"}],\"color\":\"0xffbbaa\",
            \"nestArray\":[[{\"test\":\"test1\"},{\"test\":\"test2\"}],[{\"test\":\"test3\"},
            {\"test\":\"test4\"}]],\"enmuInt\":10,\"sub\":{\"test1\":\"test1\",\"test2\":\"test2\",
            \"test3\":\"test3\"},\"height\":175,\"intArray\":[1,2,3,4],\"name\":\"吴海超\",
            \"learn\":[\"iOS\",\"android\",\"js\",\"nodejs\",\"python\"]}"

The model class:

enum WorkEnum: String,SexyJsonEnumType {
    case null = "nil"
    case one = "Work"
    case two = "Not Work"
}

enum IntEnum: Int,SexyJsonEnumType {
    case zero = 0
    case hao = 10
    case xxx = 20
}

struct Model :SexyJson {

    var age: Int = 0
    var enmuStr: WorkEnum!
    var url: URL!
    var subArray: [SubArray]!
    var color: UIColor!
    var nestArray: [[NestArray]]?
    var enmuInt: IntEnum = .xxx
    var sub: Sub!
    var height: Int = 0
    var intArray: [Int]!
    var name: String!
    var learn: [String]!

    /// Model mapping
    public mutating func sexyMap(_ map: [String : Any]) {
        age      <<<   map["age"]
        enmuStr  <<<   map["enmuStr"]
        url      <<<   map["url"]
        subArray <<<   map["subArray"]
        color    <<<   map["color"]
        nestArray <<<  map["nestArray"]
        enmuInt   <<<  map["enmuInt"]
        sub       <<<  map["sub"]
        height    <<<  map["height"]
        intArray  <<<  map["intArray"]
        name      <<<  map["name"]
        learn     <<<  map["learn"]
    }
}

You don't need to manually create the SexyJson model class you can use open source tools with the help of WHC_DataModel.app automatically created SexyJson model

Usage

Json is converted into a model object(json -> model)

let model = Model.sexy_json(json)

Model object converted into the dictionary(model -> dictionary)

let dictionary = model.sexy_dictionary()

Model object converted into the json string(model -> json)

let jsonStr = model.sexy_json()

SexyJson support json parse the key path

let subArrayModel = SubArray.sexy_json(json, keyPath: "subArray[0]")
let subNestArray = NestArray.sexy_json(json, keyPath: "nestArray[0][0]")
let test = String.sexy_json(json, keyPath: "nestArray[0][0].test")

Json is converted into a model array object(json -> [model])

let arrayModel = [Model].sexy_json(json)

Model object array converted into the array([model] -> array)

let array = arrayModel.sexy_array()

Model object array converted into the json string([model] -> json)

let arrayJson = arrayModel.sexy_json()

SexyJson support model kvc

let sub = Sub.sexy_json(json, keyPath: "sub")
let subData = NSKeyedArchiver.archivedData(withRootObject: sub!)
let subCoding = NSKeyedUnarchiver.unarchiveObject(with: subData) as? Sub
let subPy = subCoding?.copy() as? Sub

Prompt

If you want to view the analytical results, please download this demo to check the specific usage

Licenses

All source code is licensed under the MIT License.


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

算法导论(原书第3版)

算法导论(原书第3版)

Thomas H.Cormen、Charles E.Leiserson、Ronald L.Rivest、Clifford Stein / 殷建平、徐云、王刚、刘晓光、苏明、邹恒明、王宏志 / 机械工业出版社 / 2012-12 / 128.00元

在有关算法的书中,有一些叙述非常严谨,但不够全面;另一些涉及了大量的题材,但又缺乏严谨性。本书将严谨性和全面性融为一体,深入讨论各类算法,并着力使这些算法的设计和分析能为各个层次的读者接受。全书各章自成体系,可以作为独立的学习单元;算法以英语和伪代码的形式描述,具备初步程序设计经验的人就能看懂;说明和解释力求浅显易懂,不失深度和数学严谨性。 全书选材经典、内容丰富、结构合理、逻辑清晰,对本科......一起来看看 《算法导论(原书第3版)》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具