如何通过Objective-C从JSON的键解析数据?

时间:2019-01-30 03:27:52

标签: ios 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”并将其设置为导航标题。
而且我还有一个按钮可以切换导航标题为“杰克”还是“吉米”。
因此,有任何想法可以实现此功能。

@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";

}

2 个答案:

答案 0 :(得分:0)

使用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 "];

答案 1 :(得分:0)

这非常简单,您只需要提取数组中的所有键,然后根据需要使用它们即可:-

让我们以您的数据为例:-

{
"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:@", "];
             }
             */
        }
    }
}