2016-05-13 10 views
0

, so dass ich diese JSON-Datei herunterladen und dieser Teil funktioniert. Ich habe es getestet und die Saite funktioniert einwandfrei. Aber jetzt versuche ich, die JSON-Option zu verwenden, um die Datei in Variable in der Klasse zu analysieren. Ich denke, ich habe alles richtig in der Parser-Funktion eingerichtet und die Download-Funktion funktioniert gut. Also ich denke, das Problem ist, wenn ich versuche, die Antwort in ta json Typ zu ändern. Ich weiß nicht, ob ich diesen Teil richtig mache. Das ist, was ich bisher habe:JSON-Parser mit SwiftyJson analysiert keine Informationen mit Alamofire

func downlaodPromoData(myUrl : String, myUser : String, myPass : String) 
{ 
    Alamofire.request(.GET, myUrl) 
     .authenticate(user: myUser, password: myPass) 
     .validate(statusCode: 200..<300) 
     .responseString { response in 
      //print("Success: \(response.result.isSuccess)") 
      //print("Response String: \(response.result.value)") 
      self.downloadJson = response.result.value! 
      //print("Calling parser") 
      //self.parsePromoJson(self.downloadJson) 


     }.responseJSON { response in 
      print("Response JSON: \(response.result.value)") 
      let swiftyJsonVar = JSON(response.result.value!) 
      self.parseCustomerInfo(swiftyJsonVar) 

      //let userJson = JSON() as! NSDictionary 
      //parseCustomerInfo(userJson) 


    } 
} 

/* 
var barcodeNumber : String = "" 
var customerName : String = "" 
var totalPointsEarned : String = "" 
var pointsEarned : String = "" 
var rank : String = "" 
*/ 
func parseCustomerInfo(json : JSON) 
{ 
    print("Starting parsing") 
    for result in json[""].arrayValue { 
     barcodeNumber = result["barcode_id"].stringValue 
     customerName = result["name"].stringValue 
     totalPointsEarned = result["total_points_earned"].stringValue 
     pointsEarned = result["points_available_to_spend"].stringValue 
     rank = result["rank"].stringValue 


    } 
    customerName = "Gus" 
    print("new Customer name" + customerName) 
    print("Updateing ui") 
    let myData : String = "UserName: " + customerName + "\n" + "Total Points: " + totalPointsEarned + "\n" + "Ava Apoints: " + pointsEarned + "\n" + "Rank: " + rank 
    uiResultsTextField.text.appendContentsOf(myData) 

} 

Hier wird die Json seiner Parsen

{ 
    "id" : 220, 
    "name" : "King Gus", 
    "total_points_earned" : null, 
    "points_available_to_spend" : null, 
    "rank" : null, 
    "order_history" : [ ], 
    "barcode_id" : "C-00000220" 
} 

mit dieser

Antwort

0

OK guten neues für jede Hilfe Danke wird Datei hätte es funktioniert, Hier ist, wie ich es geschafft habe, wenn jemand anders als der gleiche Ort ist.

Alamofire.request(.GET, myUrl) 
     .authenticate(user: myUser, password: myPass) 
     .validate(statusCode: 200..<300) 
     .responseString { response in 
      //print("Success: \(response.result.isSuccess)") 
      //print("Response String: \(response.result.value)") 
      self.downloadJson = response.result.value! 
      print("Json file: " + self.downloadJson) 
      self.parseCustomerInfo(self.downloadJson) 

    } 
}//end of downloadCustomer function 

/* 
//Customer Information 
var myBarcode : String = "" 
var myName : String = "" 
var myTotalPointsEarned : String = "" 
var myPointsEarned : String = "" 
var myRank : String = "" 
*/ 

func parseCustomerInfo(json : String) 
{ 
    print("Starting parsing") 
    if let data = json.dataUsingEncoding(NSUTF8StringEncoding) { 
     let newJson = JSON(data: data) 
     myBarcode = newJson["barcode_id"].stringValue 
     myName = newJson["name"].stringValue 
     myTotalPointsEarned = newJson["total_points_earned"].stringValue 
     myPointsEarned = newJson["points_available_to_spend"].stringValue 
     myRank = newJson["rank"].stringValue 

    } 
    print("new Customer name: " + myName) 
    print("Barcode number: " + myBarcode) 
    print("Points Earned: " + myPointsEarned) 
    print("Total points: " + myTotalPointsEarned) 
    print("Rank: " + myRank) 
    if myTotalPointsEarned.isEmpty{ myTotalPointsEarned = "0"} 
    if myRank.isEmpty{myRank = "Baby-bee"} 
    if myPointsEarned.isEmpty {myPointsEarned = "0"} 

    //setup all the UI with the downloaded information 
    barcodeNumber.text = myBarcode 
    customer.text = myName 
    totalPoints.text = myTotalPointsEarned 
    avaPoints.text = myPointsEarned 
    rank.text = myRank 
    let img = Barcode.fromString(myBarcode) 
    barcode.image = img 
    self.view = MyPageView 


}//of of parser function 

Jetzt weiß ich, und Wissen ist die halbe Schlacht

Verwandte Themen