问题
I am generating a JSON document from an NSMutableDictionary that is composed of keys that point to NSStrings, as well as two keys that point in turn to other NSMutableDictionary's. My problem is that when I output the JSON document, I notice that the JSON document has the objects that I inserted in the NSMutableDictionary in a different order. For example, my present output looks like this:
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"
}
but I want the output to look like this:
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" : [
],
}
The code that I have that creates the JSON document looks like this:
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);
}
My other concern is that the output of the test_results are also not coming out in the order I want them to. They are coming out in the order "date", "name", "result", and I want the output to be "name", "date", "result". Is there a way that I can rectify this order as well?
回答1:
NSDictionary does not provide an ordered storage container - You should perhaps initially investigate how you present your data, i.e. querying directly for dictionary keys in order to sort your NSDictionary, as well as loading the dictionary objects into an NSArray(s).
There is plenty of information available on NSDictionary's inherent lack of 'order'.
This SO question provides lots of helpful solutions, such as using an external Library that has an ordered dictionary class, and using SortDescriptors: Is the Objective-C dictionary an ordered container?
回答2:
NSDictionary makes no guarantee about keeping the order of key/value pairs (and it shouldn't anyways because NSDictionary isn't an array, it's a look up table). If you really want this to be ordered, the simplest way would be to keep another array around whose contents are keys of the dictionary stored in the order you want. And when you unpack the JSON, use the array to query the dictionary.
来源:https://stackoverflow.com/questions/14035306/need-json-document-that-is-generated-to-be-in-same-order-as-objects-inserted-in