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

时间:2022-10-08 08:57:21

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"}

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 ran[10974:4371080] Response ==> {"success":1, "session_id":"778a6a66505b785e1130c46227d44641"}
2016-05-03 14:15:06.826 ran[10974:4371080] Success: 1
2016-05-03 14:15:06.826 ran[10974:4371080] sessionId: 140407621032752

2016-05-03 14:15:06.826 run [10974:4371080]回复号:200 2016-05-03 14:15:06.826 ran [10974:4371080]回复==> {“成功”:1,“session_id” :“778a6a66505b785e1130c46227d44641”} 2016-05-03 14:15:06.826跑[10974:4371080]成功:1 2016-05-03 14:15:06.826跑[10974:4371080] sessionId:140407621032752

As you can see session_id is coming in correct. But how can I get it?

正如您所看到的,session_id正确无误。但我怎么能得到它?

1 个解决方案

#1


1  

Your session id is a String, so you have to use the string formatter %@ for NSLog:

您的会话ID是一个String,因此您必须使用字符串formatter%@作为NSLog:

NSLog("sessionId: %@", sessionId)

#1


1  

Your session id is a String, so you have to use the string formatter %@ for NSLog:

您的会话ID是一个String,因此您必须使用字符串formatter%@作为NSLog:

NSLog("sessionId: %@", sessionId)