我是ios编程的新手。我将数据放在NSDictionary中,然后在将其发送到服务器之前使用AFJSONRequestSerializer序列化为json。
现在,如果我的json具有像key这样简单的结构 - >价值对,然后我可以很容易地做到,但如果我的结构是这样的 -
"person" = (
{
"location" = {
city = "Manila";
Country = "Phillipines";
};
image = "http://mymanila.com/1.png";
link ="http://mymanila.com/1.html"
}
);
我的问题是如何放置位置,它本身是另一个带键值对的字典?感谢。
答案 0 :(得分:2)
你走了。
NSDictionary *locationDictionary = @
{
@"city" : @"Manila",
@"Country" : @"Phillipines"
};
NSDictionary *personDictionary = @
{
@"location" : locationDictionary,
@"image" : @"http://mymanila.com/1.png",
@"link" : @"http://mymanila.com/1.html"
};
NSDictionary *fullDictionary = @
{
@"person" : personDictionary
};
现在,只需序列化fullDictionary
。
答案 1 :(得分:0)
像这样:
NSMutableDictionary* person = [NSMutableDictionary dictionary];
NSDictionary* location = @{@"city":@"Manila", @"Country":@"Phillipines"};
[person setObject:location forKey:@"location"];
[person setObject:@"http://mymanila.com/1.png" forKey:@"image"];
[person setObject:@"http://mymanila.com/1.html" forKey:@"link"];
然后你可以将它转换为JSON,如下所示:
NSData *dictData = [NSKeyedArchiver archivedDataWithRootObject:person];
id jsonObject = [NSJSONSerialization JSONObjectWithData:dictData options:NSJSONReadingMutableContainers error:nil];