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

时间:2016-05-13 01:34:06

标签: ios json parsing swift2 alamofire

所以我正在下载这个json文件,那部分有效。我测试了它,字符串一个正常。但现在我正在尝试使用JSON选项将文件解析为类中的变量。我想我在解析器功能中设置了一切,下载功能正常。所以我认为问题是当我试图将响应更改为ta json类型时。我不知道我是否正在做那个部分。 这就是我到目前为止所做的:

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"
}

感谢您对此提供任何帮助

1 个答案:

答案 0 :(得分: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

现在我知道并知道这是成功的一半