RestKit RKObjectMapping.. Nested & throwaway objects - ios

So, I have an api call that'll output some raw json in the format below...
A bit messy, but I'm only interested in the Mapping portion of this json. If I had access to the raw data, I'd serialise the json cut to the dictionary / array I want to iterate through.. However the client is using restkit and this doesn't seem quite as easy as I'm imagining it....
Also What should I do with the Images section? Again, if was doing this with serialised json, I could open up the dictionary and set what's inside to an object with name description etc... can I do this as well?
{
"rsp": {
sn : "SerialNumber",
name : "Service Name",
from : "2000-01-01T00:00:00.000+01:00",
to : "2000-01-02T00:00:00.000+01:00",
mappings : {
"mapping" : {
"1" : {
from : 2000-06-01T00:01:00.000+01:00
to : 2000-06-01T01:02:00.000+01:00
content : {
name : "name"
description : "description"
images : {
image : "b47ab5a8.png"
}
}
},
"2" : {
from : 2000-06-01T00:01:00.000+01:00
to : 2000-06-01T01:02:00.000+01:00
content : {
name : "name"
description : "description"
images : {
image : "b47ab5a8.png"
}
}
},
// etc...
My question is
// what goes here
RKObjectMapping *itemMapping = [RKObjectMapping mappingForClass:[MYItem class]];
[itemMapping addAttributeMappingsFromDictionary:#{#"description":#"itemDescription",
#"name":#"name"}];
And how do I add the image to MYItem?

For the image your mapping is something like:
[itemMapping addAttributeMappingsFromDictionary:#{#"description":#"itemDescription",
#"name":#"name",
#"images.image":#"image"}];
i.e. key paths work
But, not that they only work for dictionaries. You can't index into arrays arbitrarily in the same way.
The key path is also in the response descriptor and that's how you drill down to the mapping, i.e. rsp.mappings.mapping (again, because it's dictionaries).

Related

Neo4j Rest API datetime data types

I am using neo4j REST API to create nodes and relationships. I refer following documentation.
https://neo4j.com/docs/http-api/3.5/actions/
The nodes that I am dealing with have dynamic properties. Therefore I am using following payload with parameters to create nodes.
{
"statements" : [ {
"statement" : "CREATE (n:Person $props) RETURN n",
"parameters" : {
"props" : {
"name" : "My Node",
"dob" : "datetime('20211229T153000')",
"age": 55,
"awsome": true
}
}
} ]
}
This is perfectly work with String, Integer, Boolean data types. However I need to use datetime data type for "dob" . If I use datetime function within double quotes (as in above example) neo4j treat it as string value and does not store as datatime data type.
Is there a solution for this?
You cannot put non-literal values inside the json data (props). However, you can change your cypher statement (create) to update the node and set the dob to a datetime field. But first you need to convert that string into datetime.
{
"statements" : [ {
"statement" : "CREATE (n:Person $props) SET n.dob = datetime({epochSeconds:apoc.date.parse(replace('20211229T153000','T', ' '),'s', 'yyyyMMDD HHmmss')}) RETURN n",
"parameters" : {
"props" : {
"name" : "My Node",
"age": 55,
"awsome": true
}
}
} ]
}
I replace the char 'T' into space so that it can be parsed. Then convert it into epoch seconds so that datetime will convert it properly. I tested it in my local neo4j so it should also work for you.
Result:

Posting to a complex typed array

I have an ORDER structure in ABAP that has another ITEMS structure within it which will contain multiple items per order.
I'm populating this structure through a SAP Gateway service, which works for an ORDER + a single ITEM.
{
"d": {
"Venueid": "dsfgg",
"Items" : {
"__metadata" : {
"type" : "ZGW_XXXX_SRV.Items"},
"Venueid" : "dsd",
"Type" : ""
}
}
}
However, what would be the syntax to provide an array of more than one ITEM?
The issue was because multiples can only be handled with the DEEP_INSERT method, not the normal CREATE.

How to query nested elements in Firebase?

"John" : {
"David" : {
"-KIMA0aPsujdAOpkzP0w" : {
"message" : "hallu",
"sender" : "10154053432889835",
"time" : 1463898873196
}
}
},
"Harry" : {
"Christina" : {
"-KIMA0aPsujdAOpkzP0v" : {
"message" : "hallu",
"seen" : true,
"sender" : "self",
"time" : 1463898873195
}
},
"Pierce" : {
"-KILZ_GH7Ji9hQYNK-6p" : {
"message" : "Eli there.",
"seen" : true,
"sender" : "179914035712208",
"time" : 1463888795301
},
"-KIM8yPz2UDOZwHEg_nn" : {
"message" : "hahjajak",
"seen" : true,
"sender" : "self",
"time" : 1463898597847
},
}
I wanted to query the count of nodes where "seen" key has value "true" of top node which is John OR Harry There are multiple child node inside it and each child node has multiple child node which has automatic id set.
I just want to know the count of objects which has "seen" key set to true and also count of objects which has "seen" key set to false
I can extract all the values in node as follows:
ref.observeSingleEventOfType(.Value, withBlock: { snap in
for (key,value) in snap.value as! NSDictionary {
}
})
I can loop through the dictionary and count the number of objects manually. But that is not too computationally or data efficient as firebase is volume based.
What I want is to know if there is any query to count the number of objects whose "seen" key in root node is "true" or whose "seen" key in root node is "false".
UPDATE:
Data Structure
John and Harry are user unique ID. and the node David which is immediate child of John node is unique ID of person who the user has chatted with earlier.
And the KIMA0aPsujdAOpkzP0w node represent a unique message from user John to/from David.
So what i wanted was to count the number of messages from David to John not seen by John
If you want to get only the "seen" value, you should reconsider your database structure and denormalize where possible to make reading as fast as you can.
Keeping your structure to get "seen" value, each time you read you have to get the entire node and loop through it to get the value, downloading unnecessary data, wasting processing time and making overall operation slower.
You should create a new node and store just the "seen" value the way you are going to get it later. Lets say, the best way to structure your database is based on which read operations are going to do and what data do you need in those operations.
You shouldn't worry about duplicating data. Remember that Firebase has the "updateChildren" method that allows you to update data in different nodes in a single operation.
For more info you can read the docs about structuring data here: https://firebase.google.com/docs/database/android/structure-data
Cheers!
Not sure if this is the most efficient way to handle it, but for a rapid prototype I've been searching for the answer to this problem as well. I settled on using this strategy to isolate child nodes of the data structure:
ref.observeSingleEventOfType(FIRDataEventType.Value, withBlock: { snapshot in
for (key,value) in snapshot.value as! NSDictionary {
if (key as! String == "John/David/etc")
{
let newRef = FIRDatabase.database().reference().child("John").child(key as! String)
newRef.observeSingleEventOfType(FIRDataEventType.Value, withBlock: { snapshot in
for (childKey, childValue) in snapshot.value as! NSDictionary{
//do whatever you want with child values/etc
}
})
}
}
})
Keep in mind I am VERY new to Firebase on iOS and they just changed their documentation so I'm working through it as I can. I'm sure there are way better ways to accomplish accessing child data but this worked for my MVP so I went with it.
EDIT: This assumes that your original reference is aimed at the part of the data tree that you want to target. For example:
let ref = FIRDatabase.database().reference().child("John")
That way when you did ref.observeSingle..etc, it would look for the values underneath that child object, instead of the entire tree.

How to extract data from JSON with Alamofire

I use the Alamofire library this way:
Alamofire.request(.POST, "http://www.somesample.com/getData.php", parameters: ["user":"charles"]).responseJSON{jsonData in
var theData = jsonData.result.value
If I debug print the theData variable, it throws something like:
[
{
"userId" : "61",
"userPicture" : "147884767502.jpg",
"wasId" : "80",
"favorite" : "0",
"message" : "how are you?",
"username" : "paco",
"date" : "13\/10\/2015 03:44PM",
"userPhrase" : "hello"
"repliesNumber" : 2
},
{
"userId" : "3",
"userPicture" : "149181897286.jpg",
"wasId" : "5",
"favorite" : "0",
"message" : "let's go!",
"username" : "loliFlower",
"date" : "30\/08\/2015 07:48PM",
"userPhrase" : "ciiiii!",
"repliesNumber" : 3
}
]
I usually use SwiftyJSON so I write (even I use a for loop to walk to every single index of the array the SwiftyJSON makes:
var myData = JSON(theData.result.value)
print(myData[0]["username"].stringValue)
But what if I don't want to use SwiftyJSON library anymore? what is the native way to do this?
I recommend that you use code generation to create native models out of the json response, this will save your time of parsing by hand and reduce the risk of errors due to mistaken keys, all elements will be accessible by model properties, this will be purely native and the models will make more sense rather checking the keys. Check http://www.json4swift.com and let me know if you require further help in initiating the object from your json response. (make sure you enter the actual json response and not the printed object as that in your question)

Rails: JSON Parsing a URL for later use

I'm trying to accomplish something really easily but I could not solve it to my beginner level in rails. The question is how do you assign a JSON objects attribute to a rails object.. I'm trying to send a request to Google Map's URL with some parameters. Without JSON parsing it works just fine, however when I try to parse it, I receive many errors. The regular JSON response from Google when I query the URL ([http://maps.googleapis.com/maps/api/directions/json?origin=40.983204,29.0216549&destination=40.99160908659266,29.02334690093994&sensor=false][1]) is like down below;
{
"routes" : [
{
"bounds" : {
"southwest" : {
"lat" : 40.98289,
"lng" : 29.02054
},
"northeast" : {
"lat" : 40.99148,
"lng" : 29.02388
}
},
"summary" : "Mühürdar Cd",
"waypoint_order" : [],
"legs" : [
{
"start_location" : {
"lat" : 40.98322,
"lng" : 29.02166
},
"distance" : {
"text" : "1.3 km",
"value" : 1324
},
And goes on.
The code I have to get the "summary" attribute is;
#request = Net::HTTP.get(URI.parse('http://maps.googleapis.com/maps/api/directions/json?origin=40.983204,29.0216549&destination=40.99160908659266,29.02334690093994&sensor=false'))
#result=JSON.parse(#request)["routes"]["summary"]
I wanted to ask what would be the proper way for me to get the summary attribute from the response?
hash['routes'][0]['legs'][0]['duration']['text']
would allow you to access Text and
hash['routes'][0]['legs'][0]['duration']['value']
the Value. Just try
hash['routes'][0]['legs'][0]['duration'].class
to see what type it is and access it accordingly

Resources