-1

I am having a real hard time converting Json String to NSArray. This is my code

NSError *jsonError;
NSData *objectData = [[response objectForKey:@"result"] dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
                                                         options:NSJSONReadingMutableContainers
                                                           error:&jsonError];

NSLog(@"json = %@",json);

NSString *jsonDataString = [json objectForKey:@"data"];
NSLog(@"jsonDataArray = %@",jsonDataString);

NSData* cityData = [jsonDataString dataUsingEncoding:NSUTF8StringEncoding];

NSLog(@"%@",cityData);

NSError *errorMSg;
NSMutableArray *cityArray = [[NSMutableArray alloc] init];
NSMutableArray *testFeeds = [NSJSONSerialization JSONObjectWithData: cityData options:NSJSONReadingMutableContainers error:&errorMSg];
[cityArray addObjectsFromArray:testFeeds];

NSLog(@"%@",cityArray);
NSLog(@"%@",errorMSg);
NSLog(@"%d", [cityArray count]);

This is my Debuuged Response. I know this question has been asked too many time but none of the solution is working for me. The json response comes under single quotes not redable in iOS

json = { data = "[{'Amount': '5000', 'Description' : 'test', 'Transact at': 'Tue Dec 22 2015 18:48:14 GMT+0530 (IST)', 'Transaction mode': 'Cash'},{'Amount': '100', 'Description' : 'test', 'Transact at': 'Wed Dec 30 2015 11:15:12 GMT+0530 (IST)', 'Transaction mode': 'Cheque'}]"; status = Success; }

2015-12-30 17:44:56.890 Quickli[5528:172337] jsonDataArray = [{'Amount': '5000', 'Description' : 'test', 'Transact at': 'Tue Dec 22 2015 18:48:14 GMT+0530 (IST)', 'Transaction mode': 'Cash'},{'Amount': '100', 'Description' : 'test', 'Transact at': 'Wed Dec 30 2015 11:15:12 GMT+0530 (IST)', 'Transaction mode': 'Cheque'}]

2015-12-30 17:44:56.891 Quickli[5528:172337] <5b7b2741 6d6f756e 74273a20 27353030 30272c20 27446573 63726970 74696f6e 27203a20 27746573 74272c20 27547261 6e736163 74206174 273a2027 54756520 44656320 32322032 30313520 31383a34 383a3134 20474d54 2b303533 30202849 53542927 2c202754 72616e73 61637469 6f6e206d 6f646527 3a202743 61736827 7d2c7b27 416d6f75 6e74273a 20273130 30272c20 27446573 63726970 74696f6e 27203a20 27746573 74272c20 27547261 6e736163 74206174 273a2027 57656420 44656320 33302032 30313520 31313a31 353a3132 20474d54 2b303533 30202849 53542927 2c202754 72616e73 61637469 6f6e206d 6f646527 3a202743 68657175 65277d5d>

2015-12-30 17:48:10.166 Quickli[5528:172337] ( )

2015-12-30 17:48:10.935 Quickli[5528:172337] Error Domain=NSCocoaErrorDomain Code=3840 "No string key for value in object around character 2." UserInfo={NSDebugDescription=No string key for value in object around character 2.}

2015-12-30 17:48:12.176 Quickli[5528:172337] 0

Umesh Sharma
  • 388
  • 5
  • 23

2 Answers2

2

The error message reveals the problem:

"No string key for value in object around character 2." UserInfo={NSDebugDescription=No string key for value in object around character 2.}

That means that the JSON has the wrong format.
You have to replace the single quotes with double quotes.

Insert one line before the NSData* cityData = [… line.

jsonDataString = [jsonDataString stringByReplacingOccurrencesOfString:@"\'" withString:@"\""];
vadian
  • 274,689
  • 30
  • 353
  • 361
1

If you have an NSString called jsonDataString then you can do something like this...

NSString *jsonDataString = [json objectForKey:@"data"];
NSData *jsonData = [jsonDataString dataUsingEncoding:NSUTF8StringEncoding];
NSMutableArray * testFeeds =[NSMutableArray alloc]init];
NSArray *abc= [NSJSONSerialization JSONObjectWithData:jsonData options: NSJSONReadingMutableContainers error:nil];
[testFeeds addObject:abc];
NSLog(@"%@", testFeeds);