2016-06-06 2 views
0
[ 
    -{ 
     valid:"2", 
     invalid: "1", 
     pending: "2" 
    }, 
    -{ 
     valid:"0", 
     invalid: "1", 
     pending: "0" 
    }, 
    -{ 
     valid:"2", 
     invalid: "1", 
     pending: "2" 
    } 
] 

이 원격 json을 구문 분석하고 데이터를 배열에 채우려고합니다. 나는 왜 내 코드가 작동하지 않는지 알아 내려고 애쓰며, 배열은 항상 비어있게된다. 누군가 내가 뭘 잘못하고 있는지 말해 줄 수 있니?스위프트의 원격 json 파싱

var arrayreports : [Report] = [] 
    var report = Report() 
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 
    let host = appDelegate.host 
    if(Reachability.isConnectedToNetwork()){ 

     let postEndpoint: String = host+"/api/reportbyworkflow/7" 

     let session = NSURLSession.sharedSession() 
     let url = NSURL(string: postEndpoint)! 

     session.dataTaskWithURL(url, completionHandler: { (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in 
      guard let realResponse = response as? NSHTTPURLResponse where 
       realResponse.statusCode == 201 else { 


        print("Bad thing happened") 
        return 
      } 

      do { 


       if let ipString = NSString(data:data!, encoding: NSUTF8StringEncoding) { 


        let jsonDictionary:AnyObject! = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) 

        let json = jsonDictionary as? Array<AnyObject> 


        for index in 0...json!.count-1 { 
         let contact : AnyObject? = json![index] 
         print(contact) 

         let collection = contact! as! Dictionary<String, AnyObject> 
        let valid = collection["valid"] as! String 
        let invalid = collection["invalid"] as! String 
        let pending = collection["pending"] as! String 

        report!.valid = Double(Int(valid)!) 
        report!.invalid = Double(Int(invalid)!) 
        report!.pending = Double(Int(pending)!) 


         arrayreports.append(report!) 
        } 

       }} 


      catch { 

       print("bad things happened") 


      } 



     }).resume() 

    } 

답변

1

json이 정말로 여기에 복사 한 것이면 유효하지 않습니다 (jsonvalidator.com 확인).

따라서 직렬화가 빈 배열을 반환하는 것보다 정상입니다.