gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:55:59 28 4
gpt4 key购买 nike

我有一个 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com