本项目是 DirtyJSON Node.js Package 的 Swift 实现。
点这里查看演示:Demo App
DirtyJSONSwift 是一个 Swift 库,旨在自动修复无效的 JSON 数据。
Douglas Crockford 创建并推广 JSON 格式的初衷是提供一种严格、用户友好且标准化的数据格式,其中不允许语法错误。程序生成的 JSON 输出也应该是完美的(因为它理想情况下不应该是手写的)。然而,随着 ChatGPT 等人工智能的兴起,已经观察到不正确的 JSON 输出的实例。在我现有的数据中,在 80,000 次对话中,大约有 4,000 次输出了不正确的 JSON 格式。DirtyJSONSwift 试图纠正 AI 生成的 JSON 中的一些错误。
DirtyJSONSwift 提供以下自动修复功能:
//
和 /* */
注释;,
;{}
, []
的误用和不匹配;true
、false
和 null
除外);["quotes in "quotes" in quotes"]
转换为 ["quotes in \"quotes\" in quotes"]
;true
、false
、null
统一转换为小写;]
或 }
自动补全未完成的 JSON。不再需要担心 AI 输出格式错误的 JSON。你甚至可以随意手写 JSON 了。
您可以使用 Swift Package Manager 将 DirtyJSONSwift 集成到您的 Xcode 项目中:
DirtyJSONSwift
不需要对象键被引号包裹,并且可以处理单引号包裹的值字符串。
import DirtyJSON
print(DirtyJSON.fix("{ test: 'this is a test', 'number': 1.23e10 }"))
// output: {"test":"this is a test","number":1.23e10}
DirtyJSONSwift
可以处理字符串中嵌入的引号。
import DirtyJSON
print(DirtyJSON.fix("{\"test\": \"some text \"a quote\" more text\"}"))
// output: {"test":"some text \"a quote\" more text"}
DirtyJSONSwift
可以处理字符串内的换行符。
import DirtyJSON
print(DirtyJSON.fix("{\"test\": \"each \n on \n new \n line\"}"))
// output: {"test":"each \n on \n new \n line"}
DirtyJSONSwift
可以处理 //
和 /* */
注释。
import DirtyJSON
let jsonDataWithComments = """
{
// This is a comment
"name": "John",
/*
This is a multiline
comment
*/
"age": 30
}
""";
let fixedData = DirtyJSON.fix(jsonDataWithComments)
print(fixedData)
// output: {"name":"John","age":30}
DirtyJSONSwift
可以处理末尾的逗号 ,
。
import DirtyJSON
let jsonDataWithTrailingCommas = """
{
"name": "John",
"age": 30, // Notice this trailing comma
}
""";
let fixedData = DirtyJSON.fix(jsonDataWithTrailingCommas)
print(fixedData)
// output: {"name":"John","age":30}
DirtyJSONSwift
可以处理 {}
, []
的误用和不匹配。
import DirtyJSON
let jsonDataWithMismatch = """
{
"name": "John",
"age": 30,
"friends": [
"Alice",
"Bob",
} // this '}' should be ']'
】// this abnormal square bracket should be '}'
""";
let fixedData = DirtyJSON.fix(jsonDataWithMismatch)
print(fixedData)
// output: {"name":"John","age":30,"friends":["Alice","Bob"]}
DirtyJSONSwift
可以处理未完成的 JSON。
import DirtyJSON
let unfinishedJsonData = """
{
"name": "John",
"age": 30,
"friends": [
"Alice",
"Bob",
"""
let fixedData = DirtyJSON.fix(unfinishedJsonData)
print(fixedData)
// output: {"name":"John","age":30,"friends":["Alice","Bob"]}
DirtyJSONSwift
可以处理不正确书写的符号。
import DirtyJSON
let improperlyWrittenJSON = "},{「a」:1,,b:[2,,“3”:},]},";
let fixedData = DirtyJSON.fix(improperlyWrittenJSON)
print(fixedData)
// output: {"a":1,"b":[2,"3"]}
MIT 许可证
Simplified Chinese introduction
本项目是 DirtyJSON Node.js Package 的 Swift 实现。
点这里查看演示:Demo App
DirtyJSONSwift 可以为你解析非法的 JSON 数据。
Douglas Crockford 创建和推广 JSON 格式的初衷是提供一种严格、易用、统一的数据格式,语法错误等是不被允许的。通过程序生成的 JSON 完美无暇(这本来就不应该是手写的格式),直到人工智能 ChatGPT 出现,让它输出 JSON 的时候,在我已有的数据看来,8 万次对话有 4000 次输出了错误格式的 JSON,DirtyJSONSwift 试图修复一些 AI 生成 JSON 的错误。
DirtyJSONSwift 提供以下自动修复功能:
//
和 /* */
注释;,
;{}
, []
的乱写和不匹配问题;true
, false
, null
);["quotes in "quotes" in quotes"]
转为 ["quotes in \"quotes\" in quotes"]
;true
、false
、null
统一转换为小写;]
或 }
自动补全未完成的 JSON 结构;再也不怕 AI 输出的 JSON 乱写一气了。你也可以用手写的方式乱写 JSON 了。
您可以使用 Swift Package Manager 将 DirtyJSONSwift 集成到您的 Xcode 项目中:
不写引号或写为单引号。
import DirtyJSON
print(DirtyJSON.fix("{ test: 'this is a test', 'number': 1.23e10 }"))
// 输出: {"test":"this is a test","number":1.23e10}
处理字符串中嵌入的引号。
import DirtyJSON
print(DirtyJSON.fix("{\"test\": \"some text \"a quote\" more text\"}"))
// 输出: {"test":"some text \"a quote\" more text"}
转义字符串内的换行符。
import DirtyJSON
print(DirtyJSON.fix("{\"test\": \"each \n on \n new \n line\"}"))
// 输出: {"test":"each \n on \n new \n line"}
在 JSON 内部随意书写单行 //
和多行 /* */
注释。
import DirtyJSON
let jsonDataWithComments = """
{
// 这个是单行注释
"name": "小明",
/*
这个是多行注释
这个是多行注释
*/
"age": 30
}
""";
let fixedData = DirtyJSON.fix(jsonDataWithComments)
print(fixedData)
// 输出: {"name":"小明","age":30}
自动删除最后一个逗号 ,
。
import DirtyJSON
let jsonDataWithTrailingCommas = """
{
"name": "小明",
"age": 30, // 注意这个逗号将被删除
}
""";
let fixedData = DirtyJSON.fix(jsonDataWithTrailingCommas)
print(fixedData)
// 输出: {"name":"小明","age":30}
DirtyJSONSwift
可以处理不匹配的 {}
, []
。
import DirtyJSON
let jsonDataWithMismatch = """
{
"name": "小明",
"age": 30,
"friends": [
"小红",
"小刚",
} // 这里的 '}' 应该是 ']'
】// 这里的 '】' 应该是 '}'
""";
let fixedData = DirtyJSON.fix(jsonDataWithMismatch)
print(fixedData)
// 输出: {"name":"小明","age":30,"friends":["小红","小刚"]}
DirtyJSONSwift
可以处理未完成的 JSON。
import DirtyJSON
let unfinishedJsonData = """
{
"name": "小明",
"age": 30,
"friends": [
"小红",
"小刚",
""";
let fixedData = DirtyJSON.fix(unfinishedJsonData)
print(fixedData)
// 输出: {"name":"小明","age":30,"friends":["小红","小刚"]}
让事情更混乱一些。
import DirtyJSON
let improperlyWrittenJSON = "},{「a」:1,,b:[2,,“3”:},]},";
let fixedData = DirtyJSON.fix(improperlyWrittenJSON)
print(fixedData)
// 输出: {"a":1,"b":[2,"3"]}
MIT 许可证