无法将字符串转换为 Json 对象
Can't convert string into Json object
我有一个 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 对象:MsgType
和 Msg
。 Msg
还包含一个字符串形式的序列化 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 字符串 (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 对象:MsgType
和 Msg
。 Msg
还包含一个字符串形式的序列化 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\":\"\"}]}}"