最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】iOS10中JSON中获取不到string或stringValue的值

iOS crifan 2461浏览 0评论

代码:

        var dict:[String : AnyObject] = [String : AnyObject]()

        do {

            let parsedDict = try JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)

            dict = parsedDict as! [String : AnyObject]

        } catch {

            print(“H5 passed json data serialize failed”)

            return

        }

        // current use the url inside dict to jump

        if let url = dict[“url”]?.stringValue {

            let encodedUrl:String = url.encodedUrl

            

            if let pageTitle = dict[“pageTitle”]?.stringValue {

                curTitle = pageTitle

            }

            

            var isShowFilterButton = true

            if let hasFastSwitch = dict[“hasFastSwitch”]?.boolValue {

                if !hasFastSwitch {

                    isShowFilterButton = false

                }

            }

对于json字符串:

jsonString={“url”:”http://123.206.101.36/skrDev/src/report/retail.html?t=1510731939692″,”tabId”:”retail”,”pageTitle”:”零售”,”hasFastSwitch”:true}

转化后的结果:

在iOS11的设备中运行的好好的,可以通过:

dict[“url”]?.stringValue

获取url的string字符串的

结果在iOS10中的iPad,结果却不行了:

ios 10 json string nil

iOS解析json中null终极解决方案 – 一切随缘

JSON parsing returns null to iOS (json string looks correct) – Stack Overflow

json – ios: Compare NSString to “<null>” not working – Stack Overflow

ios – Checking a null value in Objective-C that has been returned from a JSON string – Stack Overflow

ios – Swift. How can I deal with null values into json files? – Stack Overflow

ios 10 json object string nil

iOS SWIFT – JSON object nil after NSJSONSerialization – Stack Overflow

好像是:

NSJSONSerialization.JSONObjectWithData

转换出来的是object

所以不是string

所以string或stringValues是nil?

那么去试试object

(lldb) po dict[“url”]?.object

nil

还是空

Simple and clean way to convert JSON string to Object in Swift – Stack Overflow

ios – How to convert JSON String to JSON Object in swift? – Stack Overflow

调试了半天发现,结果是直接获取值的:

(lldb) po dict[“url”]!

http://x.x.x.x/skrDev/src/report/retail.html?t=1510732624629

所以,想办法去添加:

  • 如果是iOS10,就直接获取:

    • dict[“url”]

  • 如果是iOS11,就用strignValue

    • dict[“url”]?.stringValue

swift json get value nil ios 10

swift json JSONSerialization value nil ios 10

How to handle null JSON values in Swift | Roadfire Software

参考:

if let id = json[“id”] as? Int {

去试试

Convert Dictionary to JSON in Swift – Stack Overflow

swift – Need to adjust NSJSONSerialization to iOS10 – Stack Overflow

调试了一下,还是iOS有点变态的:

对于之前iOS11上正常工作的:

  • dict[“url”]?.stringValue

  • dict[“hasFastSwitch”]?.boolValue

但是在iOS10上,却是:

  • dict[“url”]?.stringValue 得到却是nil

    • 需要改为:dict[“url”] as? String

      • (lldb) po dict[“url”] as? String

      • ▿ Optional<String>

      •   – some : “http://x.x.x.x/skrDev/src/report/retail.html?t=1510734109687”;

  • dict[“hasFastSwitch”]?.boolValue 在iOS10上却是可以工作的

    • (lldb) po dict[“hasFastSwitch”]?.boolValue

    • ▿ Optional<Bool>

    •   – some : true

不过还是:

为了完整兼容iOS10和iOS11,还是改为更靠谱的:

  • dict[“url”] as? String

  • dict[“hasFastSwitch”] as? Bool

【总结】

最终用:

<code>    func intentTo(_ jsonString: String) {
        print("intentTo jsonString=\(jsonString)")

        guard  let jsonData:Data = jsonString.data(using: .utf8) else {
            print("H5 passed json data not utf-8 format")
            return
        }

        var dict:[String : AnyObject] = [String : AnyObject]()

        do {
            let parsedDict = try JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
            dict = parsedDict as! [String : AnyObject]
        } catch {
            print("H5 passed json data serialize failed")
            return
        }

</code>

        // Note: for iOS 10

        // dict[“xxx”]?.stringValue -> nil

        // so change to: dict[“xxx”] as? String

//        if let url = dict[“url”]?.stringValue {

        if let url = dict[“url”] as? String {

            let encodedUrl:String = url.encodedUrl

            

            var isShowFilterButton = true

            //if let hasFastSwitch = dict[“hasFastSwitch”]?.boolValue {

            if let hasFastSwitch = dict[“hasFastSwitch”] as? Bool {

                if !hasFastSwitch {

                    isShowFilterButton = false

                }

            }

确保了:iOS10和iOS11都可以工作。

转载请注明:在路上 » 【已解决】iOS10中JSON中获取不到string或stringValue的值

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.241 seconds, using 22.12MB memory