-3

I am trying to convert a JSON string into an object in Objective C using the code:

NSString *jsonString = (NSString *) responseObject;
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
id  json = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];

The value of jsonString is

"[{\"Date\": \"12-01-2015\", \"Time\": \"7:00 am\", \"Title\": \"First Car Service\", \"Details\": \"This was the first car service ever done\"}]"

But the value of json is always nil. How do I convert the jsonString into a NSArray ?

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Ashish Agarwal
  • 14,555
  • 31
  • 86
  • 125
  • 1
    what does the NSError object say?.... ah, nothing, as you pass in nil. – vikingosegundo Mar 17 '15 at 23:00
  • `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=0x7f81b3c3afe0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}` – Ashish Agarwal Mar 17 '15 at 23:06
  • I started using `NSJSONReadingAllowFragments` in options, but now json is a NSString – Ashish Agarwal Mar 17 '15 at 23:07
  • for me the given example looks correct and is an array. Maybe it is polluted with some white spaces? – vikingosegundo Mar 17 '15 at 23:10
  • that there are some non-printable char present. try trimming the initial string. – vikingosegundo Mar 17 '15 at 23:12
  • I removed all whitespaces. The value of json is now a `__NSCFString` with value `[{"Date":"12-01-2015","Time":"7:00 am","Title":"First Car Service","Details":"This was the first car service ever done"}]` – Ashish Agarwal Mar 17 '15 at 23:14
  • dont do this fragment option: you have a complete json file there. – vikingosegundo Mar 17 '15 at 23:15
  • As soon as I switch that to `options:0`, the error 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=0x7fee397b04c0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}` – Ashish Agarwal Mar 17 '15 at 23:17
  • are the leading and trailing `"` in the jsonString? – vikingosegundo Mar 17 '15 at 23:18
  • Works now. I removed the leading and trailing `"` and replaced the `\"` with `"` – Ashish Agarwal Mar 17 '15 at 23:35
  • If you want to write that as an answer, I will accept – Ashish Agarwal Mar 17 '15 at 23:35
  • where do u get the json from? it is definitely incorrect. – vikingosegundo Mar 17 '15 at 23:38

2 Answers2

1

My (third) bet: your json string isn't correct. If it contains the leading and trailing quotes strip them. Replaced the \" with ".

better: make the server or other json source send correct json.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
0

From the error, it may be that your string is not formatted correctly, probably due to the quotes.

You should also make sure that after you have formatted the string, then you check the array has objects.

NSLog(@"JSON Details: %@", json[0][@"Details"]);
WrightsCS
  • 50,551
  • 22
  • 134
  • 186
  • It gives a NSDictionary that is nil – Ashish Agarwal Mar 17 '15 at 23:09
  • I tried using NSJSONReadingAllowFragments, but that is returning a NSString – Ashish Agarwal Mar 17 '15 at 23:10
  • @WrightCS: nil stays nil, what ever type you gave it. and the given example should result in an array – vikingosegundo Mar 17 '15 at 23:11
  • If `json` is an `NSDictionary`, then why do you attempt to access index 0 in the `NSLog` statement? And setting the options for mutable containers has nothing to do with the issue. – rmaddy Mar 17 '15 at 23:18
  • Whright, with all your changes: nil is nil, no matter if it is typed NSDictionary, NSArray or id. – vikingosegundo Mar 17 '15 at 23:20
  • Changing `json` back to `id` doesn't affect anything. Based on your `NSLog`, `json` should be an `NSArray` (or `NSMutableArray` if you insist on the mutable containers option). But again, none of that has anything to do with the error. – rmaddy Mar 17 '15 at 23:21