2016-09-07 3 views
-1

Swift를 사용하여 JSON 배열을 가져 오는 데 문제가 있습니다.문제 JSON 배열 가져 오기

func getBenefitJSON() -> [AnyObject] { 
     let urlString = String("https://www.kyfb.com/KYFB/includes/themes/CodeBlue/display_objects/custom/remote/webservices/services.cfc?method=getMemberBenefits") 
     let url = URL(string: urlString!) 
     var data = Data() 
     do { 
      data = try Data(contentsOf: url!) 
     } catch { 

     } 

     print("URL Data: \(data)") 

     do { 
      let json = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions()) as! [AnyObject] 
      print("JSON: \(json)") 
      return json 
     } catch { 
      print("Could not get JSON") 
      return [] 
     } 
    } 

URL에서 데이터를 가져 오지만 빈 배열이 반환됩니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까? 다음은

URL Data: 42769 bytes 
Could not get JSON 

내가 또한 jsonlint.com

URL의 URL의 응답을 검증 한 오류

UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.} 

아래

[{"image":"https://cdn.kyfb.com/KYFB/cache/file/70162997-E06B-E9B6-88514280CA8397CC_medium.jpg","description":"","link":"https://www.kyfb.com/insurance/insurance-products/","name":"KFB Insurance","children":[]}, ...] 

JSON

의 응답입니다 데이터를 문자열로 :

<wddxPacket version='1.0'><header/><data><string>[{"image":"https://cdn.kyfb.com/KYFB/cache/file/70162997-E06B-E9B6-88514280CA8397CC_medium.jpg","description":"","link":"https://www.kyfb.com/insurance/insurance-products/","name":"KFB Insurance","children":[]}, ...]</string></data></wddxPacket>) 
+2

실제로 무슨 일이 일어나고 있는지 오류를 잡는 것이 좋습니다. – Desdenova

+0

해당 URL에 도달 할 수 있습니까? –

+0

@RomanPodymov 예, 접근 가능합니다. – raginggoat

답변

-1

코드를 테스트했으며 JSON의 최상위 수준이 배열 인 경우 작동합니다. 따라서 읽고 자하는 JSON은 배열이 아니라 사전입니다.

사전을 읽으려면 [AnyObject]에서 [String:AnyObject]으로 반환 유형을 변경하고이 유형의 다른 모든 항목을 변경해야합니다.

그럼에도 Swift의 JSON 작업에는 SwiftyJSON을 사용하는 것이 좋습니다.

+0

JSON의 일부를 제 질문에 추가했습니다. 최상위 레벨은 배열입니다. – raginggoat