代码之家  ›  专栏  ›  技术社区  ›  Nitish

JSONSerialization.jsonObject返回nil

  •  0
  • Nitish  · 技术社区  · 6 年前
    var responseString = String(data: data, encoding: .utf8)
    var responseDict: [AnyHashable : Any]? = nil
    if let anEncoding = responseString?.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) {
       responseDict = try! JSONSerialization.jsonObject(with: anEncoding, options: .mutableContainers) as? [AnyHashable : Any]
    }  
    

    结果是:

    就是你的信息的样子!在文本中键入您的消息 区域并获得正确的预览 “这里”是这样一种情况,而“数据”数据的数据的数据的数据的:{},\“深度链接url”的\““,\“附件url”的地址地址的\““,\“数据”数据的数据的数据的数据的数据的数据的数据的数据的},\“数据数据数据的数据的数据的数据的数据的数据的数据的数据的数据的数据的数据的数据的数据的数据,“,\“附件url的地址地址地址的地址的地址”的,\“标题”标题的标题的标题的标题:“\“,,,\“副标题”字幕的字幕的数据的数据的数据的数据的数据的数据的数据的数据的数据的数据的数据的数据的数据的数据的数据::::}}}}}}}}}}}}}}}}}}},,,,,,,,分钟\“:\”0\“,\”延迟秒\“:\”0\“,\”消息\“:\”此 就是你的信息的样子!在文本中键入您的消息 区域并获得正确的预览

    即将成为 nil .
    Objective-C 代码运行得很好。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Ashley Mills    6 年前

    这个很好用

    let json = """
    [{\"message_id\":1916,\"in_app\":{\"primary_color\":\"#333333\",\"secondary_color\":\"#ffffff\",\"position\":\"0\",\"duration\":\"30\",\"expiry\":\"2018-11-07T23:17:46.169-05:00\",\"delay_minute\":\"0\",\"delay_second\":\"0\",\"message\":\"This is what your message will look like! Type in your message in the text area and get a preview right here\",\"data\":{},\"deeplinkurl\":\"\",\"attachment-url\":\"\",\"title\":\"\",\"subtitle\":\"\"}},{\"message_id\":1920,\"in_app\":{\"primary_color\":\"#333333\",\"secondary_color\":\"#ffffff\",\"position\":\"0\",\"duration\":\"30\",\"expiry\":\"2018-11-08T03:52:15.404-05:00\",\"delay_minute\":\"0\",\"delay_second\":\"0\",\"message\":\"This is what your message will look like! Type in your message in the text area and get a preview right here\",\"data\":{},\"deeplinkurl\":\"\",\"attachment-url\":\"\",\"title\":\"\",\"subtitle\":\"\"}}]
    """.data(using: .utf8)!
    
    do {
        let object = try JSONSerialization.jsonObject(with: json, options: [])
        print(object)
    } catch {
        print(error)
    }
    

    JSONSerialization 声明表示数据的自定义对象并使用 Decodable