【问题标题】:Parsing JSON - Objective C解析 JSON - 目标 C
【发布时间】:2017-02-23 08:52:58
【问题描述】:

我正在尝试使用 Objective-C 解析 JSON 这是与我当前代码相呼应的 NSLog。

   {
    KnowledgeBaseEntry =         {
        AllowBotAccess = 1;
        FulltextSearch = 1;
        GroupId = "";
        Id = 611552aea1fe4d789e31133d3ee77f35;
        IsPublic = 1;
        Languages = "";
        OwnerId = 8c427d5;
        ParentId = 1;
        ShortcutWord = "";
        Tags = "";
        Title = "Another Test Cell";
        Type = 1;
        Value = "This <BR>Is<BR>testing";
    };
},
    {
    KnowledgeBaseEntry =         {
        AllowBotAccess = 1;
        FulltextSearch = 1;
        GroupId = "";
        Id = fc4f1a90243246bb93641b0c8db689b9;
        IsPublic = 1;
        Languages = "";
        OwnerId = 8c427d5;
        ParentId = 1;
        ShortcutWord = "";
        Tags = "";
        Title = "Cydo Error 2";
        Type = 1;
        Value = "content<BR><BR>this is contenty";
    };
},
    {
    KnowledgeBaseEntry =         {
        AllowBotAccess = 1;
        FulltextSearch = 1;
        GroupId = "";
        Id = bd057d5443194d7a98c2398e07de919e;
        IsPublic = 1;
        Languages = "";
        OwnerId = 8c427d5;
        ParentId = 1;
        ShortcutWord = "";
        Tags = "";
        Title = testkb2;
        Type = 1;
        Value = "test content!";
    };
}

)

我需要使用 Title 来获取 Value 中的内容,以便在应用中显示。我可以访问 Title Var 但我不知道如何将两者与 NSLog 值匹配。任何帮助表示赞赏。

这是当前代码:

        NSData *rGeniusData = [[NSData alloc] initWithContentsOfURL:
                              [NSURL URLWithString:@"http://jbbar.ml/rgenius/ipapi.php"]];
                              //Parse The JSON
        NSError *error;
        NSMutableDictionary *allKB = [NSJSONSerialization JSONObjectWithData:rGeniusData
                                                          options:NSJSONReadingMutableContainers
                                                          error:&error];

        NSArray *kb = allKB[@"KnowledgeBaseEntries"];NSLog(@"AboveDP= %@",  kb);

【问题讨论】:

  • 你想要什么?你想匹配一个你想从 nslog 中选择你的值的值吗?
  • 我已将标题导入 UITableView
  • 我已将 Titles 导入 UITableView 并在选择时,我希望能够使用 Title Var 我必须与 Value 匹配,因此当我在新视图中加载时,我可以打印 Value通过它自己。 Title = "另一个测试单元"; Value = "content

    这是有内容的";我现在正在使用: NSArray *kbc = [allKB[@"KnowledgeBaseEntries"] objectAtIndex:0];只得到一个结果。即:
  • 我如何打印单个 KnowledgeBaseEntry 中的值

标签: objective-c json


【解决方案1】:

像这样解析

NSDictionary *dict;
NSString *strAllowBotAccess =[[dict objectForKey:@"KnowledgeBaseEntry"]objectForKey:@"AllowBotAccess"];

NSLog(@"dict=====%@", strAllowBotAccess);

【讨论】:

    【解决方案2】:

    试试https://github.com/jsonmodel/jsonmodel JSONModel 允许快速创建智能数据模型。您可以在您的 iOS、macOS、watchOS 和 tvOS 应用程序中使用它。模型类和 JSON 输入的自动自省大大减少了您必须编写的代码量。

    易于使用,您可以解析所有键或其中的一部分。

    @interface YourModel : JSONModel
    @property (nonatomic) NSInteger id;
    @property (nonatomic) NSString *value;
    @end
    
    @implementation YourModel
    
    + (JSONKeyMapper *)keyMapper
    {
        return [[JSONKeyMapper alloc] initWithModelToJSONDictionary:@{
            @"id": @"Id",
            @"value": @"KnowledgeBaseEntry.Value"
        }];
    }
    
    @end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-22
      • 2011-05-13
      • 1970-01-01
      相关资源
      最近更新 更多