現在、swif2.0を使用してphpから受け取ったjsonデータをパースする
プログラムを書いております。
以下が該当プログラムの全体になりますが、シミュレーターで表示を
試みても表示がされていない状況でございます。
import UIKit class TopicView: UIViewController,NSURLSessionDelegate,NSURLSessionDataDelegate{ @IBOutlet weak var myTextView: UITextView! override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.lightGrayColor() self.view.addSubview(myTextView) // 通信用のConfigを生成. let myConfig:NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration() // Sessionを生成. let mySession:NSURLSession = NSURLSession(configuration: myConfig, delegate: self, delegateQueue: nil) // 通信先のURLを生成. let myUrl:NSURL = NSURL(string: "http://localhost/book/jsonDB.php")! // タスクの生成. let myTask:NSURLSessionDataTask = mySession.dataTaskWithURL(myUrl, completionHandler: { (data, response, err) -> Void in do{ // 受け取ったjsonデータを表示. dispatch_async(dispatch_get_main_queue(), { self.myTextView.text = NSString(data: data!, encoding: NSUTF8StringEncoding)! as String }) // 受け取ったJSONデータをパースする. let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary // バックグラウンドだとUIの処理が出来ないので、メインスレッドでUIの処理を行わせる. dispatch_async(dispatch_get_main_queue(), { // パースしたJSONデータへのアクセス. for value in json { self.myTextView.text = "\(self.myTextView.text)\n\(value.key) : \(value.value)" } }) }catch{} }) // タスクの実行. myTask.resume() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
以下の直前までは実行出来ているようなので、このパートのどこかに問題が
あるのだと思うのですが、エラーも表示されておらず、何が間違っているのか判りかねている状況でございます。
// バックグラウンドだとUIの処理が出来ないので、メインスレッドでUIの処理を行わせる. dispatch_async(dispatch_get_main_queue(), { // パースしたJSONデータへのアクセス. for value in json { self.myTextView.text = "\(self.myTextView.text)\n\(value.key) : \(value.value)" } })
ご参考として、php側のコードも以下に記載いたします。
<?php require("db.php"); $sql = "SELECT * FROM AA"; $result = $mysqli -> query($sql); header('Content-type: application/json'); while($data = $result -> fetch_array()) { //jsonデータ化する $senddata = array( 'name'=>$data['name'], 'age'=>21 ); $json = json_encode($senddata,JSON_UNESCAPED_UNICODE); var_dump($json); } } ?>
以上になります。
お力添えを頂けますと幸いです、よろしくお願いいたします。
<追記>
デバックコンソールには以下のようなメッセージが表示されておりました
JSON text did not start with array or object and option to allow fragments not set.}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/05/14 07:59