簡體   English   中英

如何通過Objective-C從JSON的鍵解析數據?

[英]How to parse the data from json's key by objective-c?

我有一個像這樣的json文件:

{
"Jack": {
    "responseHeader": {
        "returnCode": "200",
        "returnMsg": "SUCCESS"
    },
    "responseBody": {
        "email": "Jack@gmailM.com",
        "mobile": "+100000000"
    }
},
"Jimmy": {
    "responseHeader": {
        "returnCode": "200",
        "returnMsg": "SUCCESS"
    },
    "responseBody": {
        "email": "Jimmy@gmailM.com",
        "mobile": "+100000001"
    }
  }
}

如果要獲取字典鍵“ Jack”和“ Jimmy”,並將它們設置為導航標題。
而且我還有一個按鈕可以切換導航標題為“ Jack”或“ Jimmy”。
因此,有任何想法可以執行此功能。

@property NSDictionary* dataDictionary;

- (void)readDataFromFile {

NSString * filePath =[[NSBundle mainBundle] pathForResource:@"myjson" ofType:@"json"];

NSError * error;
NSString* fileContents =[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];

if(error) {

    NSLog(@"Error reading file: %@",error.localizedDescription);
}

self.dataDictionary = [NSJSONSerialization
                       JSONObjectWithData:[fileContents dataUsingEncoding:NSUTF8StringEncoding]
                       options:0 error:NULL];

self.title = @"Jack or Jimmy";

}

使用NSArray實例方法componentsJoinedByString:

NSString * filePath =[[NSBundle mainBundle] pathForResource:@"myjson" ofType:@"json"];

NSError * error;
NSString* fileContents =[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];

if(error) {

    NSLog(@"Error reading file: %@",error.localizedDescription);
}

dic = [NSJSONSerialization
                       JSONObjectWithData:[fileContents dataUsingEncoding:NSUTF8StringEncoding]
                       options:0 error:NULL];
NSArray * yourKeys;
yourKeys = [dic allKeys];
strTitle = [yourKeys componentsJoinedByString:@" or "];

這非常簡單,您只需要提取數組中的所有鍵,然后根據需要使用它們,就像這樣:-

讓我們以您的數據為例:

{
"Jack": {
    "responseHeader": {
        "returnCode": "200",
        "returnMsg": "SUCCESS"
    },
    "responseBody": {
        "email": "Jack@gmailM.com",
        "mobile": "+100000000"
    }
},
"Jimmy": {
    "responseHeader": {
        "returnCode": "200",
        "returnMsg": "SUCCESS"
    },
    "responseBody": {
        "email": "Jimmy@gmailM.com",
        "mobile": "+100000001"
    }
  }
}

通過在字典上使用allKeys方法,您可以獲取數據中存在的所有鍵,而且我還更新了代碼以防止崩潰:-

-(void)readDataFromFile {
    NSString * filePath =[[NSBundle mainBundle] pathForResource:@"myjson" ofType:@"json"];
    NSError * error;
    NSString* fileContents =[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
    if(error){
        NSLog(@"Error reading file: %@",error.localizedDescription);
    }else if (fileContents){
        self.dataDictionary = [NSJSONSerialization
                               JSONObjectWithData:[fileContents dataUsingEncoding:NSUTF8StringEncoding]
                               options:0 error:NULL];
        if (self.dataDictionary){
            NSArray *allKeys = [self.dataDictionary allKeys];
            //allKeys will contain String value and after that you can do anything with your array
            if (allKeys.count){
                self.title = [allKeys componentsJoinedByString:@" or "];
            }
            /*
             You also seprate with comma's or anything you want like this
             if (allKeys.count){
             self.title = [yourKeys componentsJoinedByString:@", "];
             }
             */
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM