0

I know My question may be lengthy, but I want to share my experience please help me out with an explaination. I am getting a huge Json response from server, and I am storing it into a Dictionary.when I am printing the Dictionary it is showing (null).Can anybody please explain why it is happening like that?

After a little research I am saving the huge Json response to a file and I am able to read the Json from that file.Firstly it worked fine but now I am getting this error

Error Domain=NSCocoaErrorDomain Code=3840 "Unable to convert data to string around character 1070." UserInfo={NSDebugDescription=Unable to convert data to string around character 1070.}

This is how I am writing json to a file

-(void)writeJsonDatatoFile:(NSData*)jsonData
{
NSError *errorWriting;
    NSString *dest=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true)[0] stringByAppendingPathComponent:@"file.json"];
  [jsonData writeToFile:dest atomically:YES];
}

This is how I am reading json from file
-(void*)readJsonDataFromFile
{
    NSString *source=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true)[0] stringByAppendingPathComponent:@"file.json"];
    NSError *error;
    NSError *error1;
    NSData *data=[NSData dataWithContentsOfFile:source options:kNilOptions error:&error1];
    NSLog(@"Reading Error--%@",error1);
    NSDictionary *dataDict=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    NSLog(@"json Data From File--%@-%@",dataDict,error);
}
Smart guy
  • 494
  • 6
  • 9
  • Before `writeJsonDatatoFile`, do you check that your JSON is valid? Also is your data "`NSUTF8StringEncoding` compatible"? For instance, this answer could be usefull if not: http://stackoverflow.com/a/35338296/1801544 – Larme Mar 10 '17 at 09:24
  • Hi @Larme thanks for the reply. Yes that is the valid json. Iam getting response in post client. After writing the json data to file, iam checking the json file in vi editor and the data is present there. – Smart guy Mar 11 '17 at 00:13

0 Answers0