convert an object to JSON in objective c - objective-c

I work in tool theos projects and i want convert an object into JSON.
I need a easy to use library with examples for converting NSObjects to JSON and back again
I check a lot of question like this but i can't use them.
I use JSONModel library but i have a lot of errors.
Anybody body have a good tutorial or a sample code to convert NSObject to JSON?
I don't have any idea whether I can created a json or not.
How can I fix this?

Look at my library - https://github.com/DimasSup/BaseMangedObjectModel
With it you can serialize/deserialize any your class. aslo can save it to SQLite database if needed. Also there are NetworkHelper class which help you send/receive your classes from remote server.

Related

Pretty Print object in Swift on debug area

I am new to swift. I am trying to find a way to accomplish that.
In objective-c project, when I NSLog a response body from api, I got something like this. its pretty and readable. Ojective-c Debug area
However, when i use swift, when I print a response body from api, I got something like that. It's super hard to read. Swift Debug area
is there any way in swift, i could see the same format in debug area same as objective-c project.
If anyone could help, I really appreciate.
Thanks in advance.
It mainly depends on what you're trying to print in the Debug area. From Apple docs:
Swift provides a default debugging textual representation for any type. That default representation is used by the String(reflecting:) initializer and the debugPrint(_:) function for types that don’t provide their own.
From the image you've linked, it looks like you're printing a Dictionary, which prints out like that and there's not a straightforward way to change it. Considering you're getting data from an API, you can do debugging in three ways:
Print the raw body: if your server is returning JSON, that's usually readable and, in case it isn't, there are many JSON formatters/viewers online.
Parse the response into instances of a class that you define as conforming to CustomDebugStringConvertible; then, print the resulting array. This will not work in case the server response is malformed.
Loop through the dictionary and manually print keys and values in a format that looks readable to you.

Converting JSON to Objective-C object using key value coding

I am an Objective-C newbie coming from the Java world, so I am trying to find out how to do something that would be (relatively) straightforward to do in Java.
In an iPhone app I am accessing a rest webservice that returns JSON. I am using the AFNetworking framework, which provides support for using the NSJSONSerialization class added to iOS 5. I would like to know if there is an "automatic" way to turn the JSON object that is returned into one of my "strongly" typed classes (for example, my UserAccount class).
In other words, I'm trying to do something like:
id json = ... call webservice that returns json, which AFNetworking parses ...
UserAccount myUserAccount = [UserAccount alloc] init];
//then here I'd like something that iterates through all the properties on the
//json object and sets them on the myUserAccount object using key value coding
Is there a straightforward way to accomplish this?
Nope, not that I know of. The most common way is to add a constructor to your class called initWithDictionary, that accepts the JSON dictionary and gets the relevant information from it.

Parsing the Xml data in xcode

I am beginner to iphone. My requirement is to invoke the sap webservice in iphone.I got the result data which is in xml form.The result is to be in NSString which is of Xml form.Then how to get that result into the array in xcode.Please help me.
Here is a great library on GitHub-XML to NSDictionary
It isn't quite an NSArray but xml files are rarely just Arrays, so this provides an intermediate, dictionaries when needed, arrays if possible.
1. You can use NSXMLParser for this purpose, read through the delegate methods in https://developer.apple.com/library/ios/documentation/cocoa/reference/NSXMLParserDelegate_Protocol/Reference/Reference.html
2. This question is pretty repetitive, you could have just googled "Parsing the Xml data in iPhone" or "Parsing the Xml data using xCode" you would have got loads of results. Go trhough this ->
http://www.xcode-tutorials.com/parsing-xml-files/
and maybe this-> http://www.edumobile.org/iphone/iphone-programming-tutorials/parsing-an-xml-file/
3. Next time please do search.
Your question is quite general and it's difficult to give you a specific answer.
Here some guidelines to consume web services in iOS.
First, you need to have a mechanism that helps you to consume the ws. To do it, you can use NSUrlConnection class (see NSURLConnection Class) or use a the ASIHttpRequest framework (see ASIHTTPRequest). In this manner you can make requests and dowload response messages.
Since you use soap messages, you have first to create the request message manually. You can use class method of NSString stringWithFormat or use ASIFormDataRequest class of ASIHttpRequest framework.
Finally, if you receive a message that is like the following you posted in your comment, you need to parse it. Remember that is a soap message and it doesn't have only your tags. To do it, you can use NSXMLParser class (see NSXMLParser Class) or use GDataXML parser (see how-to-read-and-write-xml-documents-with-gdataxml).
Out there there are plenty of tutorials or posts on how to "consume web services on iOS". You can find them. In addition, there are also some kits that, taken the your service, create class wrappers to consume your services. In this case you don't need to create or parse manually masseges.
A last remark. When you need to consume data taken from a service, maybe a REST architecture could be easier to set up.
Hope it helps.

