无法读取 objective c 中 json 文件的特定值
Can not read the specific values of json file in objective c
我已经在我的应用程序文件夹中下载了 json 个文件,我想从我的应用程序文件夹中读取这些文件以打印特定值。
文件位置如下:/var/mobile/Containers/Data/Application/63E66EE9-9A1B-4D4D-AEF6-F8C54D159ED0/Library/NoCloud/MyApp/MyFolder/DTS.json
这是文件包含的内容:[{"value":0}]
然而,文件内容是在控制台中读取和打印的,正如我在下面提到的,但是当我读取特定值时,它给出了 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 is Dts contents [{"value":0}]
2018-06-11 17:04:40.940006+0500 Muslims 365[3356:819935] here is DTS data (null)
所以 libraryDirURL
是库的路径。然后 urlDTSK
是库中特定文件的路径。然后 filePathDTS
是库中该文件的内容作为 UTF8 字符串...
但是 dataDTS
是 filePathDTS
文件中写入位置的文件内容。我相信代码应该是:
NSData *dataDTS = [NSData dataWithContentsOfFile: urlDTSK.path];
发生错误是因为您从文件 URL 中获取 NSString
,然后您从 中获取 NSData
该字符串 作为无法工作的文件路径。省略那一步:
NSURL *libraryDirURL = [[NSFileManager.defaultManager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *urlDTSK = [libraryDirURL URLByAppendingPathComponent:@"NoCloud/MyApp/MyFolder/DTS.json"];
NSData *dataDTS = [NSData dataWithContentsOfURL: urlDTSK];
顺便说一下,检索到的 JSON 是一个数组,您可以从第一个似乎是数字值 (NSNumber
) 的元素中获取键 Value
的值。
并处理错误!
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
在你的代码中,你必须分配
NSMutableArray *DTSvalue = [NSMutableArray alloc]init];
使用前。
我已经在我的应用程序文件夹中下载了 json 个文件,我想从我的应用程序文件夹中读取这些文件以打印特定值。
文件位置如下:/var/mobile/Containers/Data/Application/63E66EE9-9A1B-4D4D-AEF6-F8C54D159ED0/Library/NoCloud/MyApp/MyFolder/DTS.json
这是文件包含的内容:[{"value":0}] 然而,文件内容是在控制台中读取和打印的,正如我在下面提到的,但是当我读取特定值时,它给出了 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 is Dts contents [{"value":0}]
2018-06-11 17:04:40.940006+0500 Muslims 365[3356:819935] here is DTS data (null)
所以 libraryDirURL
是库的路径。然后 urlDTSK
是库中特定文件的路径。然后 filePathDTS
是库中该文件的内容作为 UTF8 字符串...
但是 dataDTS
是 filePathDTS
文件中写入位置的文件内容。我相信代码应该是:
NSData *dataDTS = [NSData dataWithContentsOfFile: urlDTSK.path];
发生错误是因为您从文件 URL 中获取 NSString
,然后您从 中获取 NSData
该字符串 作为无法工作的文件路径。省略那一步:
NSURL *libraryDirURL = [[NSFileManager.defaultManager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *urlDTSK = [libraryDirURL URLByAppendingPathComponent:@"NoCloud/MyApp/MyFolder/DTS.json"];
NSData *dataDTS = [NSData dataWithContentsOfURL: urlDTSK];
顺便说一下,检索到的 JSON 是一个数组,您可以从第一个似乎是数字值 (NSNumber
) 的元素中获取键 Value
的值。
并处理错误!
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
在你的代码中,你必须分配
NSMutableArray *DTSvalue = [NSMutableArray alloc]init];
使用前。