0

I am getting one of my web-service response in JSON. Here is the UTF8String base responseString:

"{\"ApplicationId\":1,\"CurrentPosition\":0,\"EndOfFile\":false,\"Media\":{\"Active\":true,\"Description\":\"This video demonstrates a quick overview of Bentley LEARN Server.\",\"LocationId\":1,\"MP4URL\":\"http:\\/\\/cdnllnw.bentley.com\\/s\\/LearnVideo\\/Welcome.mp4?s=1380034262&e=1380077462&h=c2641922fd862ffe5c5a05e94786359a\",\"MediaId\":5003235,\"MediaURL\":\"http:\\/\\/cdnllnw.bentley.com\\/s\\/LearnVideo\\/Welcome.mp4?s=1380034262&e=1380077462&h=c2641922fd862ffe5c5a05e94786359a\",\"StreamServer\":null,\"Title\":\"Welcome\",\"TransferStatus\":0,\"filesize\":9094429},\"MediaDuration\":0,\"SkippedCurrentPosition\":0,\"UserBP\":8011512,\"ViewedTime\":0}"

After formatting it looks like:

{
 "ApplicationId":1,
 "CurrentPosition":0,
 "EndOfFile":false,
 "Media":
  {
   "Active":true,
   "Description":"This video demonstrates a quick overview of Bentley LEARN Server.",
   "LocationId":1,
   "MP4URL":"http:\/\/cdnllnw.bentley.com\/s\/LearnVideo\/Welcome.mp4?s=1380035311&e=1380078511&h=d8de8ade046266fb7324be90a575f978",
   "MediaId":5003235,
   "MediaURL":"http:\/\/cdnllnw.bentley.com\/s\/LearnVideo\/Welcome.mp4?s=1380035311&e=1380078511&h=d8de8ade046266fb7324be90a575f978",
   "StreamServer":null,
   "Title":"Welcome",
   "TransferStatus":0,
   "filesize":9094429
  },
 "MediaDuration":0,
 "SkippedCurrentPosition":0,
 "UserBP":8011512,
 "ViewedTime":0
 }

How can I convert this string into JSON object or NSDictionary object so that I can access all key-values easily.

I have tried this:

NSString *responseString = [NSString stringWithUTF8String:response.c_str()];


NSDictionary *JSONdict =
[NSJSONSerialization JSONObjectWithData: [responseString dataUsingEncoding:NSUTF8StringEncoding]
                                options: NSJSONReadingMutableContainers
                                  error: &error];

But it says JSONdict is nil and error description is:

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0xd8bbd30 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

Please help

Mrunal
  • 13,982
  • 6
  • 52
  • 96
  • So what error says ?... – Grzegorz Krukowski Sep 24 '13 at 14:57
  • possible duplicate of [Convert JSON feed to NSDictionary](http://stackoverflow.com/questions/5038371/convert-json-feed-to-nsdictionary) – Rob Sep 24 '13 at 14:58
  • @GrzegorzKrukowski Added error description – Mrunal Sep 24 '13 at 15:00
  • @Rob: I am using the same method still it is throwing me error. I know that method which I am calling is correct, but why its throwing an error. – Mrunal Sep 24 '13 at 15:02
  • It looks okey. Where this NSString is coming from ? Can you paste a bit more code where you create this NSString? – Grzegorz Krukowski Sep 24 '13 at 15:04
  • If you are reading from a file - maybe there are some more headers in a file - or if from web - maybe some extra characters ? – Grzegorz Krukowski Sep 24 '13 at 15:05
  • This is a response string coming from web-service. And web-services are getting called using CURL library using C++ code which returns me UTF8String. – Mrunal Sep 24 '13 at 15:12
  • @Mrunal is this string you pasted coming from breakpoint in iOS from reponse from this service ? Please NSLog a value that you get from it. – Grzegorz Krukowski Sep 24 '13 at 15:20
  • It would have made more sense to put the data directly into an NSData object and skip the conversion to/from NSString. But the other way should have worked, so there must be some "garbage" in the string that we're not seeing. – Hot Licks Sep 24 '13 at 15:59

2 Answers2

1

Adding NSJSONReadingAllowFragments as option should help according to error:

NSString* jsonString = @"{\"ApplicationId\":1,\"CurrentPosition\":0,\"EndOfFile\":false,\"Media\":{\"Active\":true,\"Description\":\"This video demonstrates a quick overview of Bentley LEARN Server.\",\"LocationId\":1,\"MP4URL\":\"http:\\/\\/cdnllnw.bentley.com\\/s\\/LearnVideo\\/Welcome.mp4?s=1380034262&e=1380077462&h=c2641922fd862ffe5c5a05e94786359a\",\"MediaId\":5003235,\"MediaURL\":\"http:\\/\\/cdnllnw.bentley.com\\/s\\/LearnVideo\\/Welcome.mp4?s=1380034262&e=1380077462&h=c2641922fd862ffe5c5a05e94786359a\",\"StreamServer\":null,\"Title\":\"Welcome\",\"TransferStatus\":0,\"filesize\":9094429},\"MediaDuration\":0,\"SkippedCurrentPosition\":0,\"UserBP\":8011512,\"ViewedTime\":0}";

NSError* error = nil;
NSDictionary *JSONdict =
[NSJSONSerialization JSONObjectWithData: [responseString dataUsingEncoding:NSUTF8StringEncoding]
                            options: NSJSONReadingMutableContainers | NSJSONReadingAllowFragments
                              error: &error];

But that means the NSString you pasted in your question contains extra characters which are not following JSON format. If you add more code from where you take this value (file, web) - I can help more to make it valid. Until then treat this answer as workaround :)

