我是 Swift 新手,但正在尝试使用基于 PHP 的 Web 服务来返回回复:-
{
responseData = {
1 = {
name = "3D SYSTEMS CORP";
result = success;
};
10 = {
name = "ABERCROMBIE & FITCH CO-CL A";
result = success;
};
};
}
我假设这是有效的 JSON 格式,但无法弄清楚如何将此数据(只是字符串列表)加载到字符串数组中。我有这样的 trie 代码来迭代一个 rte 元素,但没有成功。
let url = urlComponents.URL
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {
(data, response, error) -> Void in
if let urlContnet = data
{
do {
let jsonResult = try NSJSONSerialization.JSONObjectWithData(urlContnet, options: NSJSONReadingOptions.MutableContainers)
if let nudges = jsonResult["responseData"] as? NSArray {
for nudge in nudges {
print(nudge)
}
}
}
catch
{
print("ERROR")
}
}
}
task.resume()
谁能帮忙
谢谢
史蒂夫
最佳答案
这里有两件事
- 您的 JSON 格式错误。您可以在以下位置检查 JSON 的有效性: http://jsonlint.com
您的 JSON 中有几个问题。更正后的 JSON 在这里:
{
"responseData": {
"1": {
"name": "3D SYSTEMS CORP",
"result": "success"
},
"10": {
"name": "ABERCROMBIE & FITCH CO-CL A",
"result": "success"
}
}
}
- responseData 是字典而不是数组。更改您的代码以处理此字典而不是数组。
HTH
关于json - 在 Swift 中解析 PHP Web 服务 Replt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35820905/