简体   繁体   English

无法从 JSON 响应中获取正确的字符串?

[英]Can't get the correct string from a JSON response?

Why can't I get the correct string from a JSON response?为什么我无法从 JSON 响应中获得正确的字符串?

From the app I do a PHP call and get following JSON data back:我从应用程序调用 PHP 并获取以下 JSON 数据:

{"success":1, "session_id":"778a6a66505b785e1130c46227d44641"} {“成功”:1,“session_id”:“778a6a66505b785e1130c46227d44641”}

But when I read it in the app the session_id is 140407621032752. Why?但是当我在应用程序中读取它时session_id是 140407621032752。为什么?

Here is a little bit of my code:这是我的一些代码:

let responseData:NSString  = NSString(data:urlData!, encoding:NSUTF8StringEncoding)!

NSLog("Response ==> %@", responseData);

let jsonData:NSDictionary = try NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers ) as! NSDictionary
                        let success:NSInteger = jsonData.valueForKey("success") as! NSInteger
let sessionId:NSString = jsonData.valueForKey("session_id") as! NSString

NSLog("Success: %ld", success)
NSLog("sessionId: %ld", sessionId)

And here is output when running it:这是运行时的输出:

2016-05-03 14:15:06.826 ran[10974:4371080] Response code: 200 2016-05-03 14:15:06.826 跑 [10974:4371080] 响应代码:200
2016-05-03 14:15:06.826 ran[10974:4371080] Response ==> {"success":1, "session_id":"778a6a66505b785e1130c46227d44641"} 2016-05-03 14:15:06.826 跑 [10974:4371080] 响应 ==> {"success":1,"session_id":"778a6a66505b785e1130c46227d44641"}
2016-05-03 14:15:06.826 ran[10974:4371080] Success: 1 2016-05-03 14:15:06.826 跑 [10974:4371080] 成功:1
2016-05-03 14:15:06.826 ran[10974:4371080] sessionId: 140407621032752 2016-05-03 14:15:06.826 跑 [10974:4371080] sessionId: 140407621032752

As you can see session_id is coming in correct.正如你所看到的session_id是正确的。 But how can I get it?但我怎样才能得到它?

您的会话 ID 是一个字符串,因此您必须使用字符串格式化程序%@来处理 NSLog:

NSLog("sessionId: %@", sessionId)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM