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

  • Home
  • SEARCH
  • 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 7918785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:37:29+00:00 2026-06-03T15:37:29+00:00

I have an objective C class like, @interface message : NSObject { NSString *from;

  • 0

I have an objective C class like,

@interface message : NSObject {
 NSString *from;
 NSString *date;
 NSString *msg;
}

I have an NSMutableArray of instances of this message class. I want serialize all the instances in the NSMutableArray into a JSON file, using the new JSONSerialization APIs in iOS 5 SDK. How can I do this ?

Is creating a NSDictionary of each key, by iterating through each instance of the elements in the NSArray ? Can someone help with code of how to solve this ? I am not able to get good results in Google, as “JSON” skews the results to server-side calls and transfer of data instead of serialization. Thanks a lot.

EDIT:

NSError *writeError = nil; 
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:notifications options:NSJSONWritingPrettyPrinted error:&writeError];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 
NSLog(@"JSON Output: %@", jsonString);
  • 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-03T15:37:32+00:00Added an answer on June 3, 2026 at 3:37 pm

    EDIT: I have made a dummy app that should be a good example for you.

    I create a Message class from your code snippet;

    //Message.h
    @interface Message : NSObject {
        NSString *from_;
        NSString *date_;
        NSString *msg_;
    }
    
    @property (nonatomic, retain) NSString *from;
    @property (nonatomic, retain) NSString *date;
    @property (nonatomic, retain) NSString *msg;
    
    -(NSDictionary *)dictionary;
    
    @end
    
    //Message.m
    #import "Message.h"
    
    @implementation Message
    
    @synthesize from = from_;
    @synthesize date = date_;
    @synthesize msg = mesg_;
    
    -(void) dealloc {
        self.from = nil;
        self.date = nil;
        self.msg = nil;
        [super dealloc];
    }
    
    -(NSDictionary *)dictionary {
        return [NSDictionary dictionaryWithObjectsAndKeys:self.from,@"from",self.date,    @"date",self.msg, @"msg", nil];
    }
    

    Then I set up an NSArray of two messages in the AppDelegate. The trick is that not only does the top level object (notifications in your case) need to be serializable but so do all the elements that notifications contains: Thats why I created the dictionary method in the Message class.

    //AppDelegate.m
    ...
    Message* message1 = [[Message alloc] init];
    Message* message2 = [[Message alloc] init];
    
    message1.from = @"a";
    message1.date = @"b";
    message1.msg = @"c";
    
    message2.from = @"d";
    message2.date = @"e";
    message2.msg = @"f";
    
    NSArray* notifications = [NSArray arrayWithObjects:message1.dictionary, message2.dictionary, nil];
    [message1 release];
    [message2 release];
    
    
    NSError *writeError = nil; 
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:notifications options:NSJSONWritingPrettyPrinted error:&writeError];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 
    NSLog(@"JSON Output: %@", jsonString);
    
    @end
    

    The output when I run the application is thus:

    2012-05-11 11:58:36.018 stack[3146:f803] JSON Output: [
    {
    “msg” : “c”,
    “from” : “a”,
    “date” : “b”
    },
    {
    “msg” : “f”,
    “from” : “d”,
    “date” : “e”
    }
    ]

    ORIGINAL ANSWER:

    Is this the documentation you are looking for?

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

Sidebar

Related Questions

Say I have a class like this: @interface MyAwesomeClass : NSObject { @private NSString
I have an Objective-C class that looks something like: @interface myClass : NSObject {
I have this class: @interface RSSEntry : NSObject{ NSString *blogTitle; NSString *articleTitle; NSString *articleUrl;
I have a class like this: @interface MyCollection : NSObject { NSMutableDictionary *data; }
I have some Objective-C code that looks like this: #define myVar 10 float f
I have a property defined in a class like so: @interface myClass UIImageView *drawImage[4];
I have three Classes A,B,C. In Class A the code is like this, B*
Suppose I have Objective C interface SomeClass which has a class method called someMethod
I have an Objective-C NSMutableDictionary declared inside a class's @interface section, with getter/setter methods,
In the Modern Objective-C runtime, you can do something like this: @interface MyClass :

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.