将 JSON 数组字符串转换为 JSON 数组返回 null

Posted

技术标签:

【中文标题】将 JSON 数组字符串转换为 JSON 数组返回 null【英文标题】:Converting JSON array string to JSON array returns null 【发布时间】:2019-06-24 16:01:14 【问题描述】:

我使用 alamofire 来处理请求。这里有一个简单的响应,我从 alamofire 得到这个 JSON 字符串

Optional("[\"nl2br\":\"2019-05-02 19:52:10\",\"nl3br\":\"\",\"nl4br\":\"#lxr\",\"nl5br\":\"1\",\"nl6br\":\"nein\",\"nl7br\":\"bARxrdw9c7WS1RN9c\\/\\/MrA==822b5dd20cb73611:35cab43d0c7e82e73c62818d9f90cfe8\",\"nl8br\":\"tBDSzkQ7eCNERwldgZiwKg==a7590d9d8affdbe5:63b9a9bf6421ea7db2a55f8773990b08\",\"nl9br\":\"fpd17pxVUzcJWNskVWyBeA==cf21783de8334248:a93e8b8a92ef9b238b75ad87a315ce3e\",\"nl2br\":\"2019-04-26 21:36:32\",\"nl3br\":\"old\",\"nl4br\":\"\",\"nl5br\":null,\"nl6br\":\"ja\",\"nl7br\":\"emjUIzK92fWwHNLv\\/4xv2Q==8fca202c9a816c7b:655ef8aaaa0212bb9e77a9c35a56c3a7\",\"nl8br\":\"1C5Hy\\/ZhMk3b6SZY8c08lw==cb3b0f4fdd6b5957:034e72d400598fedc43b3111d841a31b\",\"nl9br\":\"KUfLHbih9612dhNCYPOrTg==48da9c3362430e01:3a80ba9fb516e4172aa39b017abcd96e\",\"nl2br\":\"2019-04-26 21:20:24\",\"nl3br\":\"holder\",\"nl4br\":\"\",\"nl5br\":null,\"nl6br\":\"ja\",\"nl7br\":\"J59nMBWUMyU9PzdbwQqd0g==f7d3c6c561c33f0b:3465e9131a2577c39cddcc606d0785b0\",\"nl8br\":\"G4rRevk951ZBJJQKzBeJZg==45c0ebcdc1e000e8:dc770c3849fef8b058f85cad42c73e1d\",\"nl9br\":\"338Ud9CIDgnyBV96F2Fx\\/w==d0f825064e0ecf81:0ca00ff27e977ef5f8123d38c643a8b3\",\"nl2br\":\"2019-04-10 23:57:17\",\"nl3br\":\"\",\"nl4br\":\"Wasmes, here\'s a video and do ya need?\",\"nl5br\":\"1\",\"nl6br\":\"ja\",\"nl7br\":\"fw17O1L9SuO9FS3qu6U7QQ==706bd09ee3b5ec55:6667d9020a0edcacbd217f631d2305c0\",\"nl8br\":\"2tTE9+0Kr6cVSuEOsq8h0w==3e0e46352da323be:941710bd881546dfa08d3afb6aea2831\",\"nl9br\":\"NAkMx0OsKwXVGndYXbAFmA==1234c0b2992502bf:666f8efd3bdf8ee8b0a2ee4e7222c2ef\",\"nl2br\":\"2019-02-22 21:38:16\",\"nl3br\":\"i\'m giving up on you,  but don\'t \",\"nl4br\":\"\",\"nl5br\":null,\"nl6br\":\"nein\",\"nl7br\":\"gOv13Dsxf1N2UG46KT3tvQ==dd02a83b1837bb24:4f7c7ab95e873d35685ab7b738626f44\",\"nl8br\":\"40+GOpcNU6M4F688DJyxFA==70bc177842f3f6cf:232b065423bd2683d75ca177775fd352\",\"nl9br\":\"mBc3pEHKBiiPeAEpZRskug==71b353fed6403b99:c58099676beacc146c14867645e29783\"]")

我像这样获取我的数据并像这样转换它

if let responseData = response.data 
  let responseDataString = String(data: responseData, encoding:String.Encoding(rawValue: String.Encoding.utf8.rawValue))
  print(responseDataString) // gives the whole string above
  if let obj = responseDataString?.toJSON() as? [String:AnyObject] 
    print(obj["nl2br"] as! String) // gives nothing, just nil
  

我的字符串扩展是这样的。

extension String 
    func toJSON() -> Any? 
        guard let data = self.data(using: .utf8, allowLossyConversion: false) else  return nil 
        return try? JSONSerialization.jsonObject(with: data, options: .mutableContainers)
    

知道为什么会出现这种行为吗?我尝试打印 obj 也没有给出任何结果。

【问题讨论】:

不相关但, encoding:String.Encoding(rawValue: String.Encoding.utf8.rawValue)) 是有趣的语法。为什么不简单地, encoding: .utf8)?为什么.mutableContainers?所有.mutable... 阅读选项在 Swift 中都毫无意义。省略参数。 【参考方案1】:

