我正在从NSMutableDictionary生成一个JSON文档,该文档由指向NSStrings的键组成,以及两个指向其他NSMutableDictionary的键。我的问题是,当我输出JSON文档时,我注意到JSON文档具有以不同顺序插入NSMutableDictionary中的对象。例如,我目前的输出如下所示:
JSON Output: {
"devicetype" : "iPhone Simulator",
"os" : "6.0",
"test_results" : [
{
"date" : "2012-12-25T20:38:09",
"name" : "Test 1",
"result" : "Fail"
},
{
"date" : "2012-12-25T20:38:11",
"name" : "Test 2",
"result" : "Pass"
},
{
"date" : "2012-12-25T20:38:13",
"name" : "Test 3",
"result" : "Pass"
},
{
"date" : "2012-12-25T20:38:19",
"name" : "Test 4",
"result" : "Fail"
}
],
"devicename" : "?????",
"reports" : [
],
"device_id" : "bb88413b178b4ef77830d385b9a4bd6d",
"platform" : "iPhone OS"
}
但我希望输出看起来像这样:
JSON Output: {
"devicetype" : "iPhone Simulator",
"os" : "6.0",
"devicename" : "?????",
"device_id" : "bb88413b178b4ef77830d385b9a4bd6d",
"platform" : "iPhone OS"
"test_results" : [
{
"date" : "2012-12-25T20:38:09",
"name" : "Test 1",
"result" : "Fail"
},
{
"date" : "2012-12-25T20:38:11",
"name" : "Test 2",
"result" : "Pass"
},
{
"date" : "2012-12-25T20:38:13",
"name" : "Test 3",
"result" : "Pass"
},
{
"date" : "2012-12-25T20:38:19",
"name" : "Test 4",
"result" : "Fail"
}
],
"reports" : [
],
}
我创建JSON文档的代码如下所示:
NSMutableArray* reports = [NSMutableArray array];
NSMutableArray* results = [NSMutableArray array];
for (TestResult *testObject in testResultArray) {
if ([[DataModel sharedInstance] getScore:testObject.score] == @"Not Tested") {
NSMutableDictionary *naTest = [NSMutableDictionary dictionary];
[naTest setObject:testObject.testName forKey:@"name"];
[naTest setObject:testObject.dateStamp forKey:@"date"];
[reports addObject:naTest];
}
else {
NSMutableDictionary *fullTest = [NSMutableDictionary dictionary];
NSString *score = [[DataModel sharedInstance] getScore:testObject.score];
[fullTest setObject:score forKey:@"result"];
[fullTest setObject:testObject.testName forKey:@"name"];
[fullTest setObject:testObject.dateStamp forKey:@"date"];
[results addObject:fullTest];
}
}
NSMutableDictionary *mainDoc = [NSMutableDictionary dictionary];
[mainDoc setObject:udid forKey:@"device_id"];
[mainDoc setObject:systemName forKey:@"platform"];
[mainDoc setObject:systemVersion forKey:@"os"];
[mainDoc setObject:@"?????" forKey:@"devicename"];
[mainDoc setObject:deviceType forKey:@"devicetype"];
[mainDoc setObject:results forKey:@"test_results"];
[mainDoc setObject:reports forKey:@"reports"];
NSError *ierror = nil;
NSData *jsnData = [NSJSONSerialization dataWithJSONObject:mainDoc options:NSJSONWritingPrettyPrinted error:&ierror];
NSString *jsnString = [[NSString alloc] initWithData:jsnData encoding:NSUTF8StringEncoding];
NSLog(@"JSON Output: %@", jsnString);
}
我的另一个担心是test_results的输出也没有按照我想要的顺序出现。它们以“date”,“name”,“result”的顺序出现,我希望输出为“name”,“date”,“result”。有没有办法可以纠正这个顺序呢?
答案 0 :(得分:3)
NSDictionary不提供有序的存储容器 - 您或许应该首先研究如何呈现数据,即直接查询字典键以对NSDictionary进行排序,以及将字典对象加载到NSArray中。
有很多关于NSDictionary缺乏“秩序”的信息。
这个SO问题提供了许多有用的解决方案,例如使用具有有序字典类的外部库,以及使用SortDescriptors: Is the Objective-C dictionary an ordered container?
答案 1 :(得分:1)
NSDictionary不保证保持键/值对的顺序(并且它不应该因为NSDictionary不是数组,它是一个查找表)。如果你真的想要对它进行排序,最简单的方法是保持另一个数组,其内容是按照你想要的顺序存储的字典的键。当你解压缩JSON时,使用数组来查询字典。