Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 在Swift中解析PHP Web服务Replt_Json_Swift - Fatal编程技术网

Json 在Swift中解析PHP Web服务Replt

Json 在Swift中解析PHP Web服务Replt,json,swift,Json,Swift,我是Swift新手,但我正在尝试使用基于PHP的web服务返回回复:- { responseData = { 1 = { name = "3D SYSTEMS CORP"; result = success; }; 10 = { name = "ABERCROMBIE & FITCH CO-CL A";

我是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的有效性:
  • JSON中有几个问题。更正后的JSON如下:

    {
        "responseData": {
            "1": {
                "name": "3D SYSTEMS CORP",
                "result": "success"
            },
            "10": {
                "name": "ABERCROMBIE & FITCH CO-CL A",
                "result": "success"
            }
        }
    }
    
  • responseData是字典而不是数组。更改代码以处理此字典而不是数组

  • HTH

    谢谢,我正在使用第三方web服务,因此无法控制格式,是否有任何方法可以使用当前的响应格式处理此问题,谢谢您的帮助