json - 在 Swift/iOS 中解析 JSON 响应

标签 json swift nsarray

我有以下 JSON。

[{"chatId":"1","user1_id":"1212","user2_id":"8543211123","user1_name":"dave","user2_name":"daveee","user1_profile_pic":"http:\/\/graph.facebook.com\/1212\/picture?type=large","user2_profile_pic":"https:\/\/scontent-waw1-1.xx.fbcdn.net\/v\/t1.0-9\/1212.jpg?oh=c288ac7b31a61aee751e8ddafb05e78a&oe=57DC702E","message":{"1":{"message_id":"24242241","sender":"1212","chatId":"1","text":"hello i am","timestamp":"2016-05-24 17:13:08"},"2":{"message_id":"421421","sender":"1212","chatId":"1","text":"great","timestamp":"2016-05-24 17:15:08"}}},{"chatId":"2","user1_id":"23413524635","user2_id":"1212","user1_name":"Leo","user2_name":"dave","user1_profile_pic":"https:\/\/scontent-fra3-1.xx.fbcdn.net\/v\/l\/t1.0-1\/1212.jpg?oh=1212&oe=579AE3AE","user2_profile_pic":"http:\/\/graph.facebook.com\/1212\/picture?type=large","message":{"1":{"message_id":"21321213","sender":"1212","chatId":"2","text":"yes, hi","timestamp":"2016-05-25 15:46:57"}}}]

我想遍历 message并为每个计数器提取值。这是我的代码:

for anItem in jsonArray as! [Dictionary<String, AnyObject>]
var chat_messages : [Message]? = nil
var count_messages = 0;
if let dataArray = anItem["message"] as? [Int : Dictionary<String, AnyObject> ] {
    for onemessage in dataArray as! [Dictionary<String, AnyObject>] {
        let curr_message = Message()
        if let messageid = onemessage["message_id"] as? String {
            curr_message.id =  messageid
        }
        if let messagedate = onemessage["timestamp"] as? NSDate {
            curr_message.date = messagedate
        }
        if let messagesender = onemessage["sender"] as? String {
            curr_message.sender = messagesender
        }
        if let messagetext = onemessage["text"] as? String {
            curr_message.text = messagetext
        }
        chat_messages![count_messages] = curr_message
        count_messages = count_messages + 1
    }
}

问题是 if let dataArray = anItem["message"] as? [Int : Dictionary<String, AnyObject> ] { 行总是失败并且 if永远不会输入条件。

最佳答案

如果你正在使用 NSJSONSerialization,那么每个字典都是这样的类型:[String : AnyObject]。所以这一行:

if let dataArray = anItem["message"] as? [Int : Dictionary<String, AnyObject> ] {

应该是:

if let dataArray = anItem["message"] as? [String : AnyObject] {

要遍历 dataArray 中的消息字典,您可以替换:

for onemessage in dataArray as! [Dictionary<String, AnyObject>] {

与:

for (_, messageDictionary) in dataArray {
    if let onemessage = messageDictionary as? [String : AnyObject] {

以及从 onemessage 字典中获取各种值的其余代码应该像您现在编写的那样工作,除了这一行会崩溃:

chat_messages![count_messages] = curr_message

因为你被强制展开 chat_messages,你初始化为 nil:

var chat_messages : [Message]? = nil

而不是一个空数组:

var chat_messages = [Message]()

关于json - 在 Swift/iOS 中解析 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37446984/

相关文章:

php - 如何在 PHP 中对 JSON 数据进行分页?

javascript - 使用javascript,如何转换为JSON、类似Java-POJO的类?

iphone - 如何避免 NSDictionary 中出现 UTF8 字符?

ios - 混淆将 Flutter App 的 iOS 部分连接到 Firebase

ios - 通过对具有匹配 ID 号的对象进行分组来重建 NSArray?

objective-c - 创建一个用计数 N 初始化的 NSArray,都是同一个对象

java - Android Json Parse 不返回任何内容

c# - 序列化包含字典的对象,以便字典键/值呈现为包含对象的一部分

ios - 只在某些特定的 ViewController 中启用横向

arrays - 将 Firestore 数据加载到 TableView Swift 4