2017-10-18 75 views
0
let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [.allowFragments]) 
     if let responseJSON = responseJSON as? [String:Any] { 
      if let tJsonObj = xResponse["d"] as? [[String:Any]] { 
       // not working here... 
      } 
     } 

tJsonObj变量未获取我的json数组内容。 我的JSON是这样的:Swift - 嵌套JSON中的分析数组对象不工作

{"d": "[{\"title\":\"xxx\",\"timestamp\":\"2017-10-16 23:53:40\"},{\"title\":\"Mein Test iPhone 7\",\"timestamp\":\"2017-10-17 18:16:24\"}]"} 

我希望有人能帮助 - 谢谢!

+0

看一看:https://stackoverflow.com/questions/41734982/parsing-nested-array-of-dictionaries-using-object-mapper/41735194#41735194 –

回答

1

d的值是另一个JSON字符串。您需要使用JSONSerialization两次

do { 
    if let responseJSON = try JSONSerialization.jsonObject(with: data) as? [String:Any], 
    let tJsonObj = responseJSON["d"] as? String { 
     if let innerJSON = try JSONSerialization.jsonObject(with: Data(tJsonObj.utf8)) as? [[String:Any]] { 
      for item in innerJSON { 
       print(item) 
      } 
     } 
    } 
} catch { 
    print(error) 
} 
+0

非常感谢,这个解决方案很有效! – Neneil

0

d的内部JSON看起来没有了。有效的JSON应如下所示:

{"d": "[{"title":"xxx","timestamp":"2017-10-16 23:53:40"},... 

您的JSON从哪里来?

+0

的JSON来自我的服务器。对象是使用JavaScriptSerializer创建的。我确定JSON是有效的。 – Neneil