I've spent many hours reading the documentation and tried out various sample projects etc. but I just can't solve my problems. As Restkit changed many APIs a few days ago much of the information provided and the tutorials is useless. The stuff that works does not help me with my problems.
In General, I don't understand when to use /person or /person.user or /:user or /person/user when doing the object mapping. I havent found anything about how to access certain JSON entries. Mybe someone can explain this to me.
I want to create objects out of my JSON Response. The Problem is using objects or arrays, nested in an object.
Here is the JSON that causes problems:
{
"result":
{
"persons":
[
{
"age": 20,
"name": "alice",
"user":
{
"id": 208727870
}
},
{
"age": 25,
"name": "bob",
"user":
{
"id": 308427890
}
}
]
}
}
For this sample I need to do the object mapping -> but how?
This mapping works:
{
"result":
[
{
"age": 20,
"name": "alice",
"user":
{
"id": 208727870
}
},
{
"age": 25,
"name": "bob",
"user":
{
"id": 308427890
}
}
]
}
RKObjectMapping* userMapping = [RKObjectMapping mappingForClass:[RKTUser class]];
[userMapping mapKeyPath:#"id" toAttribute:#"userID"];
RKObjectMapping* statusMapping = [RKObjectMapping mappingForClass:[RKTStatus class]];
[statusMapping mapKeyPath:#"age" toAttribute:#"age"]
[statusMapping mapKeyPath:#"name" toAttribute:#"name"]
[statusMapping mapRelationship:#"user" withMapping:userMapping];
statusMapping.rootKeyPath = #"result";
// user seems only to be a placeholder .. it does not matter, which string i write instead "user"
[objectManager.mappingProvider setObjectMapping:statusMapping forResourcePathPattern:#"/:user"];
What I finally want to do:
{
"result":
{
"id_str": "208727870",
"result_value_str": "123456",
"persons":
[
{
"age": 20,
"name": "alice",
"user":
{
"id": 208727870
}
},
{
"age": 25,
"name": "bob",
"user":
{
"id": 308427890
}
}
],
"friends":
[
{
"age": 18,
"name": "norton",
"user":
{
"id": 208457120
}
},
{
"age": 41,
"name": "edward",
"user":
{
"id": 378829810
}
}
]
}
}
Has someone an advice for me? (tutorials, links, hints…)
Best regards,
Steve
Related
I have a map, with multiple layers, some of the layers have an events field which may contain 0 or more event listings inside of it.
Some of events are nested deeper into the map, while others are closer to the top layer.
Here is what the graphql query result looks like:
{
"data": {
"users": [
{
"id": 16,
"friends": [
{
"senderId": 16,
"receiverId": 17,
"userByReceiverid": {
"id": 17,
"events": [],
"friends": [
{
"receiverId": 14,
"userByReceiverid": {
"id": 14,
"events": [
{
"id": 3,
"photoUrl": "none",
"name": "hello",
"date": "1982-06-27",
"startTime": "01:00:00+00",
"endTime": "02:00:00+00",
"fee": "$2.00",
"maxNumber": 10,
"ageRestriction": "none",
"about": "amazing",
"allowShare": false,
"private": false,
"timestamp": "2021-06-26T17:57:13.224383+00:00",
"senderId": 14
}
]
}
},
{
"receiverId": 20,
"userByReceiverid": {
"id": 20,
"events": []
}
}
]
}
}
],
"friendsByReceiverid": [
{
"senderId": 20,
"receiverId": 16,
"user": {
"id": 20,
"events": [],
"friendsByReceiverid": [
{
"receiverId": 20,
"user": {
"id": 14,
"events": [
{
"id": 3,
"photoUrl": "none",
"name": "hello",
"date": "1982-06-27",
"startTime": "01:00:00+00",
"endTime": "02:00:00+00",
"fee": "$2.00",
"maxNumber": 10,
"ageRestriction": "none",
"about": "amazing",
"allowShare": false,
"private": false,
"timestamp": "2021-06-26T17:57:13.224383+00:00",
"senderId": 14
}
]
}
},
{
"receiverId": 20,
"user": {
"id": 17,
"events": []
}
}
]
}
}
]
}
]
}
}
I want to search the Map and make a list that contains all the events that are unique, so no duplicates if possible.
How would I go about pulling out only the events field from a map at any given point?
Essentially what you need is a function (visitObject) that goes through each key of the a Map and recursively calls itself till all properties have been checked.
The function should also check for the possible types it can operate on and handle the data so you can call the visitObject function with the right arguments.
I believe the right data structure here would be a Set. In dart as Set is defined as A collection of objects in which each object can occur only once.
However Set a set falls back to the dart method of equality which is the == operator, so you would either need to use a calls for Events or use a package like BuiltValue which will generate the == operator for your value types.
Here is an example that can help you get started, I have left equality checking for you to determine what best and how you want to apply it.
final Set<Map<String, dynamic>> items = {};
void main() {
visitObject(data);
}
void visitObject(object) {
if (object is List) {
for(dynamic item in object) {
visitObject(item);
}
} else if (object is Map) {
if (object.containsKey('events')) {
var _events = object['events'];
if (_events is List && _events.isNotEmpty) {
_events.forEach((e) {
if (!items.contains(e)) {
items.add(e);
}
});
}
}
for (dynamic item in object.values) {
visitObject(item);
}
}
}
Additional Reading for equality:
==Operator
hashCode
BuiltValue
I have the JSON below in mongodb and would like write a bson.M filter to get a specific JSON in collection.
JSONs in collection:
{
"Id": "3fa85f64",
"Type": "DDD",
"Status": "PRESENT",
"List": [{
"dd": "55",
"cc": "33"
}],
"SeList": {
"comm_1": {
"seId": "comm_1",
"serName": "nmf-comm"
},
"comm_2": {
"seId": "comm_2",
"serName": "aut-comm"
}
}
}
{
"Id": "3fa8556",
"Type": "CCC",
"Status": "PRESENT",
"List": [{
"dd": "22",
"cc": "34"
}],
"SeList": {
"dnn_1": {
"seId": "dnn_1",
"serName": "dnf-comm"
},
"dnn_2": {
"seId": "dnn_2",
"serName": "dn2-comm"
}
}
}
I have written below the bson.M filter to select the first JSON but did not work because I do not know how to handle the map keys in the "SeList.serName". The keys comm_1, comm_2, dnn_1, etc could be any string.
filter := bson.M{"Type": DDD, "Status": "PRESENT", "SeList.serName": nmf-comm} // does not work because the "SeList.serName" is not correct.
I need help about how to select any JSON based on the example filter above.
I'm using loopback 3 to build a backend with mongoDB.
So i have 2 models: Object and Attachment. Object have a relation Embeds2Many to Attachment.
Objects look like that in mongoDB
[
{
"fieldA": "valueA1",
"attachments": [
{
"id": 1,
"url": "abc.com/image1"
},
{
"id": 2,
"url": "abc.com/image2"
}
]
},
{
"fieldA": "valueA2",
"attachments": [
{
"id": 4,
"url": "abc.com/image4"
},
{
"id": 5,
"url": "abc.com/image5"
}
]
}
]
The question is: how can i get Objects with attachments.id=4 over the RestAPI?
I have try with the where and include filter. But it didn't work. It look like, that this function is not implemented in loopback3, right?
I have found the solution. It only works on Mongodb, Cloudant and Memory database.
{
"filter": {
"where": {
"attachments.id": 4
}
}
}
I'm forced to use the aggregation framework and the project operation of Spring Data MongoDb.
What I'd like to do is creating an array of object as a result of a project operation.
Considering this intermediate aggregation result:
{
"processes": [
{
"id": "101a",
"assignees": [
{
"id": "201a",
"username": "carl93"
},
{
"id": "202a",
"username": "susan"
}
]
},
{
"id": "101b",
"assignees": [
{
"id": "201a",
"username": "carl93"
},
{
"id": "202a",
"username": "susan"
}
]
}
]
}
I'm trying to get for each process, all the assignee usernames and ids. Hence, what I want to obtain is something like this:
[
{
"results": [
{
"id": "201a",
"value": "carl93",
"parentObjectId": "101a"
},
{
"id": "202a",
"value": "susan",
"parentObjectId": "101a"
},
{
"id": "201a",
"value": "carl93",
"parentObjectId": "101b"
},
{
"id": "202a",
"value": "susan",
"parentObjectId": "101b"
}
]
}
]
To reach this goal I'm using 2 nested VariableOperators.mapItemsOf obtaining:
org.springframework.data.mapping.MappingException: Cannot convert [Document{{id= 201a, value= carl93, parentObjectId= 101a}}, Document{{id= 202a, value = susan, parentObjectId= 101a}}]
of type class java.util.ArrayList into an instance of class java.lang.Object!
Implement a custom Converter<class java.util.ArrayList, class java.lang.Object> and register it with the CustomConversions.
Here's the code that I'm currently using:
new ProjectionOperation().and(
VariableOperators.mapItemsOf("processes")
.as("pr")
.andApply(
VariableOperators.mapItemsOf("$pr.ownership.assignees")
.as("ass")
.andApply(aggregationOperationContext -> {
Document document = new Document();
document.append("id", "$$ass.id");
document.append("value", "$$ass.username");
document.append("parentObjectId", "$$pr.id");
return document;
})
)
).as("results");
The code produces this:
[
[
{
"id": "201a",
"value": "carl93",
"parentObjectId": "101a"
},
{
"id": "202a",
"value": "susan",
"parentObjectId": "101a"
}
],
[
{
"id": "201a",
"value": "carl93",
"parentObjectId": "101b"
},
{
"id": "202a",
"value": "susan",
"parentObjectId": "101b"
}
]
]
As you can see there are 2 nested arrays, [[],[]]. This is the reason why the exception is thrown.
Nevertheless what I want to obtain is just one array, adding all the objects in it (possibly without duplicates or null values). I've tried the addToSet operator and other aggregtion operators, without any success.
Use $reduce with $concatArrays to join the arrays.
new ProjectionOperation().and(
ArrayOperators.arrayOf("processes")
.reduce(ArrayOperators.ConcatArrays.arrayOf("$$value").concat(
VariableOperators.mapItemsOf("$$this.ownership.assignees")
.as("ass")
.andApply(aggregationOperationContext -> {
Document document = new Document();
document.append("id", "$$ass.id");
document.append("value", "$$ass.username");
document.append("parentObjectId", "$$this.id");
return document;
})
)).startingWith(Arrays.asList())
).as("results");
How to parse following JSON in iPhone. Key node_10947 may change each time.
Any Idea ?
{
"metadata": {
"node_10947": {
"nodeID": "10947",
"objectID": "11121",
"name": "Abc",
"classIdentifier": "folder",
"node_10948": {
"nodeID": "10948",
"objectID": "11122",
"name": "Abc_A",
"classIdentifier": "tag"
},
"node_10951": {
"nodeID": "10951",
"objectID": "11123",
"name": "Abc_b",
"classIdentifier": "tag"
},
"node_10952": {
"nodeID": "10952",
"objectID": "11125",
"name": "Abc_c ",
"classIdentifier": "tag"
}
},
"node_170": {
"nodeID": "170",
"objectID": "196",
"name": "XYZ",
"classIdentifier": "folder",
"node_179": {
"nodeID": "179",
"objectID": "206",
"name": "XYZ_a",
"classIdentifier": "tag"
},
"node_180": {
"nodeID": "180",
"objectID": "207",
"name": "XYZ_b",
"classIdentifier": "tag"
},
"node_182": {
"nodeID": "182",
"objectID": "209",
"name": "XYZ_c",
"classIdentifier": "tag"
}
}
}
}
I am having the above json and key node may change at any time pls suggest me how can I parse a json whose node is changing dynamically at any time ???
Use the framework as pointed by Ehab Amer.
For parsing you can use this code snippest
SBJsonParser *sbjasonObj=[[[SBJsonParser alloc]init] autorelease];
NSMutableDictionary *categoriesContainer = [[NSMutableDictionary alloc]initWithDictionary:[sbjasonObj objectWithString:result]];
where result is JSON as your.
If it is possible for you to modify that json then include one more key in the json which will hold value of current key like following:
{
"metadata": { currentKey: "node_10947}
And use that key to access values defined by node_10947.
You can use some framework like JSONKit for parsing json as well.