我被困了太久了。我必须将此数组发送为字符串:“ [{{\“ packageid \”:\“ 1 \”},{\“ packageid \”:\“ 1 \”},{\“ packageid \ \”:\ \ \ \“:\”:\ “ 3 \”}]”
等。这里的值正在插入,并且可以是任何整数。
因为我无法声明任何格式上的任何变量,例如让pjson = {\“ packageid \”:\“ 1 \”}
,所以我决定以这种方式创建一个字符串: ling pjson = “ {{\” packageid \“:\” 1 \“}”
,用于循环im以以下方式添加数据:
for package in packages {
if let pId = package.packageId {
let pjson = "{\"packageId\":\"\(pId)\"}"
packageJson.append(pjson as Any)
}
}
在此之后,我将获得这样的数组:
packageJson = ["{\"packageId\":\"1\"}", "{\"packageId\":\"2\"}", "{\"packageId\":\"3\"}", "{\"packageId\":\"4\"}", "{\"packageId\":\"5\"}", "{\"packageId\":\"6\"}"]
现在我想转换我的 packagejson
to此格式:
packageJson = [{\"packageId\":\"1\"}, {\"packageId\":\"2\"}, {\"packageId\":\"3\"}, {\"packageId\":\"4\"}, {\"packageId\":\"5\"}, {\"packageId\":\"6\"}]
如您所见,我不希望数组内容为字符串类型。
因此,我将整个数组存储为字符串,并决定删除这些“
这样:
var stringJson = "\(packageJson)"
stringJson = stringJson.replacingOccurrences(of: "\\{", with: "{", options: NSString.CompareOptions.literal, range: nil)
stringJson = stringJson.replacingOccurrences(of: "}\\", with: "}", options: NSString.CompareOptions.literal, range: nil)
我尝试“”“ {”
,但出现了错误,因此使用“ \\ {”
当我在某个地方阅读时,它可以用于“
”,但是该答案不正确,因此不确定。
如何删除这些”
将数组转换为我所需的格式?还是有更好的方法来声明让pjson =“ {\“ packageid \”:\“ \(pid)\”}“
” 这样,我可以简单地添加它而无需使用字符串格式? Swift不允许使用“ {”启动变量。
无论如何,我只想以这种方式获得最终结果:
stringJson = "[{\"packageId\":\"1\"}, {\"packageId\":\"2\"}, {\"packageId\":\"3\"}, {\"packageId\":\"4\"}, {\"packageId\":\"5\"}, {\"packageId\":\"6\"}]"
I been stuck at this for far too long. I have to send this array as string: "[{\"packageId\":\"1\"}, {\"packageId\":\"1\"}, {\"packageId\":\"3\"}]"
and so on. The values here are being inserted and can be any integer.
As I cannot declare any variable in format like let pjson = {\"packageId\":\"1\"}
so I decided to create a string instead in this way: let pjson = "{\"packageId\":\"1\"}"
and using for loop im adding data in some array in following way:
for package in packages {
if let pId = package.packageId {
let pjson = "{\"packageId\":\"\(pId)\"}"
packageJson.append(pjson as Any)
}
}
After this im getting an array like this:
packageJson = ["{\"packageId\":\"1\"}", "{\"packageId\":\"2\"}", "{\"packageId\":\"3\"}", "{\"packageId\":\"4\"}", "{\"packageId\":\"5\"}", "{\"packageId\":\"6\"}"]
Now I want to convert my packageJson
to this format:
packageJson = [{\"packageId\":\"1\"}, {\"packageId\":\"2\"}, {\"packageId\":\"3\"}, {\"packageId\":\"4\"}, {\"packageId\":\"5\"}, {\"packageId\":\"6\"}]
As you can see, I don't want array content to be a string type.
So I stored whole array as string and decided to remove those "
like this:
var stringJson = "\(packageJson)"
stringJson = stringJson.replacingOccurrences(of: "\\{", with: "{", options: NSString.CompareOptions.literal, range: nil)
stringJson = stringJson.replacingOccurrences(of: "}\\", with: "}", options: NSString.CompareOptions.literal, range: nil)
I tried to ""{"
but got error so using "\\{"
as I read somewhere it can be used for "
however that answer wasn't market correct so not sure.
How can I remove those "
to convert array into my required format? Or is there any better way to declare let pjson = "{\"packageId\":\"\(pId)\"}"
so I can simply add that without having to use string format? Swift doesn't allow to start a variable with "{" tho.
Anyway I would just like to get final result in this way:
stringJson = "[{\"packageId\":\"1\"}, {\"packageId\":\"2\"}, {\"packageId\":\"3\"}, {\"packageId\":\"4\"}, {\"packageId\":\"5\"}, {\"packageId\":\"6\"}]"
发布评论
评论(2)
您的最终结果是一系列自定义对象的JSON字符串。有一种更简单的方法来实现自己想要的东西。
首先创建一个结构以保持您的值:
然后根据您认为合适的形式创建数据。示例:
然后用JSONENCODER编码它们:
这将为您提供
Data
对象。如果您喜欢或将其发送到DatareQuest的正文中,则可以将其视为字符串。示例:
将产生:
Your final result is a JSON string of an array of custom objects. There is a way more simpler way to achieve what you want.
First create a struct to hold your values:
Then create you data as you deem fit. Example:
Then encode them with JSONEncoder:
This will give you a
Data
object. You can treat it as string if you like or send it in the body of a datarequest.Example:
will produce:
在Swift 5.0+中,
您将获得结果:
[{“ packageid”:“ 1”},{“ packageid”:“ 2”}]
In Swift 5.0+
you will get the results:
[{"packageId":"1"},{"packageId":"2"}]