【发布时间】:2015-12-01 10:25:10
【问题描述】:
我读了这个question 并没有得到答案
我有多个 JSON
路由序列
{
"checkpoint_id": 145,
"hash": "d5b335dac7c34ba1fd001268b80b21dd",
"is_end": true,
"is_forward_direction": true,
"next_checkpoint_id": 144,
"order": 0,
"path": [
"65.597615 57.153329",
"65.597668 57.153424",
"65.597659 57.153654",
"65.596790 57.153182",
"65.596679 57.153128",
"65.595181 57.152339",
"65.592321 57.150904",
"65.591666 57.150562"
],
"route_id": 87
},
.....
路线
{
"active_from": "2014-03-25",
"active_to": "2024-01-01",
"capacity_type": "",
"checkpoints_ids": [
319,
321,
338,
339,
340,
......
3355,
3375
],
"city_id": 1,
"description": "Областная библиотека - с/о Липовый остров",
"has_cars_for_disabled_persons": false,
"hash": "49587333a979a22d93d09e4313661eaa",
"id": 125,
"name": "156",
"route_type_id": 3
},
.......
检查点
{
"city_id": 2,
"code_number": "09080",
"description": "Большая, 183 в сторону Калинина",
"hash": "cd398f0d4a7baa5a47facb3476394d84",
"id": 1503,
"lat": 56.1307455464724,
"lon": 69.528181378059,
"name": "2 микрорайон",
"routes_ids": [
401,
873
]
},
.........
Entities picture 和 graph view of entities
我在循环中使用这样的东西从 json 数组创建对象:
+ (instancetype)instanceWithEntity:(NSEntityDescription*)entity inContext:(NSManagedObjectContext *)context fromJSONObject:(NSDictionary *)jsonData{
Route *route = [[Route alloc] initWithEntity:entity insertIntoManagedObjectContext:context];
route.id = [jsonData valueForKey:@"id"];
route.capacityType = [jsonData valueForKey:@"capacity_type"];
route.objectHash = [jsonData valueForKey:@"hash"];
route.sequenceDescription = [jsonData valueForKey:@"description"];
route.name = [jsonData valueForKey:@"name"];
return route;
}
还有主要问题:在这些情况下如何处理关系。我的应用程序在启动时开始为每个实体处理这个 json 文件。我问的原因是我的 json 没有完全嵌套,它只有来自其他实体的对象的 id 数组。那么如何处理呢?例如:
routeSequence.route = ?
这是指 JSON RouteSequence 的关系。我不能只在这里创建对象,我需要通过他的 id 引用已存在的对象(不能存在,因为这仍然没有下载和解析)
【问题讨论】:
标签: objective-c json core-data