简体   繁体   English

在iOS应用程序中解析Json Response时出现问题,在Dictionary中将“ =”替换为“:”

[英]Issue in parsing Json Response in iOS application, getting “=” at the place of “:” in Dictionary

I am working on an iOS application and using swift in it. 我正在开发iOS应用程序,并在其中使用swift。 I am calling an Rest api and response is JSON. 我正在调用Rest api,响应是JSON。

Here is my code: 这是我的代码:

{

        let request = NSMutableURLRequest(url: NSURL(string: path)! as URL)

        // Set the method to POST
        request.httpMethod = "POST"

        do {
            // Set the POST body for the request

            let jsonBody = try JSONSerialization.data(withJSONObject: body, options: .prettyPrinted)
            request.httpBody = jsonBody

            request.addValue("application/json", forHTTPHeaderField: "Content-Type")
            request.addValue("application/json", forHTTPHeaderField: "Accept")

            //            request.addValue("Cookie", forHTTPHeaderField: session_Id)

            let session = URLSession.shared

            let task = session.dataTask(with: request as URLRequest, completionHandler: {data, response, error -> Void in

                if let jsonData = data {

                    print("\(data?.debugDescription)")
                    do {

                        print("JSON Response String: \(String.init(data: data!, encoding: .utf8))")

                        let dict:[String:Any] = (try JSONSerialization.jsonObject(with: jsonData, options: []) as? [String:Any])!


                        print("JSON Response Dictionary: \(dict)")

                        onCompletion(dict, nil)

                    } catch {
                        // report ERROR

                        print("caught: \(error)")

                        onCompletion(nil, error as! NSError)

                    }



                } else {


                    print(error)
                    onCompletion(nil, error as! NSError)
                }
            })
            task.resume()
        } catch {
            // Create your personal error


            onCompletion(nil, nil)
        }
    }

And This is the response of api: 这是api的响应:

======== - Fetch CC list api request - =============
["userID": "898465844"]
======== - Fetch CC list api request - =============

JSON Response String: "{\"status\":\"success\",\"card_list\":[{\"cardType\":\"Visa\",\"cardholderName\":null,\"expirationMonth\":\"01\",\"expirationYear\":\"2020\",\"cardImage\":\"https://assets.braintreegateway.com/payment_method_logo/visa.png?environment=sandbox\",\"cardNumber\":\"411111******1111\",\"token\":\"348nws\"}]}"


JSON Response Dictionary: ["status": success, "card_list": <__NSSingleObjectArrayI 0x1c060f350>(
{
    cardImage = "https://assets.braintreegateway.com/payment_method_logo/visa.png?environment=sandbox";
    cardNumber = "411111******1111";
    cardType = Visa;
    cardholderName = "<null>";
    expirationMonth = 01;
    expirationYear = 2020;
    token = 348nws;
}
)
]

After parsing I am getting "=" at the place of ":" in "card_list" array of dictionary. 解析后,我在字典的“ card_list”数组中的“:”处得到“ =”。

So I am not able to figure out why I am getting "=" at the place of ":". 因此,我无法弄清楚为什么在“:”处出现“ =”。

The format of your response is not look like JSON. 您的响应格式看起来不像JSON。 Its property list or (XML) try to use - PropertyListSerialization.propertyList(from:...) or some XML parsers 它的属性列表或(XML)尝试使用PropertyListSerialization.propertyList(from:...)或某些XML解析器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM