【发布时间】:2014-06-14 03:27:16
【问题描述】:
假设我们有以下字典:
dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:currentItem], @"item number",
[NSNumber numberWithInt:([[item valueForKey:@"section"] intValue]+1)], @"section number",
currentDate, @"date of item",
[NSNumber numberWithDouble:timeDifference], @"time difference in millis",
nil];
然后我得到以下输出:
{
"time difference in millis" : 5.220093071460724,
"section number" : 1,
"date of item" : "28/04/2014 15:56:54,234",
"item number" : 3
}
我使用以下代码将字典转换为 JSON:
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonString = [[NSString alloc] initWithBytes:[jsonData bytes] length:[jsonData length] encoding:NSUTF8StringEncoding];
jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\\" withString:@""];
如何操作 JSON 字符串显示其键的顺序。例如,我想要:
{
"item number" : 3,
"section number" : 1,
"date of item" : "28/04/2014 15:56:54,234",
"time difference in millis" : 5.220093071460724
}
或者别的什么。关键是我想控制这个过程。我如何得到它?我的第一个想法是编写一个解析器,以我想要的方式显示排序。
这里有类似的问题,但我的重点是操纵顺序,而不是仅仅重新创建我从字典中放置它的顺序。
【问题讨论】:
标签: ios objective-c json dictionary