Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios JSON到多维数组_Ios_Swift_Multidimensional Array_Swifty Json - Fatal编程技术网

Ios JSON到多维数组

Ios JSON到多维数组,ios,swift,multidimensional-array,swifty-json,Ios,Swift,Multidimensional Array,Swifty Json,我试图从HTTP帖子中获取JSON,并将其放入多维数组中,用于Swift中的节/表单元格 我希望每个表节使用这些动态键(submitid),并为每个表节插入单元格数据: 15302992338145 15301374235890 15302930963080 我的JSON: let swiftyJsonVar = JSON(data!) { "data" : { "15302992338145" : [ { "date" : "2018-06-27",

我试图从HTTP帖子中获取JSON,并将其放入多维数组中,用于Swift中的节/表单元格

我希望每个表节使用这些动态键(submitid),并为每个表节插入单元格数据:

15302992338145
15301374235890
15302930963080
我的JSON:

let swiftyJsonVar = JSON(data!)

{
  "data" : {
    "15302992338145" : [
      {
        "date" : "2018-06-27",
        "username" : "user1",
        "submitid" : 15302992338145,
        "notes" : "Testing"
      },
      {
        "date" : "2018-06-28",
        "username" : "user1",
        "submitid" : 15302992338145,
        "notes" : "Testing"
      }
    ],
    "15301374235890" : [
      {
        "date" : "2018-06-21",
        "username" : "user2",
        "submitid" : 15301374235890,
        "notes" : "Comments one two three"
      },
      {
        "date" : "2018-06-22",
        "username" : "user2",
        "submitid" : 15301374235890,
        "notes" : "N/A"
      }
    ],
    "15302930963080" : [
      {
        "date" : "2018-07-03",
        "username" : "user3",
        "submitid" : 15302930963080,
        "notes" : "Hello"
      }
    ]
  }
}
我试过了,但运气不好:

if let resData = swiftyJsonVar["data"][].arrayObject {
    self.arrRes = resData as! [String: [[String:AnyObject]]]
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return arrRes.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("tableCell", forIndexPath: indexPath)

    // Configure the cell...
    var dict = arrRes[indexPath.section][indexPath.row]

    cell.dateLabel?.text = dict["date"]

    return cell
}

您应该停止使用SwiftyJSON,转而使用Swift 4和Decodable:

struct User : Decodable {
    let date : String
    let username : String
    let submitid : Int
    let notes : String
}
struct Result : Decodable {
    let data : [[User]]
    struct AnyCodingKey : CodingKey {
        var stringValue: String
        var intValue: Int?
        init(_ codingKey: CodingKey) {
            self.stringValue = codingKey.stringValue
            self.intValue = codingKey.intValue
        }
        init(stringValue: String) {
            self.stringValue = stringValue
            self.intValue = nil
        }
        init(intValue: Int) {
            self.stringValue = String(intValue)
            self.intValue = intValue
        }
    }
    init(from decoder: Decoder) throws {
        let con = try! decoder.container(keyedBy: AnyCodingKey.self)
        let intermediate = try! con.decode([String:[User]].self, 
            forKey: AnyCodingKey(stringValue:"data"))
        var data = [[User]]()
        for d in intermediate {
            data.append(d.value)
        }
        self.data = data
    }
}
// jsondata is your original JSON data, as you downloaded it
let result = try! JSONDecoder().decode(Result.self, from: jsondata)
然后,
result.data
是一个用户数组的数组

[[User(date: "2018-07-03", username: "user3", 
       submitid: 15302930963080, notes: "Hello")], 
 [User(date: "2018-06-27", username: "user1", 
       submitid: 15302992338145, notes: "Testing"), 
  User(date: "2018-06-28", username: "user1", 
       submitid: 15302992338145, notes: "Testing")], 
 [User(date: "2018-06-21", username: "user2", 
       submitid: 15301374235890, notes: "Comments one two three"), 
  User(date: "2018-06-22", username: "user2", 
       submitid: 15301374235890, notes: "N/A")]]

您应该停止使用SwiftyJSON,转而使用Swift 4和Decodable:

struct User : Decodable {
    let date : String
    let username : String
    let submitid : Int
    let notes : String
}
struct Result : Decodable {
    let data : [[User]]
    struct AnyCodingKey : CodingKey {
        var stringValue: String
        var intValue: Int?
        init(_ codingKey: CodingKey) {
            self.stringValue = codingKey.stringValue
            self.intValue = codingKey.intValue
        }
        init(stringValue: String) {
            self.stringValue = stringValue
            self.intValue = nil
        }
        init(intValue: Int) {
            self.stringValue = String(intValue)
            self.intValue = intValue
        }
    }
    init(from decoder: Decoder) throws {
        let con = try! decoder.container(keyedBy: AnyCodingKey.self)
        let intermediate = try! con.decode([String:[User]].self, 
            forKey: AnyCodingKey(stringValue:"data"))
        var data = [[User]]()
        for d in intermediate {
            data.append(d.value)
        }
        self.data = data
    }
}
// jsondata is your original JSON data, as you downloaded it
let result = try! JSONDecoder().decode(Result.self, from: jsondata)
然后,
result.data
是一个用户数组的数组

[[User(date: "2018-07-03", username: "user3", 
       submitid: 15302930963080, notes: "Hello")], 
 [User(date: "2018-06-27", username: "user1", 
       submitid: 15302992338145, notes: "Testing"), 
  User(date: "2018-06-28", username: "user1", 
       submitid: 15302992338145, notes: "Testing")], 
 [User(date: "2018-06-21", username: "user2", 
       submitid: 15301374235890, notes: "Comments one two three"), 
  User(date: "2018-06-22", username: "user2", 
       submitid: 15301374235890, notes: "N/A")]]

上面的3个键(如15302992338145)是静态的?不,它们将从POST响应中更改…抱歉,这只是一个示例。上面的3个键(如15302992338145)是静态的?不,它们将从POST响应中更改…抱歉,这只是一个示例。