https://github.com/swiftdo/json
码了个 swift json 解析器
https://github.com/swiftdo/json
json swift
Last synced: 8 months ago
JSON representation
码了个 swift json 解析器
- Host: GitHub
- URL: https://github.com/swiftdo/json
- Owner: swiftdo
- Created: 2021-04-09T02:06:15.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2021-04-21T00:36:56.000Z (about 5 years ago)
- Last Synced: 2025-03-25T02:37:06.165Z (over 1 year ago)
- Topics: json, swift
- Language: Swift
- Homepage:
- Size: 98.6 KB
- Stars: 7
- Watchers: 1
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# swiftdo/json
swift json 解析库
* [x] 完成 json 打印
* [x] 完成 json 解析
## 实现文章
* [Swift 码了个 JSON 解析器(一)](https://oldbird.run/swift/fp/t3-json1.html)
* [Swift 码了个 JSON 解析器(二)](https://oldbird.run/swift/fp/t3-json2.html)
* [Swift 码了个 JSON 解析器(三)](https://oldbird.run/swift/fp/t3-json3.html)
## 使用
```swift
let str = "{ \"a\":[8,9,10],\"c\":{\"temp\":true,\"say\":\"hello\",\"name\":\"world\"}, \"b\":10.2}"
print("json 字符串::\n\(str) \n")
do {
// 解析 json 字符串
let result = try parseJson(str: str)
print("\n返回结果::")
// 格式化 json 字符串
print(prettyJson(json: result))
} catch {
print(error)
}
```
结果输出:
```json
json 字符串::
{ "a":[8,9,10],"c":{"temp":true,"say":"hello","name":"world"}, "b":10.2}
返回结果::
{
"a":[
8,
9,
10
],
"c":{
"temp":true,
"say":"hello",
"name":"world"
},
"b":10.2
}
```
## 参考:
* [150行Haskell代码实现JSON的解析和格式化,诗一样的代码](https://zhuanlan.zhihu.com/p/359406047)
* [半小时实现一个 JSON 解析器](https://zhuanlan.zhihu.com/p/28049617)
* [自己动手实现一个简单的JSON解析器](https://segmentfault.com/a/1190000010998941)