简体   繁体   English

使用SwiftyJson的JSON解析器未使用Alamofire解析任何信息

[英]JSON parser using SwiftyJson is not parsing any information using Alamofire

so I am downloading this json file and that part works. 所以我正在下载这个json文件,那部分工作了。 I tested it and the string one works fine. 我测试了一下,字符串one正常工作。 But now I am trying to use the JSON option to parse out the file into variable in the class. 但是现在我试图使用JSON选项将文件解析为类中的变量。 I think I set up everything right in the parser function and the downloading function works fine. 我想我在解析器功能中正确设置了所有内容,并且下载功能正常运行。 So I think the problem is when I am trying to change the response into ta json type. 所以我认为问题是当我尝试将响应更改为ta json类型时。 I don't know if I am doing that part right. 我不知道我是否做对了。 This is what I have so far: 这是我到目前为止的内容:

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)

}

Here is the Json file its will be parsing 这是将要解析的Json文件

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

Thank you for any help with this 谢谢你的帮助

OK good new got it working, here is how I managed it if someone else is as the same spot. 好,好新的东西可以正常工作,如果有人在同一地点,这就是我的管理方法。

  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

Now I know and knowing is half the battle 现在我知道,知道是成功的一半

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM