2017-02-23 79 views
-2

我想解析JSON,使用Objective-C這是NSLog以我當前的代碼進行回聲。解析JSON - Objective C

{ 
    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 Var,但我不知道如何將兩者匹配到NSLog Value。任何幫助表示讚賞。

下面是當前的代碼:

 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); 
+0

你想要什麼?你想匹配你想從nslog中選擇你的值的值嗎? –

+0

我已經將標題導入到UITableView中 –

+0

我已經將標題導入到UITableView中,並且在select中,我希望能夠使用標題Var我必須匹配的值,因此當我加載新的視圖時,我可以打印價值本身。 「另一個測試小組」; Value =「內容

這是內容」; 我現在在使用:NSArray * kbc = [allKB [@「KnowledgeBaseEntries」] objectAtIndex:0];得到一個結果。 IE: –

回答

-1

嘗試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 
0

解析這樣

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

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