所以我有一个数组想要转换为 nsdictionary。我的流程运行良好,但我想向 json 字符串添加一个标题。
NSArray *showarray = [[MMIStore defaultStore] loadAllShows ];
NSMutableArray* jsonArray = [[NSMutableArray alloc] init];
for (Show* show in showarray) {
NSMutableDictionary* showDictionary = [[NSMutableDictionary alloc] init];
[showDictionary setObject:show.showid forKey:@"Showid"];
[jsonArray addObject:showDictionary];
}
NSData* nsdata = [NSJSONSerialization dataWithJSONObject:jsonArrayoptions:NSJSONReadingMutableContainers error:nil];
NSString* jsonString =[[NSString alloc] initWithData:nsdata encoding:NSUTF8StringEncoding];
NSLog(@"%@", jsonString);
以下输出:
{[
{
"Showid" : "10027"
},
{
"Showid" : "10707"
},
{
"Showid" : "10759"
}.....
]
如何让它看起来像这样
{
"Shows":[
{
"Showid" : "10027"
},
{
"Showid" : "10707"
},
{
"Showid" : "10759"
}....
]
}
最佳答案
NSMutableString *jsonString1 = [[@"{Shows:[" stringByAppendingString:jsonString] stringByAppendingString:@"]}"];
由于您的 jsonstring 是一个字符串,因此您可以更改它。
希望这有帮助..
编辑:
NSString *jsonString1 = [NSString stringWithFormat:@"{"Shows":%@}",jsonString];
关于ios - NSJSONSerialization Json 格式化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14879403/