gpt4 book ai didi

json - 从 NSMutableArray 快速创建 jsonArray

转载 作者:行者123 更新时间:2023-11-28 11:40:42 25 4
gpt4 key购买 nike

我试过了。但我没有得到 jsonArray,我不知道我该怎么做。我想将 jsonArray 作为字符串。你能帮帮我吗?

public static func createInkitchenJson(additionId : Int64, orderId : Int64)
{
let dicArray : NSMutableDictionary = NSMutableDictionary();
let nsArray : NSMutableArray = NSMutableArray();

for basket in BasketStatic.baskets
{
let dicObject : NSMutableDictionary = NSMutableDictionary();
let model = basket.Product;
dicObject.setValue(String(additionId), forKey: JsonHeader.ADDITION_ID);
dicObject.setValue(String(orderId), forKey: JsonHeader.ORDER_ID);
dicObject.setValue(BasketStatic.table?.getTableName(), forKey: JsonHeader.TABLE_NAME);
dicObject.setValue(model.Id, forKey: JsonHeader.REQUEST_ID);
dicObject.setValue(String(basket.RowCount), forKey: JsonHeader.ITEM_COUNT);
dicObject.setValue(model.Name, forKey: JsonHeader.REQUEST_NAME);
dicObject.setValue(model.getExtraToString(), forKey: JsonHeader.EXTRAS);
dicObject.setValue(basket.getType(), forKey: JsonHeader.REQUEST_TYPE);
dicObject.setValue(EMPTY_FIELD, forKey: JsonHeader.MENU_UNIQUE);
dicObject.setValue(EMPTY_FIELD, forKey: JsonHeader.MENU_NAME);
dicObject.setValue(EMPTY_FIELD, forKey: JsonHeader.MENU_ID);
dicObject.setValue(String(basket.RowPrice), forKey: JsonHeader.PRICE);
dicObject.setValue(WaiterModel.WAITER_ID, forKey: JsonHeader.CREATED_BY);
nsArray.add(dicObject);
}

// here i want to convert NsMutableArray to jsonArray
// jsonArray convert to string

}

最佳答案

最好是

let arr = [Item(/////),,,,,]
let data = try? JSONEncoder().encode(arr)
let str = String(data: data, encoding: .utf8)

struct Item:Decodable {

// add properties

}

let data = try? JSONSerialization.data(withJSONObject: dic, options: [])

let str = String(data: data, encoding: .utf8)

关于json - 从 NSMutableArray 快速创建 jsonArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53724603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com