How to convert JSON object to Objective-C Cocoa object?

Am developing a cocoa application, where I have to invoke java web scripts and fetch the response of the web script object and convert it into Objective C readable objects. I would then need to manipulate the fetched data for import, print, etc.
How to I interact with the web script and convert into cocoa readable objects?
Any pointers on the same, will be appreciated.
Thanks,
Nana
Convert the JSON string into an NSDictionary using tools like yajl, JSONKit or iOS5 JSON
Use https://github.com/elado/jastor to convert this NSDictionary to a real Objective-C class with typed properties, nested properties, arrays etc.
In iOS 5 native JSON parsing is supported (documentation). In addition you can use a number of third party libraries to download and parse JSON (see JSONKit and HTTPRiot or RestKit)
A full reference of awesome libraries can be found here.

Using Objective-C Metadata to Generate Class Dependency Graph

This guy came up with a pretty neat tool to generate a class dependency graph - however, it relies on parsing your source code and looking for #import directives.
http://seriot.ch/blog.php?article=20110124
https://github.com/nst/objc_dep/blob/master/objc-dep.py
This is neat, but I have a number of problems with this. Not least of which is it doesn't take into account imports of imports nor prefix headers nor whether-or-not the class(es) in the file referenced by the import are actually being used.
I'd like to do something more akin to class-dump and examine the Objective-C metadata stored in the Mach-O file to generate an in-memory representation of the class dependencies.
I'd rather not do this from scratch, so I'm wondering:
Has it already been done?
Is there an open-source library which would provide me with the foundational tools I need to extract this information (a library which examines the Mach-O file and creates a façade of the Objective-C information contained within - such that I could iterate over all of the classes, their methods, properties, ivars, etc and scan for references to other classes) I figure class-dump's source would be a good place to start.
If you have experience in this sort of thing, is what I'm trying to accomplish feasible?
What roadblocks will I need to overcome?
Has it already been done?
Not that I know of.
Is there an open-source library which would provide me with the
foundational tools I need to extract this information?
At the core of class-dump is libMachObjC which does exatly what you want, i.e. parse all classes/methods/ivars and more. The API is very clean, it should be very easy to use.
If you have experience in this sort of thing, is what I'm trying to
accomplish feasible?
Unfortunately, no because some classes don't declare the real class but use id instead. For example, here is the information that can be extracted from a class-dump of UIKit:
#interface UITableView : UIScrollView <NSCoding>
{
int _style;
id <UITableViewDataSource> _dataSource;
id _rowData;
...
The _rowData ivar type information is id but if you check at runtime you will see that _rowData is an instance of the UITableViewRowData class. This information is not present in the Mach-O binary so you have no way to find the relation between UITableView and UITableViewRowData. The same applies for method parameters.
Here's a solution that relies on information in mach.o files, and generates graph dependency based on that information: https://github.com/PaulTaykalo/objc-dependency-visualizer
Has it already been done?
yes - but i can't recommend a good public implementation
Is there an open-source library which would provide me with the foundational tools I need to extract this information (a library which examines the Mach-O file and creates a façade of the Objective-C information contained within - such that I could iterate over all of the classes, their methods, properties, ivars, etc and scan for references to other classes) I figure class-dump's source would be a good place to start.
most use cases would benefit by using the objc runtime facilities objc/... rather than examining the binary.
If you have experience in this sort of thing, is what I'm trying to accomplish feasible?
yes. i've done something similar using the objc runtime.
What roadblocks will I need to overcome?
that depends largely on the level of detail you want... implementation time if you find no such implementation, but i figure you will find a few options if you google the more esoteric functions in the objc runtime; perhaps you would find one in an (open) language binding or bridge?
if you do end up writing one yourself, you can get registered objc classes using objc_getClassList, then access the properties/information you want from there.