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

问题描述:

请参阅下面给出的JSON如何匹配JSON值并将数据放入SWIFT中的相应列中3

我必须将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 
} 

我已marksArray的转置是类型[INT]],但OUTPUT是结果我有。我必须将的科目(数值键)score中给出的数值相匹配,然后将marks置于各自的主题栏下。如果没有特定科目的标记,则必须将“ - ” '(短划线)。请建议,我如何获得预期输出结果。

OUTPUT:

 | 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]) 
    } 
}}} 

我想是比较subjectsArray和scoreSubjectsArray的元素,发现罕见的元素的索引,做marksArray.insert("-", at: indexOfUncommonItem)。但我不知道该怎么做。任何帮助或建议表示赞赏。

+0

解析JSON的代码是什么?问题在那里。 – Larme

+0

@Larme我已经解析了其他类中的JSON,并在我的DataViewController中调用了它。为了解析'分数'对象,我使用嵌套for循环,因为它是字典词典。下一个键本身就是以前键的值。这就是我想到的。 –

+0

@Larme请检查更新后的问题。我更新了解析JSON的代码 –

我在数据对象中添加了一些代码。请尝试以下操作:

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]) 
      } 
     } 
    } 
} 
+0

非常感谢。有效..!! –