JSON被解析为String类型,而不是根据需要键入[String:Any](Swift IOS)

时间:2017-03-08 18:59:10

标签: ios json swift

所以我正在解析一个JSON并将其转换为[String:Any]类型。但是,有时结果类型是String类型,我不知道为什么。下面是用于将JSON转换为字典的代码:

var request = URLRequest(url: URL(string: url)!)
    request.httpMethod = "POST"
    let postString = ""
    request.httpBody = postString.data(using: .utf8)
    let task = URLSession.shared.dataTask(with: request) { data, response, error in
        guard let data = data, error == nil else {
            //check for fundamental networking error
            print("error=\(error)")
            return
        }
        if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
            //check for http errors
            print("statusCode should be 200, but is \(httpStatus.statusCode)")
            print("response = \(response)")
        }
        _ = String(data: data, encoding: .utf8)!
        do{
            JSON = try JSONSerialization.jsonObject(with: data, options: []) as! [String:Any]
            print(JSON)
        }
        catch {
            print(error.localizedDescription)
        }
    }

现在奇怪的是,下面的第一个JSON作为类型[String:Any]返回,但第二个JSON只返回类型String,即使它们几乎相同:

输入[String:Any]:

["state": Closed, "description": GLOBAL -  upgrade redirect not working, "severity": S3 - Medium, "products": Internal, "gmps": {
}, "type": INCIDENT, "Escalation": , "summary": , "serviceNames": <__NSSingleObjectArrayI 0x6100000165a0>(
Web Publishing
)
, "ticket": INC-136278, "ettr": , "SocialMedia": , "services": Development & Engineering, "InternalDetails": , "oss": Desktop, "sent": 2017-03-08 16:46:42, "regions": Global, "InternalActions": support teams performed controlled restarts of the affected service, "sirs": {
    "INC-136286" =     {
        CarriersNotified = "";
        ExternalName = "Web Publishing";
        ServiceImpact = "Content publishing will be unavailable on external website ";
        StateOfService = "No_Impact";
        downtime = 77;
        end = "2017-03-08 16:01:00";
        location = Global;
        region = Global;
        resource = "Web Publishing - Content/Redirects";
        sevicerecord = "SVC-679";
        sirActualDowntime = 77;
        sirEnd = "2017-03-08 16:01:00";
        sirPlannedDowntime = "<null>";
        sirStart = "2017-03-08 14:44:00";
        start = "2017-03-08 14:44:00";
        subsaffected = 1;
    };
}, "notificationSeverity": S3]

键入字符串:

["state": Closed, "description": GLOBAL - MSG - Multiple users reporting client disconnections/client removal on, "severity": S3 - Medium, "products": Internal, "gmps": {
}, "type": INCIDENT, "Escalation": , "summary": , "serviceNames": <__NSArrayI 0x6100000345e0>(
CORP,
SecureConnectPlus-CORP
)
, "ticket": INC-134786, "ettr": , "SocialMedia": , "services": Employee Connectivity & Productivity, "InternalDetails": , "oss": Android, BB10, iOS, "sent": 2017-03-07 15:39:36, "regions": Global, "InternalActions": support teams performed controlled restarts of the affected service, "sirs": {
    "INC-134792" =     {
        CarriersNotified = "";
        ExternalName = "CORP";
        ServiceImpact = "Administrators for Internal may be unable to manage devices from the BES administration console. Impacted employees may be unable to perform new device actions. In addition, they may be unable to perform self service tasks using the self service console. Also, employees may unable to request BB2FA/Strong Authentication pre-authentication (for their VPN).";
        StateOfService = "No_Impact";
        downtime = 29;
        end = "2017-02-28 15:55:00";
        location = Global;
        region = Global;
        resource = "Enterprise Service - Internal";
        sevicerecord = "SVC-1505";
        sirActualDowntime = 29;
        sirEnd = "2017-02-28 15:55:00";
        sirPlannedDowntime = "<null>";
        sirStart = "2017-02-28 15:26:00";
        start = "2017-02-28 15:26:00";
        subsaffected = 0;
    };
    "INC-134800" =     {
        CarriersNotified = "";
        ExternalName = "SecureConnectPlus-CORP";
        ServiceImpact = "employees with Android for Work (including Samsung KNOX) activated devices, may be unable to access internal resources (including Email, PIM, Intranet). with iOS devices using Connect Plus may be unable to browse intranet resources, or Secure Connect Plus dependent internal applications.";
        StateOfService = "No_Impact";
        downtime = 9934;
        end = "2017-03-07 13:00:00";
        location = Global;
        region = Global;
        resource = " BES - Secure Connect Plus - Internal";
        sevicerecord = "SVC-1515";
        sirActualDowntime = 9934;
        sirEnd = "2017-03-07 13:00:00";
        sirPlannedDowntime = "<null>";
        sirStart = "2017-02-28 15:26:00";
        start = "2017-02-28 15:26:00";
        subsaffected = "0.1";
    };`enter code here`
}, "notificationSeverity": S3]

0 个答案:

没有答案