2016-05-13 70 views
0

因此我正在下载这个json文件并且该部分工作正常。我测试了它,并且字符串可以正常工作。但现在我正在尝试使用JSON选项将文件解析到类中的变量中。我认为我在解析器函数中设置了一切,下载功能正常工作。所以我认为问题是当我试图将响应改变为ta json类型时。我不知道我是否正确地做这件事。 这是我到目前为止有:使用SwiftyJson的JSON解析器不解析任何使用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) 

} 

这里是JSON文件它会被解析

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

感谢您对本

回答

0

确定好新的任何帮助,得到它的工作,这里是我如何管理它,如果别人是同一个地方。

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 

现在我知道了,知道是成功的一半