将NSDictionary转换为JSON(Converting NSDictionary to JSON)

编程入门 行业动态 更新时间:2024-10-28 19:19:52
将NSDictionary转换为JSON(Converting NSDictionary to JSON)

我有一个Core Data实体Checkout,它只有一个属性可以转换并设置为[String:Any]:

cart : [String:Any]

下面的函数用于从Core Data中检索值:

func getCheckOutItems()-> [NSDictionary]{ let request : NSFetchRequest<Checkout> = Checkout.fetchRequest() request.propertiesToFetch = ["cart"] request.resultType = .dictionaryResultType var checkout : [NSDictionary]! var cart : [String:Any]! do{ checkout = try context.fetch(request) }catch{ print ("Error fetching data") } return checkout }

其目的是将此查询的结果添加到包含使用此函数的其他键的JSON字符串中:

var cart = [String:Any]() cart["cart"] = getCheckOutItems() cart["telco"] = "something" cart["fcm_token"] = "tokden" cart["email"] = "email" let jsonData = try? JSONSerialization.data(withJSONObject: cart, options: []) let jsonString = String(data: jsonData!, encoding: .utf8) print("Printing JSON \(jsonString)")

这是为了产生像这样的json结果:

{ "fcm_token": "tokden", "cart": [{ "entity": 1, "quantity": 1, "id": 428, "price": 80, "date": "30 Jan 2018", "seat_no": 20, "type": "bus", "item": "STANDARD", "schedule_id": 132 }, { "entity": 1, "quantity": 1, "id": 1539, "price": 110, "date": "30 Jan 2018", "seat_no": 7, "type": "bus", "item": "EXECUTIVE", "schedule_id": 131 }, { "entity": 1, "quantity": 1, "id": 282, "price": 40, "date": "30 Jan 2018", "seat_no": 1, "type": "bus", "item": "STANDARD", "schedule_id": 114 } ], "telco": "something", "email": "email" }

主要问题是我需要消除突出显示为“购物车”的核心数据中的密钥:{为了获得上述格式的JSON。 但是这已经证明非常难以实现并且难以实现!

{ "fcm_token": "tokden", "cart": [{ **"cart": {** "entity": 1, "quantity": 1, "id": 428, "price": 80, "date": "30 Jan 2018", "seat_no": 20, "type": "bus", "item": "STANDARD", "schedule_id": 132 } }, { "cart": { "entity": 1, "quantity": 1, "id": 1539, "price": 110, "date": "30 Jan 2018", "seat_no": 7, "type": "bus", "item": "EXECUTIVE", "schedule_id": 131 } }, { "cart": { "entity": 1, "quantity": 1, "id": 282, "price": 40, "date": "30 Jan 2018", "seat_no": 1, "type": "bus", "item": "STANDARD", "schedule_id": 114 } }], "telco": "something", "email": "email" }

我如何格式化这个结果,以消除“购物车”键以格式化json,如前所述。 我很快就会觉得这个问题很难解决。

I have a Core Data entity Checkout that has just one attribute which is transformable and set as [String:Any]:

cart : [String:Any]

The function below is used to retrieve the value from Core Data:

func getCheckOutItems()-> [NSDictionary]{ let request : NSFetchRequest<Checkout> = Checkout.fetchRequest() request.propertiesToFetch = ["cart"] request.resultType = .dictionaryResultType var checkout : [NSDictionary]! var cart : [String:Any]! do{ checkout = try context.fetch(request) }catch{ print ("Error fetching data") } return checkout }

The aim is to add the results of this query to a JSON string that also contains other keys using this function:

var cart = [String:Any]() cart["cart"] = getCheckOutItems() cart["telco"] = "something" cart["fcm_token"] = "tokden" cart["email"] = "email" let jsonData = try? JSONSerialization.data(withJSONObject: cart, options: []) let jsonString = String(data: jsonData!, encoding: .utf8) print("Printing JSON \(jsonString)")

This is in order to produce a json result like this:

{ "fcm_token": "tokden", "cart": [{ "entity": 1, "quantity": 1, "id": 428, "price": 80, "date": "30 Jan 2018", "seat_no": 20, "type": "bus", "item": "STANDARD", "schedule_id": 132 }, { "entity": 1, "quantity": 1, "id": 1539, "price": 110, "date": "30 Jan 2018", "seat_no": 7, "type": "bus", "item": "EXECUTIVE", "schedule_id": 131 }, { "entity": 1, "quantity": 1, "id": 282, "price": 40, "date": "30 Jan 2018", "seat_no": 1, "type": "bus", "item": "STANDARD", "schedule_id": 114 } ], "telco": "something", "email": "email" }

The main issue is that I need to eliminate the key from core data which is highlighted as "cart": { in order to get the JSON in the format above. But this has proven very elusive and difficult to achieve!

{ "fcm_token": "tokden", "cart": [{ **"cart": {** "entity": 1, "quantity": 1, "id": 428, "price": 80, "date": "30 Jan 2018", "seat_no": 20, "type": "bus", "item": "STANDARD", "schedule_id": 132 } }, { "cart": { "entity": 1, "quantity": 1, "id": 1539, "price": 110, "date": "30 Jan 2018", "seat_no": 7, "type": "bus", "item": "EXECUTIVE", "schedule_id": 131 } }, { "cart": { "entity": 1, "quantity": 1, "id": 282, "price": 40, "date": "30 Jan 2018", "seat_no": 1, "type": "bus", "item": "STANDARD", "schedule_id": 114 } }], "telco": "something", "email": "email" }

How can I format this result such that the "cart" key is eliminated to format the json as indicated earlier. I am very new to swift and it seems really difficult to wrap my mind around this problem.

最满意答案

你可以使用flatMap 。 尝试这个。

let cartItems = getCheckOutItems() as? [[String:Any]] var cart = [String:Any]() cart["cart"] = cartItems?.flatMap({ $0.flatMap { $0.value } }) cart["telco"] = "something" cart["fcm_token"] = "tokden" cart["email"] = "email" let jsonData = try? JSONSerialization.data(withJSONObject: cart, options: []) let jsonString = String(data: jsonData!, encoding: .utf8) print("Printing JSON \(jsonString)")

You can use flatMap. Try this.

let cartItems = getCheckOutItems() as? [[String:Any]] var cart = [String:Any]() cart["cart"] = cartItems?.flatMap({ $0.flatMap { $0.value } }) cart["telco"] = "something" cart["fcm_token"] = "tokden" cart["email"] = "email" let jsonData = try? JSONSerialization.data(withJSONObject: cart, options: []) let jsonString = String(data: jsonData!, encoding: .utf8) print("Printing JSON \(jsonString)")

更多推荐

本文发布于:2023-07-14 20:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1107170.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   NSDictionary   Converting   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!