php json_encode&换行,快速JSONSerialization导致奇怪的输出

时间:2014-09-12 22:54:57

标签: php ios json web-services swift

PHP JSON和Swift JSON的一些问题。请帮我弄清楚什么是额外的空间,\ n和其他东西。为什么输出不是一个干净的连续字符串。我不需要打印或任何东西。

PHP:

$sql = "SELECT * FROM entries";
$stmt = $db->prepare($sql);
$stmt->execute();
$entries = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo '{"root":'.json_encode($entries).'}';

夫特:

    var request : NSMutableURLRequest = NSMutableURLRequest()
    request.URL = NSURL(string: url)
    request.HTTPMethod = "GET"

    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler:{ (response:NSURLResponse!, data: NSData!, error: NSError!) -> Void in

        var error: NSError?
        let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.AllowFragments, error: &error) as? NSDictionary

        if (jsonResult != nil) {
            println(jsonResult?.description )
        } else {
            println(jsonResult?.description)
        }

控制台输出:

Optional("{\n    root =     (\n                {\n            id = 1;\n            text = \"Test Entry\";\n        },\n                {\n            id = 2;\n            text = \"Test Entry 2\";\n        }\n    );\n}")

数据库:

enter image description here

1 个答案:

答案 0 :(得分:0)

要么这样做:

println(jsonResult!.description)

或者这个:

println(NSString(data:data, encoding:NSUTF8StringEncoding))