Mapping a JSON array of unnamed values with RESTKIT 0.20 - restkit

When I configure mapping from a REST service returning JSON to a object, I normally do this:
RKObjectMapping *myMapping = [RKObjectMapping mappingForClass:[MyClass class]];
[myMapping addAttributeMappingsFromDictionary:#{#"Address" : #"address", #"City" : #"city"}];
and this works great for JSON with named attributes, but how do I map the following JSON to a object with the property "name"?
["My Value","Some other value","More stuff","Hello World"]
This JSON is just a array of values and has not name/key only values. How do I map this to a object with RESTKIT 0.20?
Thank you
Søren

This expression in square brackets is a json array: http://www.json.org. If you look on the syntax tree on the home page, you can consider, every json array is a value of a "variable" with a name. It means your expression has to look like this, to be a valid json:
{ "myArray": ["My Value","Some other value","More stuff","Hello World"] }
and you map it like you always do:
[myMapping addAttributeMappingsFromDictionary:#{#"myArray" : #"myArray"}];
Your parameter MyArray in mapping target class has then a type of NSArray.

Related

Accessing specific attribute of an object

Hello I have the following object
object = [#<ShopifyAPI::DiscountCode:0x000000000e1c78a8 #attributes={"code"=>"Disc2", "amount"=>"1.00", "type"=>"percentage"}, #prefix_options={}, #persisted=true>]
How can I properly access the "code" name of that object?
I have tried object[:code] and object.code but it appears I am overlooking something.
object is an array of ShopifyAPI::DiscountCode.
The best way to access it is
object[0].attributes['code']
If u want code of all the objects available in the array, you could get the array of values by
object.map { |obj| obj.attributes['code'] }
Given that this is an Array of ShopifyAPI::DiscountCodes (which inherit from ActiveResource::Base)
You can call the code method on them. eg:
object[0].code
#=> "Disc2"
object.map(&:code)
#=> ["Disc2"]
First, object is array:
obj0 = object[0]
Second, this is instance variable:
attributes = obj0.instance_variable_get(:#attributes)
Last, gets values by keys:
attributes['code']

Hyphens in serialized JSON OpenStruct

I have a JSON object such as:
"c": {
"10-20": 9.0,
"0-10": 8.5,
"30-end": 5.085714285714286,
"20-30": 10.3
}
When I convert that JSON to a serialized object using:
JSON.parse(response.body, object_class: OpenStruct)
It gives me:
<OpenStruct 10-20=0, 0-10=8.5, 30-end=5.085714285714286, 20-30=10.3>
Naturally that can't be accessed with c.10-20 as I don't believe hyphens are valid class variable names. So, how do you access these values?
You can use square brackets like you would with a hash:
obj["10-20"]
#=> 0
Of course, if most of the keys are not valid method names anyway, then you might as well just use a hash and not bother with an OpenStruct.
Related documentation: OpenStruct#[]

Can't convert string to integer ruby error when accessing Hash/Array

I am new to ruby. So I was trying to get data from my data array but I get a "can't convert String into Integer" error.
The way I am accessing data is
data["myobject"]
It seem that data is an array, not a hash.
I think data looks like this :
data=['foo', 'bar']
instead of looking like this :
data={'myObject'=>'foo', 'myObject2'=>'bar'}
So try to change data or retrieve data by its index
data[0]

How do I extract the hotel names from this object in ruby?

I'm getting data in this format, and need to extract the hotel names from it. I have no idea how to do this.
[
#<Hotel:0x007fba499de940 #data=#<CSV::Row "Hotel":"Dropp Inn" "City":"Cityville" >>,
#<Hotel:0x007fba499d7cf8 #data=#<CSV::Row "Hotel":"Bamboo Lodge" "City":"Cityton">>
]
Write as
# Assuming in your Hotel class, you have a reader method called `data`.
array.map { |hotel| hotel.data['Hotel'] }
This will work, as you have array of Hotel objects, I can see from your question. Now, each Hotel instance is having an instance variable #data, which is nothing but a CSV::Row object. Now each CSV::Row object has fields "Hotel", "City". Now to get those fields values, you need to use CSV::Row#[] method.

JSON deserialization query

I'm trying to deserialize a JSON string with the following syntax into C# classes, but I'm sort of puzzled on how to handle the dynamic nature of the "parent" object:
{"1":[{"id":"12139811","num":"37805729","date":"2012-01-30"},{"id":"12139812","num":"36911026","date":"2012-01-30"}],"2":[{"id":"12158366","num":"17582898","date":"2012-01-30"},{"id":"12207165","num":"38493538","date":"2012-01-30"}]}
Any help on what the classes should look like will be appreciated.
(Preferably the syntax to Deserialize would be something like
var objects = JsonConvert.DeserializeObject<List<MyObject>>(jsonString);
I think is beacuse your Json star with and identifier "1" instead of the array...
so you could try this
var objects = JsonConvert.DeserializeObject<Dictionay<Object,List<MyObject>>>(jsonString);

Resources