Ios NSD字典格式
谁能帮我创建以下结构的NSDictionary格式: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
{
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"]);
将返回给您:
268obj-c中有API可以将Json转换为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);