【问题标题】:CoreData JSON MappingCoreData JSON 映射
【发布时间】:2012-12-07 09:17:07
【问题描述】:

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

...
}

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

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

谢谢。

【问题讨论】:

    标签: ios json core-data mapping


    【解决方案1】:

    我会这样做......

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

    这是一个例子:

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

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

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

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

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

    【讨论】:

    • 是的,现在我正在尝试像这样重新格式化 JSON。 CoreData 会自动匹配具有这种格式的字段吗?那么“城市”表呢?如何匹配 city_id?
    • 首先阅读此内容:cimgf.com/2011/06/02/saving-json-to-core-data 并完全理解。您不希望您的 API 发生变化而导致应用程序混乱。
    • 如果您是 Core Data 的新手,您还应该查看 Magical Record 并阅读这篇文章以了解如何正确处理它 cimgf.com/2011/05/04/core-data-and-threads-without-the-headache
    • 谢谢。还有一个问题是;创建新的 coredata 对象时(我已同步 Cities 实体)如何将 city 与 Cities managedobject 匹配?
    • 我将添加一个唯一 ID,您可以使用它来获取 City 托管对象并设置 restaurant.city 关系。使用 Magical Record 获取就像:[City findFirstByAttribute:@"uid" withValue:idOfCity];
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多