【问题标题】:Using Coredata to save Json dictionary使用Coredata保存Json字典
【发布时间】:2018-02-18 00:44:28
【问题描述】:

我是 Coredata 的新手,在我的项目中,我必须从 API 解析 json,我需要使用核心数据保存解析的 json 字典。我创建了一个名为 Json 的新实体并添加了一个字符串类型的属性,但是当我喜欢它时,它会显示一个错误,即“属性的值类型不可接受:属性 =“信息”;所需类型 = NSString

但我无法选择属性类型 NSString ,以下是我使用核心数据保存 json 字典的代码。请为此建议我一个解决方案。

let session = URLSession.shared.dataTask(with: jsonRequest) { (data, response, error) in

        if let res = response {

            print(res)
        }

        if let data = data {

            do {

                let json = try JSONSerialization.jsonObject(with: data, options: []) as! NSDictionary

               let NewData = NSEntityDescription.insertNewObject(forEntityName: "Json", into: context)

                NewData.setValue(json.value(forKey:"result")  , forKey:"information")

                do {

                    try context.save()
                    print("SAVED")
                }

                catch {

                    print(error)
                }

            }

            catch {

                print(error)
            }
        }
    }

    session.resume()

我的json数据如下

{

 "result": [


        {
            "category_name": "today"
        },

        {
            "category_name": "TechCrunch",
            "category_image": "1500899368.jpg"
        },

        {
            "category_name": "Science Daily",
            "category_image": "1500899358.jpg"
        },

        {
            "category_name": "ShowerThoughts",
            "category_image": "1500899351.jpg"
        },

        {
            "category_name": "BBC",
            "category_image": "1500899343.png"
        },


        {
            "category_name": "Goal.com",
            "category_image": "1500899336.jpg"
        },
        {
            "category_name": "Today I Learned",
            "category_image": "1500899329.jpg"
        },


        {
            "category_name": "Times Of India",
            "category_image": "1500899321.png"
        },


        {
            "category_name": "MIT Technology Review",
            "category_image": "1500899313.png"
        },


        {
            "category_name": "Business Insider",
            "category_image": "1500458239.jpg"
        },


        {
            "category_name": "coindesk",
            "category_image": "1500544039.jpg",
            "data": [
                {
                    "category_id": "34",
                    "news_id": "215",
                    "news_quote": "",
                    "title": "Bitcoin drama",
                    "image": "2017-07-20/1500544487.jpg",
                    "date": "2017-09-07 04:10:10"
                },


                {
                    "category_id": "34",
                    "news_id": "225",
                    "news_quote": "",
                    "title": "Bitcoin Price Surges",
                    "image": "2017-07-21/1500629251.jpg",
                    "date": "2017-09-07 04:10:10"
                },


                {
                    "category_id": "34",
                    "news_id": "229",
                    "news_quote": null,
                    "title": "Bitcoin Prices Gain",
                    "image": "2017-07-22/1500635952.jpg",
                    "date": "2017-09-07 04:10:10"
                }
            ]
        }
    ],
    "response": {
        "status": "success",
        "status_code": 1,
        "message": "success"
    }
}

【问题讨论】:

  • 你能显示你得到的 json 吗?
  • @3stud1ant3 更新了我的问题
  • 似乎 result 不是 String ,而是一个数组
  • @3stud1ant3 那么保存json字典的解决方案是什么

标签: ios core-data swift3


【解决方案1】:

首先不要在 Swift 中使用 Foundation NSArrayNSDictionary

result 的值是 [String:Any] 字典的数组。使用循环创建新记录并将类别名称分配给information 属性。

...

do {

    let json = try JSONSerialization.jsonObject(with: data) as! [String:Any]
    if let result = json["result"] as? [[String:Any]] {
        for category in result {
            let newData = NSEntityDescription.insertNewObject(forEntityName: "Json", into: context)
            newData.information = category["category_name"] as! String
            // or if the entity is not a `NSManagedObject` subclass
            // newData.setValue(category["category_name"] as! String, forKey:"information")
        }

        try context.save()
        print("SAVED")
    }
} catch {
    print(error)
}
  • 要将值也保存到category_image,您需要实体中的第二个属性。
  • 要将 JSON 保存在键 data 中,您需要与第一个实体有关系的第二个实体。

一些注意事项:

  • 变量名应该以小写字母开头。
  • JSONSerialization 行中的options 参数可以省略。
  • 循环后只保存一次上下文。
  • 只使用一个do catch 块。

【讨论】:

  • @vadian 谢谢你的回答
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-02
  • 1970-01-01
相关资源
最近更新 更多