单击submit按钮,我的文本字段中的数据和其他一些数据将被转换为json对象,如…
let categoryName = self.categoryTextField.text
let categoryId = self.categoryID
let category_json: [String: [String:Any]] = [
"categoryDetails": [
"category_name": categoryName,
"category_id": categoryId
]
]
if let data = try? JSONSerialization.data(withJSONObject: category_json, options: .prettyPrinted),
let str = String(data: data, encoding: .utf8) {
print(str) // `str` gives the json object
self.categoryStrToPass = str
}现在,self.categoryStrToPass被赋值给另一个json对象,最后被添加到像…这样的字符串数组中
let productID = self.prodID
let sellingPrice = self.mrpTextField.text
let categoryJSON = self.categoryStrToPass
let jsonObject: [String: [String:Any]] = [
"prodDetails": [
"product_id": productID,
"selling_price": sellingPrice,
“category_json”: categoryJSON
]
]
if let data = try? JSONSerialization.data(withJSONObject: jsonObject, options: .prettyPrinted),
let str = String(data: data, encoding: .utf8) {
print(str)
self.jsonStrToPass = str
self.jsonStringArray.append(self.jsonStrToPass)
}现在,我像…一样将jsonStringArray存储到核心数据
_product?.setValue(self.jsonStringArray, forKey:
"productJsonArray") // productJsonArray is an attribute of type Transformable and Custom class type Array<String>它是这样被获取的.
if let jsonObjArr = result.value(forKey: "productJsonArray") as?
Array<NSString> {
print(jsonObjArr)
} 现在我已经在两个不同的实例上提交了数据,这意味着在获取的同时打印jsonObjArr,它应该在一个数组中有两个不同的json对象,如下所示。
[{
"prodDetails" : {
"product_id" : "0",
"category_json" : "{\n \"categoryDetails\" : {\n \"category_id\" : \"0\",\n \"category_name\" : \"prodCAT\"\n }\n}",
"selling_price" : "500",
}
}
{
"prodDetails" : {
"product_id" : "1",
"category_json" : "{\n \"categoryDetails\" : {\n \"category_id\" : \"0\",\n \"category_name\" : \"CATNEW\"\n }\n}",
"selling_price" : "1000",
}
}]但是,打印jsonObjArr会给出这个…在像这样的两个不同的数组中...
[{
"prodDetails" : {
"product_id" : "0",
"category_json" : "{\n \"categoryDetails\" : {\n \"category_id\" : \"0\",\n \"category_name\" : \"prodCAT\"\n }\n}",
"selling_price" : "500",
}
}]
[{
"prodDetails" : {
"product_id" : "1",
"category_json" : "{\n \"categoryDetails\" : {\n \"category_id\" : \"0\",\n \"category_name\" : \"CATNEW\"\n }\n}",
"selling_price" : "1000",
}
}]如何在一个数组中获得多个json对象?
发布于 2018-02-09 20:07:54
您可以将String: Any类型的对象添加到数组中,如下所示
let firstCategoryName = "first"
let firstCategoryId = 1
let firstCategory = [
"category_name": firstCategoryName,
"category_id": firstCategoryId
] as [String : Any]
let secondCategoryName = "second"
let secondCategoryId = 2
var category_json = [[String:Any]]()
category_json.append(firstCategory)
let secondCategory = [
"category_name": secondCategoryName,
"category_id": secondCategoryId
] as [String : Any]
category_json.append(secondCategory)
print(category_json)然后序列化数组
发布于 2018-02-09 21:06:14
Swift 4.0:
let firstObj = ["prodDetails": [
"product_id": 5,
"selling_price": 6,
]]
let secondObj = ["prodDetails1": [
"product_id1": 5,
"selling_price1": 6,
]]
let jsonObject = jsonStringArray.addingObjects(from: [firstObj,secondObj])
if let data = try? JSONSerialization.data(withJSONObject: jsonObject, options: .prettyPrinted),
let str = String(data: data, encoding: .utf8) {
print(str) //prints array of dictionaries
}发布于 2018-02-09 21:47:38
category_json和jsonObject是同一类型的。
您需要了解的内容:
(NS)String <== String(data:encoding)或data(encoding:) ==> (NS)Data
应用于特定的字符串/数据: JSON:
JSON强化的<== String(data:encoding)或data(encoding:) ==> JSON数据
Swift阵列/ Swift (以及其他符合JSON标准的) <== (NS)JSONSerialization.jsonObject(withData:, options:)或(NS)JSONSerialization.data(withJSONObject:, options:) ==> JSON数据
你不能像那样附加两个JSON Stringified,你至少需要在顶层有一个数组。
因此,让我们用伪代码将这些点连接起来(根本不能确定方法名是没有错误的)
let currentData = self.jsonStrToPass.data(encoding: .utf8)
let current = JSONSerialization.jsonObject(with:currentData, options:[]) as [String:[String:Any]]
let finalArray : [[String:[String:Any]]]()
finalArray.append(current)
finalArray.append(jsonObject)
let finalData = JSONSerialization.data(withJSONObject:finalArray, options:[])
let finalString = String(data:finalData, encoding:.utf8)这就是逻辑。我没有做if let,try/catch等。
我认为在要传递的数据之间传递Any (对于Swift Array/Dictionary而不是String)可能更好。编辑它们可能会更简单(追加等)而不是字符串。
https://stackoverflow.com/questions/48704884
复制相似问题