0

I have downloaded the json files in my App Folder and I want to read that files from my App Folder to print specific values.

Files are located like this : /var/mobile/Containers/Data/Application/63E66EE9-9A1B-4D4D-AEF6-F8C54D159ED0/Library/NoCloud/MyApp/MyFolder/DTS.json

This is what file contains: [{"value":0}] However the file contents are read and printed in console as I have mentioned below but when I read specific value it gives null

NSURL *libraryDirURL = [[NSFileManager.defaultManager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *urlDTSK = [libraryDirURL URLByAppendingPathComponent:@"NoCloud/MyApp/MyFolder/DTS.json"];
NSString *filePathDTS = [NSString stringWithContentsOfURL:urlDTSK encoding:NSUTF8StringEncoding error:nil];
NSLog(@"This is Dts PATH %@", filePathDTS);
NSData *dataDTS = [NSData dataWithContentsOfFile:filePathDTS];
NSLog(@"here is DTS data  %@", dataDTS); //this shows null
NSDictionary *jsonDTS = [NSJSONSerialization JSONObjectWithData:dataDTS options:kNilOptions error:nil];
NSLog(@"here is jason DTS %@", jsonDTS);
NSMutableArray *DTSvalue = [jsonDTS valueForKeyPath: @"Value"];
DTSValueIs = DTSvalue[0];
NSLog(@"here is DTS Value first%@", DTSvalue[0]);
NSLog(@"here is DTS value is%@", DTSValueIs);

This shows This is Dts contents [{"value":0}] 2018-06-11 17:04:40.940006+0500 Muslims 365[3356:819935] here is DTS data (null)

Max
  • 3
  • 3

3 Answers3

1

So libraryDirURL is a path to library. Then urlDTSK is a path to specific file in in the library. Then filePathDTS is CONTENTS of that file in library as UTF8 string...

But dataDTS is contents of file at location written in file at filePathDTS. I believe the code should be:

NSData *dataDTS = [NSData dataWithContentsOfFile: urlDTSK.path];
Matic Oblak
  • 16,318
  • 3
  • 24
  • 43
  • `urlDTSK.path` is not evaluating that `.path` what is .path? – Max Jun 11 '18 at 10:23
  • @Max It should give you the path to the file. In your case a file path. You could also change constructor and use `NSURL` directly by writing `[NSData dataWithContentsOfURL:urlDTSK]`. So basically `.path` was there just to convert `NSURL` to `NSString`. – Matic Oblak Jun 11 '18 at 10:26
1

The error occurs because you are getting an NSString from a file URL and then you are getting NSData from that string as file path which cannot work. Omit that step:

NSURL *libraryDirURL = [[NSFileManager.defaultManager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *urlDTSK = [libraryDirURL URLByAppendingPathComponent:@"NoCloud/MyApp/MyFolder/DTS.json"];
NSData *dataDTS = [NSData dataWithContentsOfURL: urlDTSK];

By the way the retrieved JSON is an array, you get the value for key Value from the first element which seems to be a numeric value (NSNumber).

And handle the error!

NSError *error;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:dataDTS options:kNilOptions error:&error];
if (error) { NSLog(@"%@", error); }
NSNumber *dtsValue = jsonArray[0][@"value"];
NSLog(@"here is DTS value: %@", dtsValue); // here is DTS value: 0
vadian
  • 274,689
  • 30
  • 353
  • 361
  • still null no value I am getting – Max Jun 11 '18 at 10:16
  • Check the file URL. Is the file really in a subfolder of the **Library** folder? – vadian Jun 11 '18 at 10:17
  • yes its `/var/mobile/Containers/Data/Application/63E66EE9-9A1B-4D4D-AEF6-F8C54D159ED0/Library/NoCloud/MyApp/MyFolder/DTS.json` – Max Jun 11 '18 at 10:19
  • `dataDTS` is `null` or `dtsValue` is `null`? I edited the answer because the key `value` is lowercased. – vadian Jun 11 '18 at 10:23
  • tried you updated solution but still its null dtsValue is that I am not defining main bundle path?? – Max Jun 11 '18 at 10:29
  • The main bundle and the library folder are two completely different locations. – vadian Jun 11 '18 at 10:32
  • ok got it thank, but any solution how ca I get values of my json file? – Max Jun 11 '18 at 10:47
  • Once again, does `dataDTS` contain something? If yes please add the data dump to your question. – vadian Jun 11 '18 at 10:49
  • yes its prints `here is DTS data <5b7b2276 616c7565 223a307d 5d>` – Max Jun 11 '18 at 11:06
  • Good. The code in my answer is supposed to work. I added the missing `@` characters in front of the literal strings and tested the code. It prints *here is DTS value: 0* – vadian Jun 11 '18 at 11:17
  • I added too early in warnings – Max Jun 11 '18 at 11:32
  • `here is DTS data < what are these numbers inside >`. yes its DTSdata prints here is DTS data`<5b7b2276 616c7565 223a307d 5d>` – Max Jun 11 '18 at 11:32
  • in JSON all it keys acts as strings despite if what it contains like `"12go%23"` this key is a string value in json I think?, but I am getting block of random values instead of a string in json why? like `DTSdata prints here is DTS data<5b7b2276 616c7565 223a307d 5d>` would you help plz? – Max Jun 12 '18 at 06:09
  • `<5b7b2276 616c7565 223a307d 5d>` is clearly `[{"value":0}]`. I have no idea where the random value comes from. – vadian Jun 12 '18 at 06:39
0

In your code, you must allocate

NSMutableArray *DTSvalue = [NSMutableArray alloc]init];

before use.

Vinu Jacob
  • 393
  • 2
  • 15