RestKit : How to parse the JSON response to NSDictionary without mapping any entity or class? - ios

I just want the RestKit to parse the JSON data into NSDictionary, but not a class. This is because the attributes of the JSON data is dynamic, means the number of fields is not fixed and field count can be large. So I don't want to create a huge class to map the json data. Just keep that in NSDictionary. Does RestKit provide this functionality or we have to work out some other way.Guidance Needed.
Thanks.
Have to modify the Restkit to support the ability ....

RKObjectRequestOperation.HTTPRequestOperation.responseData
then parse the json data to dictionary or array

Either use AFNetworking (which RestKit is built on top of) or use a dynamic mapping (depending on your destination class needs).
If you have large arbitrary data then you should just avoid using RestKit as it will only slow your performance.

Related

Save a large JSON to Realm using Swift 3

I have a JSON with more or less 75 keys.
I need to receive this JSON and store offline it using Realm.
I do not want to iterate through the keys, since I've heard that there are ways to save a large JSON using a few lines. How can I do this?
EDIT:
My JSON (
I saved on a server away because it's too big)
http://myjson.com/i7e6l
There is no easy, one liner to parse the JSON and store it in Realm, since each JSON response is unique and no framework can have explicit knowledge about the structure of your JSON response without you giving some information to this framework about your JSON.
You will need to write some code either to parse the response or to make a mapping between your JSON response's fields and the properties of your Realm object. If you choose the latter solution, you can use Alamofire Object Mapper to do the JSON parsing automatically, but even then you have to write code for the mapping.

Swift : How to avoid duplicate entries in CoreData for implementation with JSON array?

I am developing an app in Swift which fetches JSON records from API, converts it into NSArray and reloads in UITableView.
I need to map the NSArray with CoreData and, when making a new request to API, if there is new child added in JSON response, I've to store only that child in CoreData. I.E. I have to avoid repetition of values of NSArray in CoreData.
I found few solution as "using unique constraint for data models" and "HyperSync and DATAStack" but I am not able to do so. Do I have to implement a module to check through all the records ?
If your objects in JSON have some identification, e.g. [ { "id" : 4, ...} ...], then you should store these "id" values in your CoreData objects, and try fetching already existing ones during mapping before creating new ones.
If there is no identification or unique fields in JSON objects, then your task is not resolvable.
E.g. RestKit framework does all that for you automatically, you have to configure only mapping and identificationAttributes.

How to retain order of JSON data retrieved with AFNetworking?

I am using AFNetworking to retrieve JSON data from a web service. Part of the response string I get is:
{"DATA":{"LEASE TYPE":"3 Yrs + 0 renew of 0 Yrs","LANDLORD":"","TENANT":"test comp"...
and so on. The order of the key values in the "DATA" dictionary ("LEASE TYPE","LANDLORD","TENANT"...) is important for presentation purposes. However, when AFNetworking calls NSJSONSerialization's:
+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error;
method, the returned dictionary has the keys in a different order.
I notice that the AFJSONRequestOperation object has the server's response stored as an NSString, with everything in the correct order. However I'm not keen on parsing the JSON by hand if I can avoid it.
Can anyone suggest a way that will let me get at / keep the keys in their original order?
Thanks.
If the order is important use an array not a dictionary, dictionaries are be by their nature unordered. Or add an array of dictionary keys in the order desired.
If you have no control over the response that is sent you will have to parse the JSON yourself at least for the ordering.
When you'r creating an NSDictionary, the order will not be the same. I often recognized that dictionaries get ordered by key-name alphabetically.
But when using dictionaries the order doesn't really matter. And they shouldn't!
As the previous answers mentions dictionaries are by nature without order, but you can find here a nice class of OrderedDictionary:
http://www.cocoawithlove.com/2008/12/ordereddictionary-subclassing-cocoa.html
http://projectswithlove.com/projects/OrderedDictionary.zip

Hardcoding value into Coredata using RestKit for iOS

I am using the latest RestKit for iOS. I have a few different APIs that I want to map into the same CoreData Object. I am trying to hard code a value in field on the object that identifies which API the data came from.
Does anyone know if this is possible using the RKEntityMapping for RestKit?
You need to set your API field, which will store this value, to transient in your core data object. Next add block with setWillMapDeserializedResponseBlock: in which you add an appropriative indicator value to the mapping dictionary.

NSDictionary Vs. NSArray

I am reading on objective-c (a nerd ranch book), and I can't help thinking about this question: How do I decide which collection type, NSArray or NSDictionary (both with or w/o their mutable subclasses), to use when reading content from URL?
Let's say am reading JSON data from a PHP script (a scenario am dealing with), which to use? I know it is stated in many references that it depends on structure of data (i.e. JSON), but could a clear outline of the two structures be outlined?
Thank you all for helping :)
NSArray is basically just an ordered collection of objects, which can be accessed by index.
NSDictionary provides access to its objects by key(typically NSStrings, but could be any object type like hash table).
To generate an object graph from a JSON string loaded via a URL, you use NSJSONSerialization, which generates an Objective-C object structure. The resulting object depends on the JSON string. If the top-level element in your JSON is an array (starts with "["), you'll get an NSArray. If the top-level element is a JSON object (starts with "{"), you'll get an NSDictionary.
You want to use NSArray when ever you have a collection of the same type of objects, and NSDictionary when you have attributes on an object.
If you have, lets say a person object containing a name, a phone number and an email you would put it in a dictionary.
Doing so allows the order of the values to be random, and gives you a more reliable code.
If you want to have more then one person you can then put the person objects in an array.
Doing so allow you to iterate the user objects.
"withContentOfURL" or "withContentOfFile" requires the data in the URL or the file to be in a specific format as it is required by Cocoa. JSON is not that format. You can only use these methods if you wrote the data to the file or the URL yourself in the first place, with the same data. If you write an NSArray, you can read an NSArray. If you write an NSDictionary, you can read an NSDictionary. Everything else will fail.

Resources