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

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.

Related

In Kotlin How to read and parse json file

I am new to Kotlin and trying to understand how I can read and parse the .json file.
Say, I have a file Test.json with some json array and need to read the array and stored the content in mutable list.
I tried searching the blog but could not find the answer.
Thanks in advance.
It depends, if you know the format of the JSON file maps to one of your Kotlin classes then you could use a library such as Jackson, Gson, Klaxon or Moshi to convert the contents of the file to an instance of this class.
Alternatively you could manually parse the JSON using Java's JSONObjects and work through the nested map of JsonObjects/Values.
I personally use this Klaxon library to parse json file and use in in my android application.
which I think will do your work.
Add dependncy to your gradle.build file
compile 'com.beust:klaxon:0.30'

convert an object to JSON in 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.

Qt Webkit Bridge: C++ access to JavaScript

I am using a QWebView and call to setHtml() to display some HTML/JavaScript pages. I am passing data updates using QWebFrame::evaluateJavaScript by passing it a QString containing a call to a JavaScript function (with arguments). After reading about the Qt WebKit Bridge I
feel like there should be a better way to do his. I see discussion of exposing Qt objects / functions in C++ to the JavaScript, but not the other direction. (I do not want the JavaScript to poll the C++ side for updates.) Is there a way to connect a JavaScript function as a slot to a Qt/C++ signal? (Or a similar pattern) I feel like I have looked through a lot of docs, posted questions (even the 'Similar Questions' as I type this), but have not seen examples of this. Appreciate any info, links or nudges in the right direction.
Use addToJavaScriptWindow object for exposing your C++ objects the Javascript tier:
page()->mainFrame()->addToJavaScriptWindowObject(QString("myObject"), myObject);
Check documentation here:
http://qt-project.org/doc/qt-4.8/qwebframe.html#addToJavaScriptWindowObject
That will expose your C++ myObject as "myObject".
Then, you can do exactly what you are looking for on your post, let's suppose myObject declares a signal in the following way:
signal:
mySignal(QString aParameter);
you can simply connect that signal to a slot on your web side as follows:
myObject.mySignal.connect(this, this.mySignalSlot);
In this case, mySignalSlot should have the same signature than mySignal declaration at the C++ class side (well, "same signature" would mean same number of parameters as javascript is not typed, so you won't need to declare a type for each of them).
What's really usefull here is to pass JSon objects. In case you need to pass big amount of data you can use base64 encoding. For images, QTWebKit supports natively QImage and QPixmap classes; these last two are natively encoded as complex javascript objects by QTWebKit Bridge engine.

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.

Is there a way to read in files in TypedStream format

I have a file in the following format:
NeXT/Apple typedstream data, little endian, version 4, system 1000
Looking at it in a hex editor, it's clearly made up of NSsomething objects (NSArray, NSValue, etc). It also appears to have an embedded plist!
I'm guessing there's a straightforward way to read this file and output it in some more readable fashion (similar to the output of repr() or print_r()).
I assume I'll need to do this using Objective-C?
First, some history:
Older versions of the Objective-C runtime (pre-OS X) included a psuedo-class called NXTypedStream, which is the pre-OPENSTEP ancestor of NSCoder. Older versions of Foundation contained a header called NSCompatibility.h, which had functions and categories for dealing with old NeXTStep formats. NSCompatibility.h no longer exists, but a (deprecated) subset of that functionality can still be found in NSCoder.h.
NSCoder debuted as part of the original Foundation Kit in OPENSTEP, but probably used typedstreams as its serialization format. At some point, it was switched over to a plist-based format. The current version of Interface Builder (as part of Xcode) is still able to read older, typedstream-based NIBs, which is a good clue that this functionality still exists in OS X.
Now, the solution:
I can't find this in any (current) Apple documentation, but it turns out that NSCoder/NSUnarchiver can still read typedstream files just fine. If you want to read a typedstream file in a Cocoa/Objective-C program, just do this:
NSUnarchiver *typedStreamUnarchiver = [[NSUnarchiver alloc] initForReadingWithData:[NSData dataWithContentsOfFile:#"<path to your typedstream file>"]];
That's it! The decoding is handled internally in a function called _decodeObject_old. Now you can unarchive using standard NSCoder methods, like:
id object = [typedStreamUnarchiver decodeObject];
NSLog(#"Decoded object: %#", object);
Note that if the class in the typedstream is not a valid class in your program, it will throw an NSArchiverArchiveInconsistency exception.
See also: http://www.stone.com/The_Cocoa_Files/Legacy_File_Formats.html
If it's a binary plist, it should be easy to read it with Xcode / Plist Editor, plutil, or your own Objective-C code. Otherwise, depending on the file, it could be more challenging. The file stores a serialized representation of Objective-C objects. As such, it's only intended to be readable by the software that created it.