ios - 使用字符替换制作有效的 jSON 字符串

标签 ios json swift

我正在用 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)

这不成功。

最佳答案

您的 convertedStr3convertedStr4 应该如下所示:

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/

相关文章:

ios - 使用MPMoviePlayerController流音频

c# - 在 C# 中从 SQL Server 检索大量数据的最简单方法

ios - 取消 NSOperationQueue 上的所有操作后不久发生此异常,我该怎么办

Swift firestore 同时获取多个集合的文档?

ios - 在 3.5 和 4 英寸外形尺寸之间使用自动布局调整 View 大小

ios在页面更改时暂停音频

javascript - 转换 JSON 对象数组,从两个属性值创建新属性

ios - 带有导致内存泄漏的变量的Swift闭包

ios - 在 xcode 6.1 和 os x yosemite 中不支持 ios 7.1 及以下版本?

javascript - 通过查找值 AngularJS 在数组中添加键