Wie mit einem JSON Object eines REST Calls umgehen?

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Wie mit einem JSON Object eines REST Calls umgehen?

    Ich bekomme von einem REST Call Daten zurück die ich mit if let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any]
    in ein JSON Objekt umwandeln lasse. Wenn ich mir das Objekt nun mit print anzeigen lasse bekomme ich das hier:

    ["ConfigItem": <__NSSingleObjectArrayI 0x6000000060f0>( { CIXMLData = { AllIPAddresses = "192.168.0.60"; AssignedCompany = 13003; CPU = "Intel(R) Xeon(R) CPU 3060 @ 2.40GHz(2400 MHz)"; FQDN = WORKGROUP; HardDisk = ( { Capacity = "238.44 GB "; HardDisk = "HP LOGICAL VOLUME SCSI Disk Device (238.4 GB) "; }, { Capacity = " 11177.91 GB "; HardDisk = " HP LOGICAL VOLUME SCSI Disk Device (11177.9 GB) "; }, { Capacity = " 1863.01 GB"; HardDisk = " WDC WD20 EARX-00PASB0 USB Device (1863 GB)"; }, "", "" ); IPAddress = "192.168.0.60"; Model = "ProLiant DL320s G1"; NIC = ( { IPoverDHCP = Yes; MACAdress = "00:1E:0B:F9:93:DC"; NIC = "[00000007] Broadcom NetXtreme-Gigabit-Ethernet "; }, { IPoverDHCP = No; NIC = " [00000010] Broadcom NetXtreme-Gigabit-Ethernet"; }, "", "", "" ); NumberOfCPUCores = 2; NumberOfPhysicalCPU = 1; OperatingSystem = "Microsoft Windows Server 2008 R2 Standard"; Ram = 8190; SerialNumber = BG874064GB; Type = Server; Vendor = HP; }; Class = Computer; ConfigItemID = 16; CreateBy = 3; CreateTime = "2017-02-08 10:08:42"; CurDeplState = Production; CurDeplStateType = productive; CurInciState = Operational; CurInciStateType = operational; DefinitionID = 41; DeplState = Production; DeplStateType = productive; InciState = Operational; InciStateType = operational; LastVersionID = 24; Name = "HPS"; Number = 0022000077; VersionID = 24; } ) ] 1 ("ConfigItem", <__NSSingleObjectArrayI 0x6000000060f0>( { CIXMLData = { AllIPAddresses = "192.168.0.60"; AssignedCompany = 13003; CPU = "Intel(R) Xeon(R) CPU 3060 @ 2.40GHz(2400 MHz)"; FQDN = WORKGROUP; HardDisk = ( { Capacity = "238.44 GB "; HardDisk = "HP LOGICAL VOLUME SCSI Disk Device (238.4 GB) "; }, { Capacity = " 11177.91 GB "; HardDisk = " HP LOGICAL VOLUME SCSI Disk Device (11177.9 GB) "; }, { Capacity = " 1863.01 GB"; HardDisk = " WDC WD20 EARX-00PASB0 USB Device (1863 GB)"; }, "", "" ); IPAddress = "192.168.0.60"; Model = "ProLiant DL320s G1"; NIC = ( { IPoverDHCP = Yes; MACAdress = "00:1E:0B:F9:93:DC"; NIC = "[00000007] Broadcom NetXtreme-Gigabit-Ethernet "; }, { IPoverDHCP = No; NIC = " [00000010] Broadcom NetXtreme-Gigabit-Ethernet"; }, "", "", "" ); NumberOfCPUCores = 2; NumberOfPhysicalCPU = 1; OperatingSystem = "Microsoft Windows Server 2008 R2 Standard"; Ram = 8190; SerialNumber = BG874064GB; Type = Server; Vendor = HP; }; Class = Computer; ConfigItemID = 16; CreateBy = 3; CreateTime = "2017-02-08 10:08:42"; CurDeplState = Production; CurDeplStateType = productive; CurInciState = Operational; CurInciStateType = operational; DefinitionID = 41; DeplState = Production; DeplStateType = productive; InciState = Operational; InciStateType = operational; LastVersionID = 24; Name = "HPS"; Number = 0022000077; VersionID = 24; } ) )

    Ich weiß jetzt nicht wie ich an die Daten in dem Object ran kommen. ich möchte die gerne in einer Tableview darstellen lassen. Ich habe schon versucht im Debugger (wenn ein Haltepunkt gesetzt ist) mit "po" herauszufinden was geht, aber mehr als "po jason["ConfigItem"] bekomme ich nicht raus. In diesem "ConfigItem" steckt aber noch mehr drin, wie man sehen kann.

    Kann mir vielleicht jemand weiter helfen?

    Danke im voraus.

    Dirk
  • class func jsonObject(with data: Data, options opt: JSONSerialization.ReadingOptions = []) throws -> Any
    Description
    Returns a Foundation object from given JSON data.
    The data must be in one of the 5 supported encodings listed in the JSON specification: UTF-8, UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE. The data may or may not have a BOM. The most efficient encoding to use for parsing is UTF-8, so if you have a choice in encoding the data passed to this method, use UTF-8.
    Note
    In Swift, this method returns a nonoptional result and is marked with the throws keyword to indicate that it throws an error in cases of failure.
    You call this method in a try expression and handle any errors in the catch clauses of a do statement, as described in Error Handling in The Swift Programming Language (Swift 3.0.1) and Error Handling in Using Swift with Cocoa and Objective-C (Swift 3.0.1).
    Parameters
    data
    A data object containing JSON data.
    options
    Options for reading the JSON data and creating the Foundation objects.
    For possible values, see JSONSerialization.ReadingOptions.
    Returns
    A Foundation object from the JSON data in data, or nil if an error occurs.
    Availability
    iOS (5.0 and later), macOS (10.7 and later), tvOS (9.0 and later), watchOS (2.0 and later)
    Declared In
    Foundation
    More
    Type Method Reference
  • Eine Methode wie ich an die Daten in dem Objekt komme wäre super. Ich stelle mir vor das ich das irgendwie in ein Dictionary bekomme um die Daten dann in ein Tableview zu bekommen.
    Bei Arrays kenne ich es das ich an die Elemente mit den objectAtIndex komme, aber wie mache ich das bei diesem JSON Objekt?

    Gruß

    Dirk
  • Ein JSON OBject besteht aus Arrays und Dictionaries. Von daher must du dir das Object nur ansehen. Dann siehst du z.B. an der [ am Anfang, dass der Root Container ein Array ist. Darin kommen dann Objecte mit {. Das bedeutet das sind Dictionaries.
    2 Stunden Try & Error erspart 10 Minuten Handbuchlesen.

    Pre-Kaffee-Posts sind mit Vorsicht zu geniessen :)
  • ich habe mir eine Schleife gebastelt:

    var key:String = ""
    for key in json["ConfigItem"] as! [AnyObject] {

    }
    Jetzt sollte ja in key ein Array stecken, oder?
    Wenn ich aber versuche key[0] auszugeben kommt ein Fehler: "fatal error: unexpectedly found nil wile unwrapping an Optional value"

    Was mache ich denn nur falsch? Irgendwie kapiere ich nicht wie ich an das komme was ich brauche
  • Danke an alle die mich in die richtige Richtig geschubst haben. Ich habe es soweit hinbekommen das ich ein Dictionary heraus bekomme. Nun muss ich noch dynamisch damit umgehen können, das es in denn dictionary noch weiter Dictionarys geben kann und diese Extrahieren.
    Und wenn es sonst niemanden stört würde ich bei Problemen gerne wieder hier nachfragen.
  • @gritsch Ich weiß nicht so ganz wie du das meinst. Meine Lösung bisher:
    for var configItem in json["ConfigItem"] as! [AnyObject]{ dict = configItem["CIXMLData"] as! NSDictionary}
    Damit kann ich jetzt auf das Dictionary zugreifen mit dict.object(forKey: "Ram")
    Ich bin aber offen für alle Vereinfachungen. Die Schleife dürfte wohl noch übertrieben sein.