0

在我的 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"
        }
    }]
},

...
}

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

我必须先同步城市、美食标签和可用性限制,然后在创建餐厅时我会匹配它们?这是正确的方法吗?

谢谢。

4

1 回答 1

0

我会这样做......

您应该在餐厅对象中拥有所有“餐厅”属性(即限制、美食标签)。

这是一个例子:

{
    "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" : "..."
                }
            ]
        }
    ]
}

也摆脱“餐厅”对象,它是不需要的。如果我有一个名为“餐厅”的数组。我只想在其中/从其中放置/获取餐厅。

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

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

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

于 2012-12-19T22:51:24.673 回答