【问题标题】:Can not read the specific values of json file in objective c无法读取目标c中json文件的具体值
【发布时间】:2018-11-20 13:10:23
【问题描述】:

我已将 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)

【问题讨论】:

    标签: ios objective-c filepath jsonreader


    【解决方案1】:

    所以libraryDirURL 是库的路径。然后urlDTSK 是库中特定文件的路径。然后filePathDTS 是库中该文件的内容为UTF8 字符串...

    但dataDTS 是filePathDTS 文件中写入位置的文件内容。我相信代码应该是:

    NSData *dataDTS = [NSData dataWithContentsOfFile: urlDTSK.path];
    

    【讨论】:

    • urlDTSK.path 没有评估 .path 什么是 .path?
    • @Max 它应该给你文件的路径。在您的情况下,文件路径。您还可以通过编写[NSData dataWithContentsOfURL:urlDTSK] 来更改构造函数并直接使用NSURL。所以基本上.path 只是为了将NSURL 转换为NSString。
    【解决方案2】:

    发生错误是因为您从文件 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 是一个数组,您可以从第一个元素中获取键 Value 的值,该元素似乎是一个数值 (NSNumber)。

    并处理错误!

    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
    

    【讨论】:

    • 仍然为空,没有我得到的价值
    • 检查文件 URL。该文件是否真的在 Library 文件夹的子文件夹中?
    • 是的,它的/var/mobile/Containers/Data/Application/63E66EE9-9A1B-4D4D-AEF6-F8C54D159ED0/Library/NoCloud/MyApp/MyFolder/DTS.json
    • dataDTS 是 null 还是 dtsValue 是 null?我编辑了答案,因为键 value 是小写的。
    • 试过你更新的解决方案,但它的空 dtsValue 是我没有定义主包路径??
    【解决方案3】:

    在你的代码中,你必须分配

    NSMutableArray *DTSvalue = [NSMutableArray alloc]init];

    使用前。

    【讨论】:

    • 不,你不能。但是valueForKeyPath 的结果甚至不是一个可变数组。
    • 结果是一个字典数组。从他的示例代码中它不会得到任何值,它显示为 NULL。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-22
    • 2011-06-29
    • 1970-01-01
    • 2023-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多