如何匹配JSON值并将数据放在SWIFT 3中的相应列中

时间:2017-08-17 05:53:30

标签: ios dictionary multidimensional-array swift3 multiple-columns

请查看下面给出的 JSON

我必须将rollNumber设置为行,将subjects设置为列,并将marks放在相应的主题列下。列将包含所有5个主题。 这就是我所做的:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

if case (1...(subjectsArray.count + 1), 1...(rollNoArray.count + 1)) = (indexPath.column, indexPath.row)
{
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: DataCell.self), for: indexPath) as! DataCell
let text = marksArrayTranspose[indexPath.column-1][indexPath.row - 1]

    if !text.isEmpty
    {
        cell.label.text = text
    }
    else {
        cell.label.text = "-"

    }
    return cell
}
return nil
}

我已经采用了[All]类型的marksArray转置,但 OUTPUT 是我得到的结果。我必须将score中给出的主题(值的键)subjects数组的值进行匹配,然后将marks放在相应的主题列下。特定主题没有标记,我必须加上' - '(短划线)。请告诉我,如何获得预期输出结果。

输出:

      | Mathematics | English | Science | History | Geography |
---------------------------------------------------------------
100   |      82     |   90    |    80   |    -    |     -     |
101   |      95     |   78    |    89   |   82    |    80     |
102   |      74     |   81    |    71   |   68    |     -     |

预期输出

      | Mathematics | English | Science | History | Geography |
---------------------------------------------------------------
100   |      -      |   82    |    90   |   80    |     -     |
101   |      95     |   78    |    89   |   82    |    80     |
102   |      74     |   81    |    71   |    -    |    68     |

更新 JSON如下:

{
"subjects": [
  "Mathematics",
  "English",
  "Science",
  "History",
  "Geography"
],
"data": [
  {
    "rollNumber" : 100,
    "studentName": "Mary Alex"
  },
  {
    "rollNumber" : 101,
    "studentName": "John Smith"
  },
  {
    "rollNumber" : 102,
    "studentName": "Anna Brook"
  }
],
"score": {
  "100": {
    "English": {
        "status": "present",
        "marks" : "82",
        "remark": "excellent"
    },
    "Science": {
        "status": "present",
        "marks" : "90",
        "remark": "excellent"
      },
    "History": {
        "status": "present",
        "marks" : "80",
        "remark": "excellent"
      }
  },
  "101": {
    "Mathematics": {
        "status": "present",
        "marks" : "95",
        "remark": "excellent"
      },
    "English": {
        "status": "present",
        "marks" : "78",
        "remark": "excellent"
      },
    "Science": {
        "status": "present",
        "marks" : "89",
        "remark": "excellent"
      },
    "History": {
        "status": "present",
        "marks" : "82",
        "remark": "excellent"
      },
    "Geography": {
        "status": "present",
        "marks" : "80",
        "remark": "excellent"
      }
    },
  "102": {
    "Mathematics": {
        "status": "present",
        "marks" : "74",
        "remark": "good"
      },
    "English": {
        "status": "present",
        "marks" : "81",
        "remark": "excellent"
      },
    "Science": {
        "status": "present",
        "marks" : "71",
        "remark": "good"
      },
    "Geography": {
        "status": "present",
        "marks" : "68",
        "remark": "satisfactory"
      }
    }
  }
}

解析JSON的代码:

class Data: NSObject
{
var marksArray = [[String]]()
public required init(dictionary: [String : Any])
{ super.init()
if let dataValues = dictionary["score"] as? Dictionary<String,Dictionary<String,Any>>
{
    let sortedDataValues = dataValues.sorted(by: { $0.key < $1.key })
//  --------------- iterate over first dictionary  -------------------------
    for(key, value) in sortedDataValues
    {
        //  rollNumbers are keys of first Dict
        let rollNumber:String = key

        // subjects are keys of values of first Dict
        let subjects = value.sorted { $0.key > $1.key}
        let marks = subjects.flatMap() {$0.value}
        let subMarks = (marks as AnyObject).value(forKey: "marks")
        marksArray.append(subMarks as! [String])
    }
}}}

我在想的是比较subjectArray和scoreSubjectsArray的元素,找到不常见元素的索引并执行marksArray.insert("-", at: indexOfUncommonItem)。但我不知道该怎么做。任何帮助或建议表示赞赏。

1 个答案:

答案 0 :(得分:0)

我在您的Data对象中添加了一些代码。请尝试以下方法:

class Data: NSObject
{
    var marksArray = [[String]]()
    public required init(dictionary: [String : Any]) {
        super.init()
        let subjectsArray:[String] = dictionary["subjects"] as! [String]
        if let dataValues = dictionary["score"] as? Dictionary<String,Dictionary<String,Any>>
        {
            let sortedDataValues = dataValues.sorted(by: { $0.key < $1.key })
            //  --------------- iterate over first dictionary  -------------------------
            for(key, value) in sortedDataValues
            {
                //  rollNumbers are keys of first Dict
                let rollNumber:String = key

                // subjects are keys of values of first Dict
                var subjects = value.sorted { $0.key > $1.key}
                let subjectsKeys = subjects.map{$0.key}
                var x = 0
                for i in subjectsArray {
                    if !subjectsKeys.contains(i) {
                        subjects.insert((key: i, value: ["marks":"-"]), at: x)
                    }
                    x += 1
                }
                let marks = subjects.flatMap() {$0.value}
                let subMarks = (marks as AnyObject).value(forKey: "marks")
                marksArray.append(subMarks as! [String])
            }
        }
    }
}