2

So my JSON request should look something like:

{ 
   "MyDictionary" : ["some values"], 
   "RegularValue" : "regularValue",
}

So what I currently have done is:

NSDictionary *myDict = @{@"Blah" : @"1", @"Yadayada" : @"2"}; 
NSDictionary *jsonDict = @{"MyDictionary" : myDict, "RegularValue" : "someValue"};
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:kNilOptions error:&error];

So this works fine. The problem I have is, instead of manually creating MyDictionary, what I want to do is use a custom sublcass of NSObject and serialize that to JSON. I basically have to manually create the NSDictionary for the properties of my custom NSObject and put that in the NSJSONSerialization method. But I was wondering if there was a better way to do this? Thanks.

Crystal
  • 28,460
  • 62
  • 219
  • 393

2 Answers2

0

maybe this can help JLCustomClassDisassembler.

it iterates the custom object to get the properties. this will give you dictionary/array then just use sbjson/jsonkit or what ever you prefer to construct your json string.

Joshua
  • 2,432
  • 1
  • 20
  • 29
-3

To achieve this you would need to create your own custom NSObject class with attributes and more and still have a custom method to serialize the object, similar to the way toString works on java, on Objective-C you would need to implement

- (NSString *)description

on your method and then return the serialized object.

on this method you could read all the attributes of the current object and then somehow serialize them, this thread presents a nice sample code to read all the attributes of a certain Object

Get an object properties list in Objective-C

Community
  • 1
  • 1
perrohunter
  • 3,454
  • 8
  • 39
  • 55