Hilfe bei JSON

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

  • Hilfe bei JSON

    Hallo zusammen ich bin ganz neu was das Thema iOS Entwicklung angeht. Leider hab ich hier im Forum bislang keine Suchfunktion gefunden.

    Ich hab ein Swift Beispiel gefunden für eine Loginfunktionalität die perr JSON den Benutzer und das Passwort an einen REST Webservice übergibt.

    Quellcode

    1. postLogInURL.httpMethod = "POST"
    2. postLogInURL.addValue("application/json", forHTTPHeaderField: "Content-Type")
    3. postLogInURL.addValue("application/json", forHTTPHeaderField: "Accept")
    4. let json: [String: Any] = ["username": "\(self.usernameTextBox.text!)","password": "\(self.passwordTextBox.text!)"]
    5. let jsonData = try? JSONSerialization.data(withJSONObject: json)
    6. postLogInURL.httpBody = jsonData


    Das ist im Prinzip auch das was ich benötige, nur meine JSON Datei muss folgenderrmaßen aufgebaut sein.


    Quellcode

    1. {
    2. "platform":{
    3. "login": {
    4. "userName": "user",
    5. "password": "kennwort"
    6. }
    7. }
    Alles anzeigen
    Was muss ich bei mir ändern dass meine JSON auch den richtigen Aufbau hat? Kann mir bitte jemand helfen ?

    Danke
  • apfelfan schrieb:

    Leider hab ich hier im Forum bislang keine Suchfunktion gefunden.
    [...]
    Was muss ich bei mir ändern dass meine JSON auch den richtigen Aufbau hat? Kann mir bitte jemand helfen ?
    Oben rechts findest Du ein Lupensymbol mit dem nebenstehenden Text "Aktuelles Forum durchsuchen" (oder auf der Eingangsseite "Alle Foren durchsuchen"). Damit kann man ... suchen :)

    Bzgl. Deines JSON-Problems schau Dir mal die Methode dataWithJSONObject:options:error: an. Dort kann man z. B. die Option NSJSONWritingPrettyPrinted mitgeben, die sollte das gewünschte Ergebnis liefern.

    Mattes
    Diese Seite bleibt aus technischen Gründen unbedruckt.
  • ich hab es jetzt einmal so versucht:

    Quellcode

    1. import UIKit
    2. // Prepare URL
    3. let url = URL(string: "https://url/api/login")!
    4. var request = URLRequest(url: url)
    5. // Configure request authentication
    6. //request.setValue("authToken", forHTTPHeaderField: "Authorization")
    7. request.setValue("application/xml; utf-8", forHTTPHeaderField: "Content-Type")
    8. request.setValue("application/xml", forHTTPHeaderField: "Acept")
    9. // Serialize HTTP Body data as JSON
    10. let body = ["userName": "myuser", "password": "mypassword"]
    11. let bodyData = try? JSONSerialization.data(
    12. withJSONObject: body,
    13. options: []
    14. )
    15. // Change the URLRequest to a POST request
    16. request.httpMethod = "POST"
    17. request.httpBody = bodyData
    18. let session = URLSession.shared
    19. let task = session.dataTask(with: request) { (data, response, error) in
    20. // Check for Error
    21. if let error = error {
    22. print("Error took place \(error)")
    23. return
    24. }
    25. // Convert HTTP Response Data to a String
    26. if let data = data, let dataString = String(data: data, encoding: .utf8) {
    27. print("Response data string:\n \(dataString)")
    28. }
    29. }
    30. task.resume()
    Alles anzeigen
    leider weiß ich immer noch nicht wie ich das mit dem json so hinbekomme dass auch plattform und login drin sind,
    kann mir da bitte jemand mit einem beispiel helfen ? Wärre sehr nett. Danke
  • Du baust das JSON aktuell so zusammen:

    Quellcode

    1. let json: [String: Any] = ["username": "\(self.usernameTextBox.text!)","password": "\(self.passwordTextBox.text!)"]
    Das ergibt folgendes JSON:

    Quellcode

    1. {
    2. username: "user",
    3. password: "kennwort"
    4. }
    Was du benötigst ist das:

    Quellcode

    1. {
    2. "platform":{
    3. "login": {
    4. "userName": "user",
    5. "password": "kennwort"
    6. }
    7. }
    8. }
    Du musst also dein Dictionary entsprechend aufbauen. So wie MCDan gesagt hat
    Knowing is not enough, we must apply.
    Willing is not enough, we must do.
  • apfelfan schrieb:

    marceo und genau da liegt meine problem, ich verstehe was MCDan meint weiß aber nicht wie ich das mir dem

    Quellcode

    1. let json: [String: Any] = ["username": "\(self.usernameTextBox.text!)","password": "\(self.passwordTextBox.text!)"]
    zusammenbekomme
    Es steht eigentlich hier alles in diesem Thread da. Du musst nur noch die Teile richtig kombinieren. Ist ein bisschen wie im Matheunterricht, wo man Formeln ineinander einsetzen muss. ;)
  • Quellcode

    1. let json: [String: Any] = ["username": "\(self.usernameTextBox.text!)","password": "\(self.passwordTextBox.text!)"]
    ist ein Dictionary [String: Any] mit den keys "username" und "password"
    Das ergibt folgendes JSON:

    Quellcode

    1. {
    2. username: "user",
    3. password: "kennwort"
    4. }

    Der nächste Schritt wäre, dieses Dictionary in ein weiteres Dictionary zu packen mit dem key "login"

    Quellcode

    1. {
    2. "login": {
    3. "userName": "user",
    4. "password": "kennwort"
    5. }
    6. }

    und das Ergebnis dann in ein Dictionary mit dem Key "platform":

    Quellcode

    1. {
    2. "platform":{
    3. "login": {
    4. "userName": "user",
    5. "password": "kennwort"
    6. }
    7. }
    8. }
    btw: der Key "unserName" muss mit großem N sein
    Knowing is not enough, we must apply.
    Willing is not enough, we must do.