2016-03-29 28 views
0

JSON配列を作成するPHPファイルがあります。これはJSON出力です。Swiftを使用したJSON解析

[{ "EMPLOYEE_ID": "1"、 "EMPLOYEE_NAME": "スティーブ"、 "employee_designation": "VP"、 "employee_salary": "60000"}、{ "EMPLOYEE_ID": "2"、 "employee_id": "3"、 "employee_name": "Luci"、 "employee_designation": "Manager"、 "employee_designation"、 "employee_salary": "20000" "employee_id": "4"、 "employee_name": "Joe"、 "employee_designation": "Executive"、 "employee_salary": "25000"}、{employee_id ":" 5 " 」、 "EMPLOYEE_NAME": "ジュリア"、 "employee_designation": "練習"、 "employee_salary": "10000"}]

私は私のアプリで迅速を使用して、この配列を解析します。だから私は、JSON配列

func jsonParser() { 
    let urlPath = "xxxxxxxxx/dbretrieve.php" 
    guard let endpoint = NSURL(string: urlPath) else { print("Error creating endpoint");return } 
    let request = NSMutableURLRequest(URL:endpoint) 
    NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in 
     do { 
      guard let dat = data else { throw JSONError.NoData } 
      guard let json = try NSJSONSerialization.JSONObjectWithData(dat, options:.AllowFragments) as? NSArray else { throw JSONError.ConversionFailed } 
      print(json) 
     } catch let error as JSONError { 
      print(error.rawValue) 
     } catch { 
      print(error) 
     } 
     }.resume() 
} 

を解析するために、次のコードを使用するが、私は

エラードメイン= NSCocoaErrorDomainコード= 3840「の文字は0の周りに無効な値が」次のエラーを取得しますUserInfo = {NSDebugDescription =文字0の値が無効です。}

私は間違いがありますか?

+0

[ This](http://stackoverflow.com/a/25986715/730701)mi助けてください。 – Adam

+0

@Adam Objective-CのコードはAFNetworking – madhev

+1

を使用していると思いますが、問題はありません。エラーは同じで、理由も同じである可能性が最も高いです。これはシリアル化に関連しており、ネットワーキングとは関係ありません。また、 'NSJSONSerialization'は、他のすべての' Cocoa(Touch) 'フレームワークと同様に、Objective-Cとまったく同じようにSwiftでも動作します。 – Adam

答えて

-1

jsonアレイを確認しました。それは完璧だ。 問題がシリアル化される可能性のあるポイント。

  1. あなたは
  2. シリアル化コードの下に試してみてくださいPHPでデバッグモードを無効にしてください応じて、取得データに依存

サンプルコード:

do { 
     let json = try NSJSONSerialization.JSONObjectWithData(data, options: []) as! [String: AnyObject] 

     print(json) 

} catch let error as NSError { 
print("Failed to load: \(error.localizedDescription)") 
}