代码之家  ›  专栏  ›  技术社区  ›  MixCoded

NSDictionary格式

  •  -1
  • MixCoded  · 技术社区  · 9 年前

    谁能帮我创建一个如下结构的NSDictionary格式

    {
    key1 = "value1";
        key2 = "value2";
        key3 =     [
                    {
                key01 = "value01";
                key02 = "value02";
            },
                    {
                key01 = "value01";
                key02 = "value02";
            },
                    {
                key01 = "value01";
                key02 = "value02";
            }
        ];
    }
    
    4 回复  |  直到 9 年前
        1
  •  3
  •   Vishnuvardhan Woona    9 年前

    试试这段代码,也许会对你有所帮助。

    NSDictionary *dicationary = @{
                                      @"key1":@"value1",
                                      @"key2":@"value2",
                                      @"key3":@[@{@"key01":@"value01",@"key02":@"value02"},
                                                @{@"key01":@"value01",@"key02":@"value02"},
                                                @{@"key01":@"value01",@"key02":@"value02"}]
                                      };
    
        2
  •  1
  •   V-Xtreme    9 年前

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

    1. 首先将json转换为nsdata(假设上面的json是字符串格式)

    2.然后使用API将其转换为NSDictionary:

    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    
        3
  •  0
  •   Pepeng Hapon    9 年前

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

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

    // 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);
    
        4
  •  0
  •   Roy K    9 年前

    以下内容适用于您:

    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