我正在 Swift 4.2
中编写一个 iOS应用服务器使用无效字符响应质量差的json。我必须替换一些字符以使其成为有效的json字符串,但我无法。
服务器响应字符串:
{\“ fieldsets \”:[{\“ properties \”:[{\“ alias \”:\“ categories \”,\“ value \”:\“ Equity \”},{\“ alias \ “:\” subCategory \“,\” value \“:\” [\ r \ n \\“ Banking \\”,\ r \ n \\“ FMCG \\”,\ r \ n \\“ Infrastructure \ \“,\ r \ n \\”国际基金\\“,\ r \ n \\”大盘股“,\ r \ n \\”大中盘股“,\ r \ n \\ “ Mid Cap \\”,\ r \ n \\“ Balanced Fund \\”,\ r \ n \\“ Multi Cap \\”,\ r \ n \\“ Others \\”,\ r \ n \ \“ Pharma \\”,\ r \ n \\“税收计划\\”,\ r \ n \\“每日\”,\ r \ n \\“小帽子”,\ r \ n \ \“ Mid&Small Cap \\”,\ r \ n \\“ Diversified \\” \ r \ n] \“},{\” alias \“:\” icon \“,\” value \“:\ “ /media/1138/equity.png \”}“],\”别名\“:\” categooriesSubCategories \“,\” disabled \“:false,\” id \“:\” 170e1ba0-6f05-4a11-a2a6- 04e108e7fa1f \“,\” releaseDate \“:null,\” expireDate \“:null,\” allowedMemberGroups \“:\” \“},{\” properties \“:[{\” alias \“:\” categories \“,\” value \“:\”平衡\“},{\”别名\“:\” subCategory \“,\” value \“:\” [\ r \ n \\“债务导向型激进型\ \“,\ r \ n \\”债务导向的保守派\\“,\ r \ n \\”信贷机会\\“,\ r \ n \\”权益导向的\\“,\ r \ n \\ “资产Alloc ation \\“,\ r \ n \\”其他\\“,\ r \ n \\”每年\\“,\ r \ n \\”高级\\“ \ r \ n] \”},{ \“ alias \”:\“ icon \”,\“ value \”:\“ / media / 1139 / balanced.png \”}],\“ alias \”:\“ categorysSubCategories \”,\“ disabled \” :false,\“ id \”:\“ aabf640c-1ba4-4136-9dd0-79f3dfd6b79b \”,\“ releaseDate \”:null,\“ expireDate \”:null,\“ allowedMemberGroups \”:\\“ \”} ,{\“ properties \”:[{\“ alias \”:\“ categories \”,\“ value \”:\“ Tax Saver \”},{\“ alias \”:\“ subCategory \”,\ “ value \”:\“ [\ r \ n \\” Any \\“ \ r \ n] \”},{\“ alias \”:\“ icon \”,\“ value \”:\“ / media / 1140 / tax-saver.png \“}],\” alias \“:\” categooriesSubCategories \“,\” disabled \“:false,\” id \“:\” fac9c316-16dc-48ab-bc0d- e8c22c02378f \“,\” releaseDate \“:null,\” expireDate \“:null,\” allowedMemberGroups \“:\” \“},{\” properties \“:[{\” alias \“:\” categories \“,\” value \“:\”债务\“},{\”别名\“:\” subCategory \“,\” value \“:\” [\ r \ n \\“液体\\”, \ r \ n \\“ FMP \\”,\ r \ n \\“ Gilt中长期债券”,\ r \ n \\“ Gilt短期债券\”,\ r \ n \\“收入\\“,\ r \ n \\”其他\\“,\ r \ n \\”短期\\“,\ r \ n \\”超短期\\“,\ r \ n \\”中上限\“,\ r \ n \\“小型股\\”,\ r \ n \\“浮标\\”,\ r \ n \\“金银基金\”,\ r \ n \\“动态债券\\” \ r \ n ] \“},{\” alias \“:\” icon \“,\” value \“:\” / media / 1141 / debt.png \“}],\” alias \“:\” categoriesSubCategories \“ ,\“ disabled \”:false,\“ id \”:\“ ef1efeda-3334-4a9b-bda7-afff76474a40 \”,\“ releaseDate \”:null,\“ expireDate \”:null,\“ allowedMemberGroups \” :\“ \”}]}
我正在尝试使用:
let convertedStr = jsonStr.replacingOccurrences(of: "\\r\\n", with: "", options: .literal, range: nil)
let convertedStr2 = convertedStr.replacingOccurrences(of: "\\", with: "", options: .literal, range: nil)
let convertedStr3 = convertedStr2.replacingOccurrences(of: "\"[", with: "", options: .literal, range: nil)
let convertedStr4 = convertedStr3.replacingOccurrences(of: "]\"", with: "", options: .literal, range: nil)
没有解决问题。
答案 0 :(得分:1)
您的convertedStr3
和convertedStr4
应该看起来像这样:
let convertedStr3 = convertedStr2.replacingOccurrences(of: "\"[", with: "[", options: .literal, range: nil)
let convertedStr4 = convertedStr3.replacingOccurrences(of: "]\"", with: "]", options: .literal, range: nil)
您的代码无法完全正常工作,因为您正在删除[
和]
之类的代表json中数组的字符。要解决此问题,请注意,我同时添加了两个with: "[",
和with: "]",
,这使得替换成为可能:
"[
和[
在convertStr3中]"
和]
在convertStr4中尝试print(convertedStr4)
,您应该可以复制结果并jsonlint.com对其进行检查,以验证json的格式正确。
答案 1 :(得分:0)
该问题是由于\r\n
引起的,它应该是“ \ r \ n”或什么也不是,因为它们位于JSON(JSON中的JSON字符串化)之内。
所以这两个:
let convertedStr = jsonStr.replacingOccurrences(of: "\r\n", with: "")
或
let convertedStr = jsonStr.replacingOccurrences(of: "\r\n", with: "\\r\\n")
应该足够了。
无需其他修改。我建议将其修复在后端部分。
后端应向您发送此消息(有效):
{\"fieldsets\":[{\"alias\":\"categoriesSubCategories\",\"allowedMemberGroups\":\"\",\"id\":\"170e1ba0-6f05-4a11-a2a6-04e108e7fa1f\",\"expireDate\":null,\"properties\":[{\"alias\":\"categories\",\"value\":\"Equity\"},{\"alias\":\"subCategory\",\"value\":\"[\\r\\n \\\"Banking\\\",\\r\\n \\\"FMCG\\\",\\r\\n \\\"Infrastructure\\\",\\r\\n \\\"International Funds\\\",\\r\\n \\\"Large Cap\\\",\\r\\n \\\"Large & Mid Cap\\\",\\r\\n \\\"Mid Cap\\\",\\r\\n \\\"Balanced Fund\\\",\\r\\n \\\"Multi Cap\\\",\\r\\n \\\"Others\\\",\\r\\n \\\"Pharma\\\",\\r\\n \\\"Tax Planning\\\",\\r\\n \\\"Daily\\\",\\r\\n \\\"Small Cap\\\",\\r\\n \\\"Mid & Small Cap\\\",\\r\\n \\\"Diversified\\\"\\r\\n]\"},{\"alias\":\"icon\",\"value\":\"\\/media\\/1138\\/equity.png\"}],\"releaseDate\":null,\"disabled\":false},{\"alias\":\"categoriesSubCategories\",\"allowedMemberGroups\":\"\",\"id\":\"aabf640c-1ba4-4136-9dd0-79f3dfd6b79b\",\"expireDate\":null,\"properties\":[{\"alias\":\"categories\",\"value\":\"Balanced\"},{\"alias\":\"subCategory\",\"value\":\"[\\r\\n \\\"Debt-Oriented Aggressive\\\",\\r\\n \\\"Debt-Oriented Conservative\\\",\\r\\n \\\"credit opportunities\\\",\\r\\n \\\"Equity Oriented\\\",\\r\\n \\\"Asset Allocation\\\",\\r\\n \\\"Others\\\",\\r\\n \\\"Yearly\\\",\\r\\n \\\"Advance\\\"\\r\\n]\"},{\"alias\":\"icon\",\"value\":\"\\/media\\/1139\\/balanced.png\"}],\"releaseDate\":null,\"disabled\":false},{\"alias\":\"categoriesSubCategories\",\"allowedMemberGroups\":\"\",\"id\":\"fac9c316-16dc-48ab-bc0d-e8c22c02378f\",\"expireDate\":null,\"properties\":[{\"alias\":\"categories\",\"value\":\"Tax Saver\"},{\"alias\":\"subCategory\",\"value\":\"[\\r\\n \\\"Any\\\"\\r\\n]\"},{\"alias\":\"icon\",\"value\":\"\\/media\\/1140\\/tax-saver.png\"}],\"releaseDate\":null,\"disabled\":false},{\"alias\":\"categoriesSubCategories\",\"allowedMemberGroups\":\"\",\"id\":\"ef1efeda-3334-4a9b-bda7-afff76474a40\",\"expireDate\":null,\"properties\":[{\"alias\":\"categories\",\"value\":\"Debt\"},{\"alias\":\"subCategory\",\"value\":\"[\\r\\n \\\"Liquid\\\",\\r\\n \\\"FMP\\\",\\r\\n \\\"Gilt Medium & Long Term\\\",\\r\\n \\\"Gilt Short Term\\\",\\r\\n \\\"Income\\\",\\r\\n \\\"Others\\\",\\r\\n \\\"Short Term\\\",\\r\\n \\\"Ultra Short Term\\\",\\r\\n \\\"Mid Cap\\\",\\r\\n \\\"Small Cap\\\",\\r\\n \\\"Floaters\\\",\\r\\n \\\"Gilt Fund\\\",\\r\\n \\\"Dynamic Bond\\\"\\r\\n]\"},{\"alias\":\"icon\",\"value\":\"\\/media\\/1141\\/debt.png\"}],\"releaseDate\":null,\"disabled\":false}]}
或者没有反斜杠:
{"fieldsets":[{"alias":"categoriesSubCategories","allowedMemberGroups":"","id":"170e1ba0-6f05-4a11-a2a6-04e108e7fa1f","expireDate":null,"properties":[{"alias":"categories","value":"Equity"},{"alias":"subCategory","value":"[\r\n \"Banking\",\r\n \"FMCG\",\r\n \"Infrastructure\",\r\n \"International Funds\",\r\n \"Large Cap\",\r\n \"Large & Mid Cap\",\r\n \"Mid Cap\",\r\n \"Balanced Fund\",\r\n \"Multi Cap\",\r\n \"Others\",\r\n \"Pharma\",\r\n \"Tax Planning\",\r\n \"Daily\",\r\n \"Small Cap\",\r\n \"Mid & Small Cap\",\r\n \"Diversified\"\r\n]"},{"alias":"icon","value":"\/media\/1138\/equity.png"}],"releaseDate":null,"disabled":false},{"alias":"categoriesSubCategories","allowedMemberGroups":"","id":"aabf640c-1ba4-4136-9dd0-79f3dfd6b79b","expireDate":null,"properties":[{"alias":"categories","value":"Balanced"},{"alias":"subCategory","value":"[\r\n \"Debt-Oriented Aggressive\",\r\n \"Debt-Oriented Conservative\",\r\n \"credit opportunities\",\r\n \"Equity Oriented\",\r\n \"Asset Allocation\",\r\n \"Others\",\r\n \"Yearly\",\r\n \"Advance\"\r\n]"},{"alias":"icon","value":"\/media\/1139\/balanced.png"}],"releaseDate":null,"disabled":false},{"alias":"categoriesSubCategories","allowedMemberGroups":"","id":"fac9c316-16dc-48ab-bc0d-e8c22c02378f","expireDate":null,"properties":[{"alias":"categories","value":"Tax Saver"},{"alias":"subCategory","value":"[\r\n \"Any\"\r\n]"},{"alias":"icon","value":"\/media\/1140\/tax-saver.png"}],"releaseDate":null,"disabled":false},{"alias":"categoriesSubCategories","allowedMemberGroups":"","id":"ef1efeda-3334-4a9b-bda7-afff76474a40","expireDate":null,"properties":[{"alias":"categories","value":"Debt"},{"alias":"subCategory","value":"[\r\n \"Liquid\",\r\n \"FMP\",\r\n \"Gilt Medium & Long Term\",\r\n \"Gilt Short Term\",\r\n \"Income\",\r\n \"Others\",\r\n \"Short Term\",\r\n \"Ultra Short Term\",\r\n \"Mid Cap\",\r\n \"Small Cap\",\r\n \"Floaters\",\r\n \"Gilt Fund\",\r\n \"Dynamic Bond\"\r\n]"},{"alias":"icon","value":"\/media\/1141\/debt.png"}],"releaseDate":null,"disabled":false}]}
您可以将第二个传递给JSON验证器,它是有效的。第一个不是真的因为“ \ r \ n” arent正确地逃脱了。
另一方面,对于在JSON中漂亮打印的使用并没有兴趣,我想说的是在JSONStringified中的使用。只需添加标签,新行char,添加杂音和请求的内存即可。