我正在用 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 \\"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\"}],\"alias\":\"categoriesSubCategories\",\"disabled\":false,\"id\":\"170e1ba0-6f05-4a11-a2a6-04e108e7fa1f\",\"releaseDate\":null,\"expireDate\":null,\"allowedMemberGroups\":\"\"},{\"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\"}],\"alias\":\"categoriesSubCategories\",\"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\":\"categoriesSubCategories\",\"disabled\":false,\"id\":\"fac9c316-16dc-48ab-bc0d-e8c22c02378f\",\"releaseDate\":null,\"expireDate\":null,\"allowedMemberGroups\":\"\"},{\"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\"}],\"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)
这不成功。
最佳答案
您的 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: "]",
这使得替换成为可能:
[
与[
在 convertedStr3]"
与]
在 convertedStr4
尝试print(convertedStr4)
,您应该能够复制结果并检查它jsonlint.com验证 json 格式是否正确。
关于ios - 使用字符替换制作有效的 jSON 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55587590/