【问题标题】:How to generate a JSON file to preload and import to coreData?如何生成 JSON 文件以预加载和导入 coreData?
【发布时间】:2015-09-19 01:17:16
【问题描述】:

我正在关注 raywenderlich.com 上的热门教程来预加载核心数据。 这是我的 JSON 文件:

[
{
    "countDescription": "###",
    "countId": "###",
    "states": [
        {
            "stateDesc": "###",
            "stateId": "###",
            "cities": [
                {
                    "cityDescription": "###",
                    "cityId": "###",
                    "destinations": [
                        {
                            "destDescription": "###",
                            "destId": "###"
                        },
                        {
                            "destDescription": "###",
                            "destId": "###"
                        }
                    ]
                },
                {
                    "cityDescription": "###",
                    "cityId": "###",
                    "destinations": [
                        {
                            "destDescription": "###",
                            "destId": "###"
                        },
                        {
                            "destDescription": "###",
                            "destId": "###"
                        }
                    ]
                }
            ]
        },
        {
            "stateDesc": "###",
            "stateId": "###",
            "cities": [
                {
                    "cityDescription": "###",
                    "cityId": "###",
                    "destinations": [
                        {
                            "destDescription": "###",
                            "destId": "###"
                        },
                        {
                            "destDescription": "###",
                            "destId": "###"
                        }
                    ]
                }
            ]
        }
    ]
},
{
    "countDescription": "###",
    "countId": "###",
    "states": [
        {
            "stateDesc": "###",
            "stateId": "###",
            "cities": [
                {
                    "cityDescription": "###",
                    "cityId": "###",
                    "destinations": [
                        {
                            "destDescription": "###",
                            "destId": "###"
                        }
                    ]
                }
            ]
        }
    ]
}
]

我的问题是如何在 excel 中执行此操作,然后我可以将其保存为 CSV 文件并使用 converter 获取我的 JSON。

在转换器网站中,我使用了 JSON-to-CSV 选项,但生成的 CSV 文件只为我提供实体 Country 的列,然后每一行的其他所有内容都在下一列中,其他属性的名称消失。

如果我使用另一个 source 那就太好了,它看起来就像我想的那样:

    "countDescription","countId","states__stateDesc","states__stateId","states__cities__cityDescription","states__cities__cityId","states__cities__destinations__destDescription","states__cities__destinations__destId"
"###","###","###","###","###","###","###","###"
"","","","","","","###","###"
"","","","","###","###","###","###"
"","","","","","","###","###"
"","","###","###","###","###","###","###"
"","","","","","","###","###"
"###","###","###","###","###","###","###","###"

但是,一旦我对表格进行了更改,然后继续将其保存为 CSV 文件,格式就不同了,这是什么原因?

英语是我的第二语言,我希望我的问题不是太难理解,如果你们不介意我问,一对多对一对多对一是否正确-数据模型的多对多?我确实设法将 JSON 文件预加载到核心数据中。

【问题讨论】:

    标签: ios json csv core-data


    【解决方案1】:

    我认为快速编写自己的解析器并解析 CSV 文件要容易得多。至少这是我的工作流程,并且被证明是非常有效的。以字符串的形式读入文件,分成除以\n的数组,循环遍历行,每行循环遍历字段,以列标题为键构造字典。

    然后使用NSJSONSerialization 类方法生成您的JSON 数据(提示:使用.PrettyPrinted 选项以提高可读性)。然后,您可以使用 String(data:encoding:) 从数据中生成 String 并将其保存到文件中(或将其转储到控制台并复制粘贴)。

    【讨论】:

    • 感谢@Mundi 的回复,我是初学者,希望有一个快速的解决方案,但我需要做更多的研究。我会尝试link,然后我肯定会带着更多问题回来。再次感谢。
    猜你喜欢
    • 2015-12-22
    • 2020-03-25
    • 1970-01-01
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    • 2020-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多