swift 3服务器响应与奇怪的字典格式

时间:2017-10-01 15:56:29

标签: json dictionary swift3 response

将不胜感激 我用奇怪的字典格式获得服务器响应

Data: {
    "featureHelpshift" = "true";
    "featureAppleTVSynchronisation" = "false";
    "featureDirectPlay" = "true";
    "googleLoginSwitchOn" = "true";
    "serverVersion" = "6.11.1";
    "facebookLoginSwitchOn" = "true";
    "allAvailableWorlds" = (
        {
            "name" = "\U0420\U043e\U0441\U0441\U0438\U044f 16 (\U043d\U043e\U0432\U044b\U0439)";
            "mapURL" = "https://backend2.lordsandknights.com/maps/LKWorldServer-RU-16";
            "worldStatus" = {
                "id" = "3";
                "description" = "online";
            };
            "country" = "RU";
            "language" = "ru";
            "id" = "190";
            "url" = "https://backend2.lordsandknights.com/XYRALITY/WebObjects/LKWorldServer-RU-16.woa";
        },...

首先我尝试使用swiftyJSON和json序列化程序将此响应序列化为JSON

比我得到数据响应并将其解析为String

func some () {
    Alamofire.request(serverRequest, method: .post, parameters: parameters).responseData { response in

            if let data = response.result.value, let utf8Text = String(data: data, encoding: .utf8) {
                //print("Data: \(utf8Text)") //- server response that shown above i print here
                print("Change->")
                let str = self.responseToJsonParsing(stringResponse: utf8Text)
                print("Data: \(str)")
            }

        }
}

我尝试将我的数据字符串响应引导到JSON字符串格式,创建用于解析的函数:

func responseToJsonParsing(stringResponse: String) -> String {
        var parseResponse = stringResponse.replacingOccurrences(of: ";\n\t\t}", with: "\n\t\t}", options: .literal)
        parseResponse = parseResponse.replacingOccurrences(of: ";\n\t\t\t}", with: "\n\t\t\t}", options: .literal)
        parseResponse = parseResponse.replacingOccurrences(of: ";\n\t}", with: "\n\t}", options: .literal)
        parseResponse = parseResponse.replacingOccurrences(of: ";\n}", with: "\n}", options: .literal)
        parseResponse = parseResponse.replacingOccurrences(of: ";", with: ",")
        parseResponse = parseResponse.replacingOccurrences(of: "=", with: ":")
        parseResponse = parseResponse.replacingOccurrences(of: "(", with: "[")
        parseResponse = parseResponse.replacingOccurrences(of: ")", with: "]")

        return parseResponse
    }

解析后我仍然无法创建JSON或词典

0 个答案:

没有答案