psvmc/RxSwift-SwiftyJSON


这是一个把 Alamofire 请求的数据 转成 SwiftyJSON 中的 JSON 或者转化成对象的组件 http://www.psvmc.cn

Download


Step 1. Add the JitPack repository to your build file

Add it in your root settings.gradle at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url 'https://jitpack.io' }
		}
	}

Add it in your settings.gradle.kts at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url = uri("https://jitpack.io") }
		}
	}

Add to pom.xml

	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>

Add it in your build.sbt at the end of resolvers:

 
    resolvers += "jitpack" at "https://jitpack.io"
        
    

Add it in your project.clj at the end of repositories:

 
    :repositories [["jitpack" "https://jitpack.io"]]
        
    

Step 2. Add the dependency

	dependencies {
		implementation 'com.github.psvmc:RxSwift-SwiftyJSON:'
	}
	dependencies {
		implementation("com.github.psvmc:RxSwift-SwiftyJSON:")
	}
	<dependency>
	    <groupId>com.github.psvmc</groupId>
	    <artifactId>RxSwift-SwiftyJSON</artifactId>
	    <version></version>
	</dependency>

                            
    libraryDependencies += "com.github.psvmc" % "RxSwift-SwiftyJSON" % ""
        
        

                            
    :dependencies [[com.github.psvmc/RxSwift-SwiftyJSON ""]]
        
        

Readme


RxSwift-SwiftyJSON

这是一个把 Alamofire 请求的数据 转成 SwiftyJSON 中的 JSON 或者转化成对象

安装方法(Installation)

你可以通过CocoaPods 来使用RxSwift-SwiftyJSON 在你的Podfile中添加如下配置

RxSwift-SwiftyJSON is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'RxSwift-SwiftyJSON', '~> 1.1'

然后运行pod install

使用方法(Usage)

创建一个实现 ZJSwiftyJSONAble 协议的Class 或者 Struct

Create a Class or Struct which implements the ZJSwiftyJSONAble protocol.

如果你返回数据的格式为:

if you get the json like this:

{
    "success":"true",
    "msg":"获取用户信息成功",
    "obj":[]
}

你可以建立以下两个对象:

you can create two model like this:

import Foundation
import SwiftyJSON

class ZJResult_S<T: ZJSwiftyJSONAble>: ZJSwiftyJSONAble {
    var success: String!
    var msg: String!
    var obj: [T]?
    
    
    required init?(jsonData:JSON){
        self.success = jsonData["success"].stringValue
        self.msg = jsonData["msg"].stringValue
        self.obj = jsonData["obj"].arrayValue.flatMap { T(jsonData: $0) }
    }
}
import Foundation
import SwiftyJSON

class ZJArticle_S: ZJSwiftyJSONAble {
    var title: String!
    var keywords: String!
    var description: String!
    var date: String!
    var path: String!
    var url: String!
    
    
    required init?(jsonData:JSON){
        self.title = jsonData["title"].stringValue
        self.keywords = jsonData["keywords"].stringValue
        self.description = jsonData["description"].stringValue
        self.date = jsonData["date"].stringValue
        self.path = jsonData["path"].stringValue
        self.url = jsonData["url"].stringValue
    }
}

添加pod库

add pod

pod 'RxAlamofire'

然后我们就可以这样请求数据了

then we can query data like this:

_ = string(.POST, "http://t.yidaisong.com:90/login!in.do",
    parameters: ["userPhone":"15225178360","userLoginPswd":"123456"])
    .observeOn(MainScheduler.instance)
    .mapSwiftyObject(ZJResult_S<ZJUser_S>)
    .subscribe(
        onNext: { repos -> Void in
            self.showTextView.text = "用SwiftyJSON把结果转为对象\n"
                                   + "用户名:\(repos.obj!.userName)\n"
                                   + "昵称:\(repos.obj!.userAlias)";
        },
        onError: { (error) -> Void in
            self.showTextView.text = "\(error)";
    })

是不是很简单

so easy

如果你不想转为对象 想直接用 SwiftyJSON 对应的JSON格式 你可以这样写

if you dont want to change it to model,just to JSON,you can write like this

_ = string(.POST, "http://t.yidaisong.com:90/login!in.do",
    parameters: ["userPhone":"15225178360","userLoginPswd":"123456"])
    .observeOn(MainScheduler.instance)
    .mapSwiftyJSON()
    .subscribe(
        onNext: { repos -> Void in
            self.showTextView.text = "用SwiftyJSON把结果转为JSON\n"
                                   + "用户名:\(repos["obj"]["userName"].stringValue)\n"
                                   + "昵称:\(repos["obj"]["userAlias"].stringValue)";
        },
        onError: { (error) -> Void in
            self.showTextView.text = "\(error)";
    })

作者(Author)

剑行者

版权(License)

你可以在MIT许可下使用RxSwift-SwiftyJSON,更多信息请查看LICENSE文件

RxSwift-SwiftyJSON is available under the MIT license. See the LICENSE file for more info.