如何根据json对象中的日期进行分组?

时间:2017-11-07 06:41:26

标签: ios json swifty-json

我想显示此图片中显示的数据。

enter image description here

我的回答json就像

{
  "transactions" : [
    {
      "amount" : "1.03",
      "transaction_type_id" : "20",
      "transaction_date" : "2017-07-27",
      "value_date" : "2017-07-27",
      "message" : "Amount of interest payment received 03-0128146",
      "debit_credit" : "C",
      "foreign_id" : "45745"
    },
    {
      "amount" : "1.33",
      "transaction_type_id" : "20",
      "transaction_date" : "2017-07-26",
      "value_date" : "2017-07-26",
      "message" : "Amount of interest payment received 03-0128322",
      "debit_credit" : "C",
      "foreign_id" : "46135"
    },
   ],
 "success" : 1
}

我想在transaction_date的基础上创建该组。任何人都可以指导我如何做到这一点?我正在使用swiftyJson

2 个答案:

答案 0 :(得分:0)

首先,您需要在Foundation对象中转换JSON响应。

之后应用谓词来实现结果。

目标C:

NSDictionary *dict = [NSDictionary dictionary]; // assign Foundation object that is converted from json result using SwiftyJson


NSMutableArray *arrTransactionDates = [NSMutableArray array];
for (NSDictionary *dictTmp in [dict valueForKey:@"transactions"]) {

    if (![arrTransactionDates containsObject:[dictTmp valueForKey:@"transaction_date"]]) {

        [arrTransactionDates addObject:[dictTmp valueForKey:@"transaction_date"]];
    }
}
NSMutableArray *arrTmp = [NSMutableArray array];
for (int i=0; i<arrTransactionDates.count; i++) {

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"transaction_date=%@",[arrTransactionDates objectAtIndex:i]];
    NSArray *tmp = [[dict valueForKey:@"transactions"] filteredArrayUsingPredicate:predicate];
    [arrTmp addObject:tmp];
}
arrTransactionDates = nil;
NSArray *outputArray = [NSArray arrayWithArray:arrTmp];
arrTmp = nil;
NSLog(@"%@",outputArray);

夫特:

let dict = [String:Array<Dictionary<String, String>>]() // assign Foundation object that is converted from json result using SwiftyJson


    var arrTransactionDates = [String]()
    for dict in dict["transactions"]! {

        if !arrTransactionDates.contains(dict["transaction_date"]!) {

            arrTransactionDates.append(dict["transaction_date"]!)
        }
    }
    var arrTmp = [Array<Dictionary<String, Any>>]()
    for i in 0..<arrTransactionDates.count {

        var tmp:Array = dict["transactions"]!
        tmp = tmp.filter { ($0["transaction_date"]!).range(of: arrTransactionDates[i], options: [.diacriticInsensitive, .caseInsensitive]) != nil }
        arrTmp.append(tmp)
    }
    print(arrTmp)

答案 1 :(得分:0)

收到回复后jsonObj["transactions"].array ?? [JSON]() 这里jsonObj是我的回复词典

var dictionaryNew = Dictionary<String, [JSON]>()
var keyNew = [String]()

for obj in self.transactions {

         let date = obj["transaction_date"].string
         if self.dictionaryNew.index(forKey: date!) == nil {

                     self.dictionaryNew[date!] = [obj]
                     self.keyNew.append(date!)

                 }
                  else {

                      self.dictionaryNew[date!]?.append(obj)
                  }
                }

            self.tblAccountStatement.reloadData()

在numberOfSections方法

return keyNew.count

在numberOfRows方法

return dictionaryNew[keyNew[section]]?.count ?? 0

然后在表中委托方法cellForRowAtIndexPath

cell.lblAmountValueDeducted.text = "+ " + (dictionaryNew[keyNew[indexPath.section]]?[indexPath.row]["amount"].string)!