json - 无法将字符串转换为 Json 对象

标签 json swift swift2 swifty-json

我有一个 json 字符串(text2):

"{\"MsgType\":103,\"Msg\":\"{\"UserObject\":{\"SecretId\":\"dsofgihsdaoifhad=\",\"FirstName\":\"ASDGF\",\"LastName\":\"hdsa\",\"IsFemale\":0,\"PhoneOffice\":\"\",\"ISDCode\":\"\",\"PhonePersonal\":\"91923426989\",\"Designation\":\"\",\"Company\":\"\",\"TagLine\":\"helping mplemented\",\"FbId\":\"\",\"FbURL\":\"\",\"FbToken\":\"\",\"GplusId\":\"\",\"GplusURL\":\"\",\"GplusToken\":\"\",\"LinkedinId\":\"\",\"LinkedinURL\":\"\",\"LinkedinToken\":\"\",\"Status\":\"\",\"Email\":\"\",\"DisplayPicture\":\"\",\"IsPrivatePhoneOffice\":0,\"IsPrivatePhonePersonal\":1,\"IsPrivateEmail\":0,\"DeviceType\":\"android\",\"NotificationRegistrationID\":\"APA9HZ_RmEy7gfbQtN-QBxXr7dafG394oT9Dg1HpAv7OaWbUsMOsfpMI1a_7Qa2aNkqBOWB3M29djtsRW0fWl4oZSG0bwVv1zEPDBAseZvv1eHfqVj_JUI8tZixX\",\"Location\":\"\",\"Latitude\":18.69943,\"Longitude\":77.12576,\"IsPrivate\":0},\"ConnectionStatus\":3,\"ConnectionType\":1,\"PreviousMeetings\":[{\"SecretId\":\"sadfosdfjasdf=\",\"FriendSecretId\":\"dsofgihsdaoifhad=\",\"MeetingTime\":1447088420440,\"Status\":\"\",\"MeetingNotes\":\"\",\"Location\":\"\",\"Latitude\":28.6994,\"Longitude\":77.1258,\"ContactType\":\"\"},{\"SecretId\":\"sadfosdfjasdf=\",\"FriendSecretId\":\"dsofgihsdaoifhad=\",\"MeetingTime\":1447088335275,\"Status\":\"\",\"MeetingNotes\":\"\",\"Location\":\"\",\"Latitude\":28.6994,\"Longitude\":77.1258,\"ContactType\":\"\"},{\"SecretId\":\"sadfosdfjasdf=\",\"FriendSecretId\":\"dsofgihsdaoifhad=\",\"MeetingTime\":1447088229120,\"Status\":\"\",\"MeetingNotes\":\"\",\"Location\":\"\",\"Latitude\":28.6994,\"Longitude\":77.1258,\"ContactType\":\"\"},{\"SecretId\":\"sadfosdfjasdf=\",\"FriendSecretId\":\"dsofgihsdaoifhad=\",\"MeetingTime\":1447088014838,\"Status\":\"\",\"MeetingNotes\":\"\",\"Location\":\"\",\"Latitude\":28.6994,\"Longitude\":77.1258,\"ContactType\":\"\"},{\"SecretId\":\"sadfosdfjasdf=\",\"FriendSecretId\":\"dsofgihsdaoifhad=\",\"MeetingTime\":1444547028931,\"Status\":\"\",\"MeetingNotes\":\"\",\"Location\":\"\",\"Latitude\":28.6994,\"Longitude\":77.1258,\"ContactType\":\"\"}]}\"}"

它包含一个具有 2 个字段的 json 对象:MsgTypeMsgMsg 还包含一个字符串形式的序列化 json 对象。

我需要将 MsgType 的值读取为数字,将 Msg 的值读取为 json 对象。

我尝试了一些事情:

首先:

  if let dataFromString = text2.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) {
                                let json = JSON(data: dataFromString)
                                print("swiftyjson:\(json)")
                            }

来源:https://github.com/SwiftyJSON/SwiftyJSON#initialization

输出:

swiftyjson:null

其次:

let data = text2.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!

                            do {
                                let jsonSys = try NSJSONSerialization.JSONObjectWithData(data, options: [])
                                print("jsonSys:\(jsonSys)")
                            } catch let error as NSError {
                                print("Failed to load: \(error.localizedDescription)")
                            }

输出:

Failed to load: The data couldn’t be read because it isn’t in the correct format.

最佳答案

您的 JSON 字符串未正确转义。

  • "Msg"开头的字典 { 前面不应该有双引号,应该是这样的:

"{\"MsgType\":103,\"Msg\":{\"UserObject\" ...

  • 最后还是一样的错误,应该是:

... 77.1258,\"ContactType\":\"\"}]}}"

关于json - 无法将字符串转换为 Json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33636013/

相关文章:

javascript - 获取具有定义类型的 JSON 对象长度

ios - UICollection 崩溃无效更新 : invalid number of sections.

swift - 如何使用 NSURLComponents 将 + 编码为 %2B

ios - Swift Array of Arrays 终止了 Xcode 索引,因此 type-ahead 不起作用

swift - 在 Swift 中向数组中插入一个新元素

Swift 将 unsafemutablepointer<MyStruct> 快速转换为 C void 返回指针

php - 从 php 传递的 json 数组获取键 => 值

javascript - 使用 Node.js 将 json 文件写入 Firebase?

json - NodeJS JSON 文件读取时不带换行符

ios - 如何在不进行身份验证的情况下将核心数据持久化到云端