您的根是数组而不是字典更改responseDataString?.toJSON() as? [[String:Any]]

extension String 
    func toJSON() -> Any? 
        return try? JSONSerialization.jsonObject(with: Data(self.utf8))
    

let responseDataString:String? =  """[\"nl2br\":\"2019-05-02 19:52:10\",\"nl3br\":\"\",\"nl4br\":\"#lxr\",\"nl5br\":\"1\",\"nl6br\":\"nein\",\"nl7br\":\"bARxrdw9c7WS1RN9c\\/\\/MrA==822b5dd20cb73611:35cab43d0c7e82e73c62818d9f90cfe8\",\"nl8br\":\"tBDSzkQ7eCNERwldgZiwKg==a7590d9d8affdbe5:63b9a9bf6421ea7db2a55f8773990b08\",\"nl9br\":\"fpd17pxVUzcJWNskVWyBeA==cf21783de8334248:a93e8b8a92ef9b238b75ad87a315ce3e\",\"nl2br\":\"2019-04-26 21:36:32\",\"nl3br\":\"old\",\"nl4br\":\"\",\"nl5br\":null,\"nl6br\":\"ja\",\"nl7br\":\"emjUIzK92fWwHNLv\\/4xv2Q==8fca202c9a816c7b:655ef8aaaa0212bb9e77a9c35a56c3a7\",\"nl8br\":\"1C5Hy\\/ZhMk3b6SZY8c08lw==cb3b0f4fdd6b5957:034e72d400598fedc43b3111d841a31b\",\"nl9br\":\"KUfLHbih9612dhNCYPOrTg==48da9c3362430e01:3a80ba9fb516e4172aa39b017abcd96e\",\"nl2br\":\"2019-04-26 21:20:24\",\"nl3br\":\"holder\",\"nl4br\":\"\",\"nl5br\":null,\"nl6br\":\"ja\",\"nl7br\":\"J59nMBWUMyU9PzdbwQqd0g==f7d3c6c561c33f0b:3465e9131a2577c39cddcc606d0785b0\",\"nl8br\":\"G4rRevk951ZBJJQKzBeJZg==45c0ebcdc1e000e8:dc770c3849fef8b058f85cad42c73e1d\",\"nl9br\":\"338Ud9CIDgnyBV96F2Fx\\/w==d0f825064e0ecf81:0ca00ff27e977ef5f8123d38c643a8b3\",\"nl2br\":\"2019-04-10 23:57:17\",\"nl3br\":\"\",\"nl4br\":\"Wasmes, here\'s a video and do ya need?\",\"nl5br\":\"1\",\"nl6br\":\"ja\",\"nl7br\":\"fw17O1L9SuO9FS3qu6U7QQ==706bd09ee3b5ec55:6667d9020a0edcacbd217f631d2305c0\",\"nl8br\":\"2tTE9+0Kr6cVSuEOsq8h0w==3e0e46352da323be:941710bd881546dfa08d3afb6aea2831\",\"nl9br\":\"NAkMx0OsKwXVGndYXbAFmA==1234c0b2992502bf:666f8efd3bdf8ee8b0a2ee4e7222c2ef\",\"nl2br\":\"2019-02-22 21:38:16\",\"nl3br\":\"i\'m giving up on you,  but don\'t \",\"nl4br\":\"\",\"nl5br\":null,\"nl6br\":\"nein\",\"nl7br\":\"gOv13Dsxf1N2UG46KT3tvQ==dd02a83b1837bb24:4f7c7ab95e873d35685ab7b738626f44\",\"nl8br\":\"40+GOpcNU6M4F688DJyxFA==70bc177842f3f6cf:232b065423bd2683d75ca177775fd352\",\"nl9br\":\"mBc3pEHKBiiPeAEpZRskug==71b353fed6403b99:c58099676beacc146c14867645e29783\"]"""
if let obj = responseDataString?.toJSON() as? [[String:Any]]  
   obj.forEach 
     print($0["nl2br"])
    

结果

可选(2019-05-02 19:52:10)

可选(2019-04-26 21:36:32)

可选(2019-04-26 21:20:24)

可选(2019-04-10 23:57:17)

可选(2019-02-22 21:38:16)


编辑:在json这个

[ 
   "name":"ppp"
]

是要被[[String:Any]]通知嵌套[[]]的字典数组,而这个

 
   "name":"ppp"

是由[String:Any]表示的字典,只通知[]

【讨论】:

[String:AnyObject] 是字典,[String:AnyObject] 是数组的字典,不是吗?

以上是关于将 JSON 数组字符串转换为 JSON 数组返回 null的主要内容,如果未能解决你的问题,请参考以下文章

PHP json_encode转换空数组为对象

如何将字符串数组转换为 JSON 对象?

将数组转换为有效的JSON字符串,而不使用JSON.stringify?

Xamarin 表单将 Json 数组转换为 C# 并返回 JSON 数组

将数组转换为 JSON 时,json_encode 返回 JSON_ERROR_UTF8 [关闭]

如何从函数返回(bigint,双精度)数组以将其转换为json数组