Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSD字典格式_Ios_Objective C_Json_Nsarray_Nsdictionary - Fatal编程技术网

Ios NSD字典格式

Ios NSD字典格式,ios,objective-c,json,nsarray,nsdictionary,Ios,Objective C,Json,Nsarray,Nsdictionary,谁能帮我创建以下结构的NSDictionary格式: { key1 = "value1"; key2 = "value2"; key3 = [ { key01 = "value01"; key02 = "value02"; }, { key01 = "value01"; key02 = "value

谁能帮我创建以下结构的NSDictionary格式:

{
key1 = "value1";
    key2 = "value2";
    key3 =     [
                {
            key01 = "value01";
            key02 = "value02";
        },
                {
            key01 = "value01";
            key02 = "value02";
        },
                {
            key01 = "value01";
            key02 = "value02";
        }
    ];
}

试试这段代码,它可能会对你有所帮助

NSDictionary *dicationary = @{
                                  @"key1":@"value1",
                                  @"key2":@"value2",
                                  @"key3":@[@{@"key01":@"value01",@"key02":@"value02"},
                                            @{@"key01":@"value01",@"key02":@"value02"},
                                            @{@"key01":@"value01",@"key02":@"value02"}]
                                  };

以下内容适用于您:

NSString *jsonString = @"{\"ID\":{\"Content\":268,\"type\":\"text\"}}";
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

NSLog(@"%@", jsonDict[@"ID"][@"Content"]);
将返回给您:
268

obj-c中有API可以将Json转换为nsdictionary。我想您应该试试:

  • 首先将json转换为nsdata(假设上面的json是字符串格式)
  • 2.然后使用API将其转换为NSDictionary:

    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    

    为了回答您关于将JSON数据转换为NSDictionary的问题,这里是:

    (假设您已经获得JSON数据)



    这是NSDictionary格式,您想问什么?如何编写此json结构?您想将其转换为NSDictionary吗?格式很重要,它使此代码易于理解。
    // add the first 2 VALUES with it's KEYS
    NSMutableDictionary *mainDict = [NSMutableDictionary dictionary];
    [mainDict setValue:VALUE1 forKey:KEY1];
    [mainDict setValue:VALUE2 forKey:KEY2];
    
    // then for the last KEY, create a mutable array where you will store your sub dictionaries
    NSMutableArray *ma = [NSMutableArray array];
    NSMutableDictionary *subDict = [NSMutableDictionary dictionary];
    [subDict setValue:SUB_VALUE1 forKey:SUB_KEY1];
    [subDict setValue:SUB_VALUE1 forKey:SUB_KEY2];
    [ma addObject:subDict];
    
    // then add that array to your main dictionary
    [mainDict setValue:ma forKey:KEY3];
    
    // check the output
    NSLog(@"mainDict : %@", mainDict);
    
    // SAMPLE DATA - Test this if this is what you want
    NSMutableDictionary *mainDict = [NSMutableDictionary dictionary];
    [mainDict setValue:@"value1" forKey:@"key1"];
    [mainDict setValue:@"value2" forKey:@"key2"];
    
    NSMutableArray *ma = [NSMutableArray array];
    NSMutableDictionary *subDict = [NSMutableDictionary dictionary];
    [subDict setValue:@"subValue1" forKey:@"subKey1"];
    [subDict setValue:@"subValue2" forKey:@"subKey2"];
    [ma addObject:subDict];
    
    [mainDict setValue:ma forKey:@"key3"];
    
    NSLog(@"mainDict : %@", mainDict);