The properly formatted string should be:

{\"ApplicationId\":1,\"CurrentPosition\":0,\"EndOfFile\":false,\"Media\":{\"Active\":true,\"Description\":\"This video demonstrates a quick overview of Bentley LEARN Server.\",\"LocationId\":1,\"MP4URL\":\"http:\\/\\/cdnllnw.bentley.com\\/s\\/LearnVideo\\/Welcome.mp4?s=1380034262&e=1380077462&h=c2641922fd862ffe5c5a05e94786359a\",\"MediaId\":5003235,\"MediaURL\":\"http:\\/\\/cdnllnw.bentley.com\\/s\\/LearnVideo\\/Welcome.mp4?s=1380034262&e=1380077462&h=c2641922fd862ffe5c5a05e94786359a\",\"StreamServer\":null,\"Title\":\"Welcome\",\"TransferStatus\":0,\"filesize\":9094429},\"MediaDuration\":0,\"SkippedCurrentPosition\":0,\"UserBP\":8011512,\"ViewedTime\":0}

Without " at the beggining and at the end. Maybe that's the reason. If you are loading this from web - make sure you don't have any JS included or extra headers and confirm that your web service responds with JSON format in headers.

Grzegorz Krukowski
  • 18,081
  • 5
  • 50
  • 71
  • I have already tried that option, but when I tried to use NSLog(@"%@", [JSONdict objectForKey:@"Media"]); it says -[__NSCFString objectForKey:]: unrecognized selector sent to instance 0xc69e3e0 – Mrunal Sep 24 '13 at 15:14
  • Hm this means your JSONdict is NSString not a NSDictonary - please answer my question from where this NSString comes from and paste more code around that. – Grzegorz Krukowski Sep 24 '13 at 15:18
  • I added NSString definition to code in my answer - please copy paste and see it's working - you have a problem somewhere else - not in serialization – Grzegorz Krukowski Sep 24 '13 at 15:19
1

I've try your NSString with the next piece of code (it's a starting point) to output the dictionary from "Media";

NSString *string = @"{\"ApplicationId\":1,\"CurrentPosition\":0,\"EndOfFile\":false,\"Media\":{\"Active\":true,\"Description\":\"This video demonstrates a quick overview of Bentley LEARN Server.\",\"LocationId\":1,\"MP4URL\":\"http:\\/\\/cdnllnw.bentley.com\\/s\\/LearnVideo\\/Welcome.mp4?s=1380034262&e=1380077462&h=c2641922fd862ffe5c5a05e94786359a\",\"MediaId\":5003235,\"MediaURL\":\"http:\\/\\/cdnllnw.bentley.com\\/s\\/LearnVideo\\/Welcome.mp4?s=1380034262&e=1380077462&h=c2641922fd862ffe5c5a05e94786359a\",\"StreamServer\":null,\"Title\":\"Welcome\",\"TransferStatus\":0,\"filesize\":9094429},\"MediaDuration\":0,\"SkippedCurrentPosition\":0,\"UserBP\":8011512,\"ViewedTime\":0}";

NSString *newString = [string stringByReplacingOccurrencesOfString:@"\\" withString:@""];

NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[newString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];

NSLog(@"%@", [dictionary valueForKey:@"Media"]);

And the result is:

{
Active = 1;
Description = "This video demonstrates a quick overview of Bentley LEARN Server.";
LocationId = 1;
MP4URL = "http://cdnllnw.bentley.com/s/LearnVideo/Welcome.mp4?s=1380034262&e=1380077462&h=c2641922fd862ffe5c5a05e94786359a";
MediaId = 5003235;
MediaURL = "http://cdnllnw.bentley.com/s/LearnVideo/Welcome.mp4?s=1380034262&e=1380077462&h=c2641922fd862ffe5c5a05e94786359a";
StreamServer = "<null>";
Title = Welcome;
TransferStatus = 0;
filesize = 9094429;
}

Hope it will be useful for you.

RFG
  • 2,880
  • 3
  • 28
  • 44