Hello I a class of type NSObject:
ProductDetails *details = [[ProductDetails alloc] init];
details.name = @"Soap1";
details.color = @"Red";
details.quantity = 4;
I want to pass the "details" object to a dictionary.
I did,
NSDictionary *dict = [NSDictionary dictionaryWithObject:details forKey:@"details"];
I am passing this dict to another method which performs a check on JSONSerialization:
if(![NSJSONSerialization isValidJSONObject:dict])
And I am getting a crash on this check. Am I doing anything wrong here? I know that the details I am getting is a JSON object and I am assigning it to the properties in my ProductDetails class.
Please help me. I am a noob in Objective-C.
I now tried:
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:(NSData*)details options:kNilOptions error:&error];
All I need here is an easy way to convert details to NSData.
I noticed that I have an array inside my object may be thats why all the ways I tried is throwing an exception. However since this question is becoming to big, I have started an another question thread for it where I have displayed the data I am getting inside the object - https://stackoverflow.com/questions/19081104/convert-nsobject-to-nsdictionary
This may well be the easiest way to achieve it. Do import
#import <objc/runtime.h>
in your class file.In .h File
in .m file
if any crash ,You check the property (NSMutableArray,NSString,etc ) in else if condition inside of for.
In Your Controller, in any func...
You can try mantle, Mantle makes it easy to write a simple model layer for your application. mantle
You also can use the
NSObject+APObjectMapping
category which is available on GitHub: https://github.com/aperechnev/APObjectMappingIt's a quit easy. Just describe the mapping rules in your class:
And then you can easily map your object to dictionary:
Also you can parse your object from dictionary:
As mmackh said, you want to define a custom method for your
ProductDetails
object that will return a simpleNSDictionary
of values, e.g.:Let's assume that we added
manufacturer
property to ourProductDetails
, which referenced aManufacturerDetails
class. We'd just write ajsonObject
for that class, too:And then change the
jsonObject
forProductDetails
to employ that, e.g.:If you have potentially nested collection objects (arrays and/or dictionaries) with custom objects that you want to encode, you could write a
jsonObject
method for each of those, too:If you do something like that, you can now convert arrays or dictionaries of your custom objects object into something that can be used for generating JSON:
If you're simply trying to save these objects in a file, I'd suggest
NSKeyedArchiver
andNSKeyedUnarchiver
. But if you need to generate JSON objects for your own private classes, you can do something like the above might work.The perfect way to do this is by using a library for serialization/deserialization many libraries are available but one i like is JagPropertyConverter https://github.com/jagill/JAGPropertyConverter
it can convert your Custom object into NSDictionary and vice versa
even it support to convert dictionary or array or any custom object within your object (i.e Composition)