I was working on iPhone project and I needed to consume data from some JSON service.
I've tried to find some library to convert returned JSON String into my own custom objects... but I didn't.
Most popular JSON libraries (JSONKit, SBJson, etc ..) just convert JSON String into Objective-c data structures (NSDictionary, NSArray).
So I decided to write a lib to convert from NSDictionary and NSAarry into my own custom-objects.
ObjectMapper [https://github.com/MuhammadHewedy/ObjectMapper] is a project used to convert NSDictaionry to Custom user objects. It can used with JSON parsing libraries (JSONKit, SBJSON, etc) to convert the returned NSDictionary and NSArray into custom user objects
Example usage:
Complete Example project found here : https://github.com/MuhammadHewedy/ObjectMapper/tree/master/ObjectMapperTester
Good luck!
Showing posts with label library. Show all posts
Showing posts with label library. Show all posts
07 October 2011
18 September 2011
Building and installing NSJSONSerialization on gnustep to parse JSON data
I was seeking for a JSON Parser for Objective-c on gnustep runtime..
Thanks to sudo rm -rf he directed me to use the source code from gnustep repository .
I got the source code for Three Files
Steps to use:
Note: Steps below are executed on Mingw and should be used with little or no modifications on Linux.
You now safe to remove all the directory
Now You are done installing the
Now let's write a test program. Now create a brand new directory into your home directory say
Put the following code in it:
[The inclusion for the libraries should be exactly in this order]
Now run:
Congratulations! you can use gnustep to parse json data.
Disclaimer: This article is distributed in the hope that it will be useful, but without any warranty. Use it on your own.
Thanks to sudo rm -rf he directed me to use the source code from gnustep repository .
I got the source code for Three Files
NSJSONSerialization.h, NSJSONSerialization.m
and GSFastEnumeration.h
, although not worked directly, I've did very small modifications
Steps to use:
Note: Steps below are executed on Mingw and should be used with little or no modifications on Linux.
- Go and download the archive that contains the modified Three files from here.
put them into a directory say in your home folder with the following path:~/objc/json
-
cd ~/objc/json
and issue the following command to build the files:gcc -c `gnustep-config --objc-flags` *.m
If no errors reported, this means the build process succeed. and the object file has been generated. -
Now let's put this files inside the gnustep installation, to do so, mv the the .h files
NSJSONSerialization.h
andGSFastEnumeration.h
to/GNUstep/System/Library/Headers/Foundation
-
Now let's create a static library and put it inside gnustep libraries path:
Use the following command:
ar q libNSJSONSerialization.a NSJSONSerialization.o
Then move the generatedlibNSJSONSerialization.a
file to/GNUstep/System/Library/Libraries/
You now safe to remove all the directory
~/objc/json
and all of its contents.
Now You are done installing the
NSJSONSerialization
as a static lib into gnustep.Now let's write a test program. Now create a brand new directory into your home directory say
~/objc/json_test
and create a file named json_main.m
in this directory.Put the following code in it:
1. #import <Foundation/Foundation.h> 2. #import <Foundation/NSJSONSerialization.h> 3. 4. int main(void) 5. { 6. NSAutoreleasePool* pool = [[NSAutoreleasePool alloc]init]; 7. 8. NSDictionary* dictionary = [NSDictionary dictionaryWithObjects: 9. [NSArray arrayWithObjects: @"Muhammad", @"26", @"2011", @"Cairo, Egypt", 10. [NSArray arrayWithObjects: @"Arabic", @"English", nil] , nil] 11. forKeys: 12. [NSArray arrayWithObjects: @"name", @"age", @"year", @"address", @"language", nil]]; 13. NSError* error; 14. NSData* data = [NSJSONSerialization dataWithJSONObject: dictionary 15. options:NSJSONWritingPrettyPrinted 16. error: &error]; 17. if (error) 18. { 19. NSLog(@"ERROR %@", error); 20. }else 21. { 22. NSString* jsonString = [[NSString alloc] initWithData: data 23. encoding: NSASCIIStringEncoding]; 24. NSLog(@"%@", jsonString); 25. 26. [jsonString release]; 27. } 28. 29. [pool release]; 30. return 0; 31. }Now use the following command to compile it:
gcc `gnustep-config --objc-flags` *.m -L /GNUstep/System/Library/Libraries -lNSJSONSerialization -lobjc -lgnustep-base
[The inclusion for the libraries should be exactly in this order]
Now run:
$ a.exe
2011-09-18 01:35:49.726 a[5696] {
"language": [
"Arabic",
"English"
],
"year": "2011",
"name": "Muhammad",
"age": "26",
"address": "Cairo, Egypt"
}
Congratulations! you can use gnustep to parse json data.
Disclaimer: This article is distributed in the hope that it will be useful, but without any warranty. Use it on your own.
Subscribe to:
Posts (Atom)