0
我试图将一个JSON响应迅速转换为一个可用的字典。这似乎是一个简单的任务,但是我得到的JSON响应格式很奇怪,无论我尝试什么,我都无法将它转换为字典。所有的谷歌的例子,我已经能够找到假定JSON响应的格式如下:iOS Swift Casting JSON响应与奇怪的格式到字典
{
"someKey": 42.0,
"anotherKey": {
"someNestedKey": true
},
{
"someKey": 42.0,
"anotherKey": {
"someNestedKey": true
}
然而,在迅速我用下面的格式为代码在接收到打印响应如下:
{assets = (
{
"someKey": 42.0,
"anotherKey": {
"someNestedKey": true
},
{
"someKey": 42.0,
"anotherKey": {
"someNestedKey": true
}
);
}
这是尽我所能试图将这些数据转换成字典在迅速。它将“资产”添加为字典中的单个关键字,该关键字的值就是整个响应的其余部分。
let url = URL(string: "https://\(apiKey):\(password)@\(yourStore).myshopify.com/admin/themes/\(currentThemeID)/assets.json")!
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
print(error)
} else {
if let urlContent = data {
do {
let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: [.allowFragments, JSONSerialization.ReadingOptions.mutableContainers])
print(jsonResult)
if let dictionary = jsonResult as? [String: [String]] {
print(dictionary)
}
} catch {
print("json processing failed")
}
}
}
}
task.resume()
我很确定挂断存在于JSON响应中两个“括号”和“分号”的存在。我无法找到任何有关这些角色如何影响响应的文档,或者在尝试快速下调时如何处理它们。
任何帮助,将不胜感激!
编辑: 我拉起我的浏览器中的JSON响应,而这里的格式:
{"assets":[{"key":"assets\/1-1.png","public_url":"https:\/\/cdn.shopify.com\/s\/files\/1\/0810\/2125\/t\/22\/assets\/1-1.png?5272098227851596200","created_at":"2016-05-16T16:58:27-05:00","updated_at":"2016-05-16T16:58:27-05:00","content_type":"image\/png","size":9127,"theme_id":124078279}{"key":"templates\/search.liquid","public_url":null,"created_at":"2016-05-16T16:59:04-05:00","updated_at":"2016-05-16T16:59:04-05:00","content_type":"text\/x-liquid","size":2931,"theme_id":124078279}]}
这JSON响应不具有资产=();部分,并且格式正确。不知何故,我的swift代码不正确地解析数据?
这个响应是'print(jsonResult)'的结果还是你得到的实际响应数据?这是无效的json – dan
@dan我在浏览器中提出了响应,并且在那里格式正确。我在上面的问题编辑中发布了snippit。这告诉我这个问题存在于我的Swift代码中,以及它如何解析数据? –
要回答你的问题@dan,它是在swift中打印的结果,而不是实际的json响应。 –