1

I have a json string (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\":\"\"}]}\"}"

It contains a json object having 2 fields: MsgType and Msg. Msg further containes a serialized json object in form of string.

I need to read the value of MsgType as number and Msg as a json object.

I have tried couple of things:

Firstly:

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

Source: https://github.com/SwiftyJSON/SwiftyJSON#initialization

Output:

swiftyjson:null

Secondly:

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

Output:

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

6
  • Why is your code intended so strangely? The least you can do is format your question properly so that it is readable. Commented Nov 10, 2015 at 17:35
  • print error rather than error.localizedDescription this gives you the location where the text is improperly formatted. Commented Nov 10, 2015 at 17:37
  • PS: as far as I can see there are a lot of superfluous double quotes in the text Commented Nov 10, 2015 at 17:45
  • @vadian: here is the error: Error Domain=NSCocoaErrorDomain Code=3840 "Badly formed object around character 24." UserInfo={NSDebugDescription=Badly formed object around character 24.} Commented Nov 10, 2015 at 17:45
  • 1
    The answer from @eric-d is the correct answer. By the way you can check this by yourself next time with a JSON validator like jsonlint. And be sure to use print(text2) to log your JSON properly. Commented Nov 10, 2015 at 18:26

1 Answer 1

1

Your JSON string is not properly escaped.

  • The "Msg" opening dictionary { shouldn't be preceded by a double quote, it should be like this:

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

  • Same error at the end, it should be:

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.