0

I have a complex object that contains ivars of type NSString, NSArray, BOOL etc. I need to convert them to JSON.

What I am currently doing is setting the Keys for each of the NSObject's values (although the keys are same as the NSObject's properties) like below :-

[crEntityDict setObject:[[MyObjectName sharedObject] vinNumber] forKey:@"vinNumber"];
[crEntityDict setObject:[[MyObjectName sharedObject] make] forKey:@"make"];

MyObjectName is a Singleton object having the same properties as the keys set here. I just could not find how to convert this object directly into an NSDictionary with properties' names as Keys and properties' values as Values.

Is there any better, simpler way to do this?

EDIT Here is the complete Header file code for the NSObject that I am trying to convert to JSON :- MyObjectName.h

#import <Foundation/Foundation.h>

@interface MyObjectName : NSObject
{
    NSString *vinNumber,*make,*model;
    int mileage;
    BOOL isKeyPresent;
}

@property (nonatomic,strong) NSString *vinNumber,*make,*model;
@property (nonatomic,assign) int mileage;
@property (nonatomic,assign) BOOL isKeyPresent;

+ (MyObjectName *)sharedObjectName;

How can I directly convert this Object to a JSON representation, without creating a NSDictionary again by setting the values for the keys with same names as ivars, with the property names as Keys and property values as Values like : {"vinNumber":,"make":make_value_string,"model":model_value_string,"mileage":mileage_value_int,"isKeyPresent":isKeyPresent_value_bool}

ilight
  • 1,622
  • 2
  • 22
  • 44
  • Looks to me like you're building a dictionary above. You want something different? – Hot Licks May 18 '12 at 10:57
  • Edited my question with a few more details. Can you plz have a look at that? – ilight May 18 '12 at 11:08
  • I'm not sure that there's a tool to do that. You can, of course, provide an implementation of `JSONString`, or whatever the verb is your JSON kit of choice uses, but you still have to do the work internally, one way or the other. – Hot Licks May 18 '12 at 11:15
  • see my example [here](http://stackoverflow.com/questions/14958883/ios-serialize-deserialize-complex-json-generically-from-nsobject-class). The best method so far in generating json from nsobject – Alphapico May 28 '13 at 10:02

1 Answers1

0

Use one of the pre-rolled solutions from json.org.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242