0

Bitte schauen Sie sich die unten angegebene JSON an.Wie man JSON-Werte anpasst und die Daten in entsprechende Spalten in SWIFT 3 einfügt

Ich muss rollNumber als Zeilen und subjects als Spalten setzen und marks unter entsprechende Betreff-Spalte setzen. Spalten enthalten alle 5 Themen. Hier ist das, was ich getan habe:

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 
} 

Ich habe transponiert marksArray genommen, die vom Typ [[Int]], aber OUTPUT ist das Ergebnis, das ich bekommen haben. Ich muss die Fächer (Schlüssel der Werte) gegeben in score mit den Werten von subjects Array und dann setzen Sie die marks unter entsprechenden Betreff Spalte.Wo gibt es keine Markierungen für ein bestimmtes Thema, da muss ich ein '- '(Strich). Bitte schlagen Sie vor, wie kann ich erwartetes Ergebnis erhalten.

OUTPUT:

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

ERWARTENDE OUTPUT

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

Die AKTUALISIERT JSON ist wie folgt:

{ 
"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" 
     } 
    } 
    } 
} 

Code JSON zu analysieren:

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

Was ich dachte Elemente subjectsArray und scoreSubjectsArray zu vergleichen, den Index der ungewöhnlichen Elemente finden und marksArray.insert("-", at: indexOfUncommonItem) tun. Aber ich weiß nicht, wie ich das machen soll. Jede Hilfe oder Anregung geschätzt.

+0

Wie lautet der Code zum Parsen des JSON? Das Problem liegt da. – Larme

+0

@Larme Ich habe den JSON in einer anderen Klasse analysiert und diesen in meinem DataViewController aufgerufen. Um Parsing-Objekte zu analysieren, habe ich verschachtelte For-Schleifen verwendet, da es Dictionary of Dictionaries ist. Die nächsten Tasten selbst sind Werte der vorherigen Tasten. Daran habe ich gedacht. –

+0

@Larme Bitte überprüfen Sie die aktualisierte Frage. Ich habe Code aktualisiert, um JSON zu analysieren –

Antwort

0

Ich habe Ihrem Data-Objekt Code hinzugefügt. Bitte versuchen Sie folgendes:

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

Dank eine Tonne gegeben. Es funktionierte..!! –

Verwandte Themen