I have given JSON and cannot parse partial data. It seems dictionary into dictionary:
{
"products": [
{
"id": 6796,
"title": "my title",
"description": "desc",
"code": "12345",
"valueType": "null",
"discounts": [
{
"minPrice": null,
"maxPrice": null,
"value": 20,
"avail": false
}
]
}
]
}
I am using the latest version of RESTKit but I cannot properly parse under discounts.
my RestKit settings are:
responseMapping.addAttributeMappingsFromDictionary([
"id" : "id",
"code" : "code",
"title" : "title",
"valueType" : "valueType",
"description" : "desc",
"discounts.minPrice" : "minPrice",
"discounts.maxPrice" : "maxPrice",
"discounts.value" : "value",
"discounts.avail" : "avail",
])
but all values below discounts always return Nil. What I am doing wrong?
You can't directly map using discounts.XXX because discounts is an array and you have no way to index into that array and extract a single value.
You either need to change the source JSON to compress the values out of the dictionary, or create a custom object that you can map each item in the discounts array to.
Technically you could map the whole discounts array, which would give you an array of dictionaries, that you could then unpack in the setter method, but the array of custom objects is usually a better approach.
Related
In a JSON model coming from a REST api I have a field that is supposed to be a Dictionary (a map, hashtable, call it whatever you want), e.g.:
"moduleMap": {
"677e55b2-d80c-4b32-bcbb-e99074fbfcf6": {
"id": "677e55b2-d80c-4b32-bcbb-e99074fbfcf6",
"startTime": 1496054599227,
"status": "ACTIVE"
},
"c20acde2-639f-4cb7-9b90-6d8d24c78166": {
"id": "c20acde2-639f-4cb7-9b90-6d8d24c78166",
"startTime": 1496054598997,
"status": "UNAVAILABLE"
}
}
As I understand RestKit (I am a newbie), usually this is mapped to an object in Swift. However, since the keys in this map are arbitrary uuids, I cannot write a class with these properties.
Can anybody point me to a direction of how to get this map into a Swift dictionary using RestKit mapping?
I would like to map it to var moduleMap: [String:DomainObject], or at least var moduleMap: NSMutableDictionary. Also, I need it to be mappable back to the same JSON.
I have following REST API response:
"items":
[
{
"empid": "1234",
"name": "Santosh",
"hiredby": "Mark",
"date": "2017-01-31,00:19:41 PST",
},
{
"empid": "5678",
"name": "Kumar",
"hiredby": "Bob",
"date": "2017-01-31,08:30:31 PST"
}
]
My query is : - How do i get empid based on querying name as Kumar.
For example: I need to find "Kumar" name and get his empid. (that is, search by name and get his empid as response) I'm able to get the response and store it in Response object. but, from response object how can i traverse and query to get the required value.
Also,
I tried by retrieving as:
String name = get(REST_ENDPOINT).then().body("items.name",hasItems("Kumar")).extract().path("items.empid").toString();
when i print the response i get collection of the empid like [1234,5678], where as my expectation is to get only 5678.
Do I need to parse via JSONArray and JSONObject and iterate the response?
Please suggest.
You can use something like this
response1.jsonPath().getList("collect { it.credentials.findAll { it.credentialType == 'Ban User Name'}.credentialId }.flatten()")
I am trying to read a list of objects
[
{
"name" : "Jhone",
"age" : 25
},
{
"name" : "Chris",
"age" : 24
}
]
How to handle that types of request?
You can loop through the array to access each object by its key. Assuming that array of objects is set to a variable called array
array.each { |element| puts element["name"] }
Btw, the objects are incorrectly formatted. You need a comma after each name value.
Here is the file I'm parsing: https://raw.githubusercontent.com/pahnev/AirportJSON/master/CountriesAndAirports.json
{
"Airport": [
{
"AirportID": "1",
"Name": "Goroka",
"City": "Goroka",
"Country": "Papua New Guinea",
"IATA-FAA": "GKA",
"ICAO": "AYGA",
"Latitude": "-6081689",
"Longitude": "145391881",
"Altitude": "5282",
"Timezone": "10",
"DST": "U",
"tzdbtimezone": "Pacific/Port_Moresby"
},
{
"AirportID": "2",
"Name": "Madang",
"City": "Madang",
"Country": "Papua New Guinea",
"IATA-FAA": "MAG",
"ICAO": "AYMD",
"Latitude": "-5207083",
"Longitude": "1457887",
"Altitude": "20",
"Timezone": "10",
"DST": "U",
"tzdbtimezone": "Pacific/Port_Moresby"
},
....
I'm trying to create a list of the countries based on it, and then from that list, the airports in that country.
I know how to do all this and have done it with dummy data.
The problem is the JSON file as it has list of airports, which have the country name inside them. So when I try to parse this I get multiple values of one country.
_countryList = [json valueForKeyPath:#"Airport.Country"];
With this code I get list looking something like this:
Papua New Guinea
Papua New Guinea
Papua New Guinea
etc...
So can I somehow parse this and combine all same names of one country? Or do I have to heavily modify this database?
After getting a list of countries without duplicates I would like to be able to select a country from that and get the airports that are in that country.
So Papua New Guinea would show 6 airports, Greenland 4, and so on.
You can move your data through an NSSet to remove the duplicates -
NSArray *uniqueCountries=[[NSSet setWithArray:_countryList] allObjects];
Given a country you can then get the airports using filteredArrayUsingPredicate -
NSString *taregtCountry=#"Papua New Guinea";
NSPredicate *countryPredicate=[NSPredicate withFormat:#"Airport.Country==%#",targetCountry];
NSArray *airportsInCountry=[json filteredArrayUsingPredicate:countryPredicate];
An easy way to do this is to build a dictionary of arrays.
If [json valueForKeyPath:#"Airport.Country"] returns to you "Papua New Guinea", you can ask the dictionary to return to you the object for the key "Papua New Guinea". If you get back something (Array of airports), simply add the airport to that array. Otherwise you allocate a new array with that airport and set it to the dictionary under the country key.
The keys for the dictionary are the countries and the value for each key would be the array of airports. Since a dictionary does not hold duplicate keys, your data structure would match what you are trying to accomplish.
I am having trouble understanding how to Map location Data stored in Mongo DB with RestKit.
Here is the data I will be mapping in JSON
{ "name" : "TestPoint2", "media_resource" : "tester", "added" : ISODate("2012-10-10T23:00:00Z"), "loc" : { "type" : "Point", "coordinates" : [ -33.1, 12.54 ] }, "comments" : [ ], "tags" : [ ]}
The main point I am struggling with is mapping the "coordinates" of "loc"
To begin with, I'd treat the coordinates as they are represented in the JSON and store them as numbers in an array. If your object / entity has an array property / transformable attribute then you can just map the coordinates into that wholesale. You can then add accessor methods / transient attribute(s) which access that coordinate information and provide a nicer interface to get the lat/long than indexing into an array.