Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9030671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:32:54+00:00 2026-06-16T07:32:54+00:00

I am generating a JSON document from an NSMutableDictionary that is composed of keys

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T07:32:55+00:00Added an answer on June 16, 2026 at 7:32 am

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an app that is importing data from JSON and generating (through JQuery's
I'm generating json from a Rails app, and am filtering fields using the format.json
I have written java code for generating json of my searched data from file.But
Performance issue, when generating around 800~ options from jSon object via javascript. Any suggestion,
I am generating a list of check boxes, after parsing it from a JSON.
I'm generating JSON response from PHP witch looks like this: { done:'1', options: [{
I am generating json from an xml file using the Newtonsoft dll. From the
I am programmatically generating a JSON-Schema schema. I wish to ensure that the schema
I have PHP generating JSON data but I am having a problem making a
I am generating the json from PHP.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.