CoreData JSON映射

时间:2012-12-19 22:21:57

标签: ios json core-data mapping

在我的api中我有这样的JSON输出;

{
"restaurants": [{
    "Restaurant": {
        "id": "3",
        "name": "...",
        "slug": "...",
        "phone": "2161234567",
        "address": "...",
        "city_id": "34",
        "county_id": "426",
        "discount_type_id": null,
        "discount": "",
        "people_limit": "2",
        "reservations_required": false,
        "lat": "41.015608",
        "lon": "28.934834",
        "about": "...",
        "logo": "...",
        "logo_dir": "3",
        "photo_1": "...",
        "photo_2": null,
        "photo_3": null,
        "photo_dir": "3",
        "created": "2012-11-30 16:30:21",
        "modified": "2012-11-30 20:43:50",
        "deleted": true
    },
    "AvailabilityRestriction": [{
        "id": "1",
        "title": "...",
        "AvailabilityRestrictionRestaurant": {
            "id": "28",
            "restaurant_id": "3",
            "availability_restriction_id": "1"
        }
    }],
    "CuisineTag": [{
        "id": "3",
        "name": "...",
        "CuisineTagRestaurant": {
            "id": "47",
            "restaurant_id": "3",
            "cuisine_tag_id": "3"
        }
    }]
},

...
}

餐厅有很多cuisine_tags和可用性限制。还有一个城市表...同步它们的最佳方式是什么?

我必须首先同步cities,cuisine_tags和availability_restrictions,然后在创建餐馆时我会匹配它们吗?这是正确的方法吗?

感谢。

1 个答案:

答案 0 :(得分:0)

我会这样做....

您应该在餐厅对象中拥有所有“餐厅”属性(即restirctions,cuisine-tags)。

以下是一个例子:

{
    "Restaurants" : [
        { 
            "id" : "3",
            "name" : "...",
            "slug" : "...",
            "phone" : "2161234567",
            "address" : "...",
            "city_id" : "34",
            "county_id" : "426",
            "discount_type_id" : null,
            "discount" : "",
            "people_limit" : "2",
            "reservations_required" : false,
            "lat" : "41.015608",
            "lon" : "28.934834",
            "about" : "...",
            "logo" : "...",
            "logo_dir" : "3",
            "photo_1" : "...",
            "photo_2" : null,
            "photo_3" : null,
            "photo_dir" : "3",
            "created" : "2012-11-30 16:30:21",
            "modified" : "2012-11-30 20:43:50",
            "deleted" : true,
            "AvailabilityRestrictions" : [
                {
                    "id" : "1",
                    "title" : "..."
                },
                {
                    "id" : "2",
                    "title" : "..."
                }
            ],
            "CuisineTags" : [
                {
                    "id" : "3",
                    "name" : "..."
                }
            ]
        }
    ]
}

还摆脱了“餐厅”对象,这是不必要的。如果我有一个名为Restaurants的数组。我想只在其中放入/送餐馆。

我认为你的问题更多,但我无法理解你的需求。您是在谈论如何在Core Data中建立它们之间的关系吗?

如果你想设置cruisine标签和城市之间的关系,或者我认为这是最好的方法,因为你可以循环通过JSON响应,如果其中一个属性是一个列表,你可以确定列表并获取正确的托管对象并创建关系。

为了清楚起见,您应该在不同的请求中保留这些城市,预定义的巡航标签。您希望保持API简单灵活。