将数组用作字符串并删除双引号符号以将数组内容类型从字符串更改为任何,因为整个数组将为字符串,其类型可以是任何

发布于 2025-02-11 15:49:41 字数 1825 浏览 2 评论 0 原文

我被困了太久了。我必须将此数组发送为字符串:“ [{{\“ 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\"}]"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄居者 2025-02-18 15:49:41

您的最终结果是一系列自定义对象的JSON字符串。有一种更简单的方法来实现自己想要的东西。

首先创建一个结构以保持您的值:

struct PackageContainer: Codable{
    var packageId: String
}

然后根据您认为合适的形式创建数据。示例:

let packages = (0...10).map{ PackageContainer(packageId: "\($0)") }

然后用JSONENCODER编码它们:

let json = try JSONEncoder().encode(packages)

这将为您提供 Data 对象。如果您喜欢或将其发送到DatareQuest的正文中,则可以将其视为字符串。


示例:

print(String(data: json, encoding: .utf8)!)

将产生:

[{“ packageid”:“ 0”},{“ packageid”:“ 1”},{“ packageid”:“ 2”},{“ packageid”:“ 3”},{“ packageid”} 4“},{“ packageid”:“ 5”},{“ packageid”:“ 6”},{“ packageid”:“ 7”},{“ packageid”:“ 8”},{“ packageid”:“” 9“},{“ packageid”:“ 10”}]

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:

struct PackageContainer: Codable{
    var packageId: String
}

Then create you data as you deem fit. Example:

let packages = (0...10).map{ PackageContainer(packageId: "\($0)") }

Then encode them with JSONEncoder:

let json = try JSONEncoder().encode(packages)

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:

print(String(data: json, encoding: .utf8)!)

will produce:

[{"packageId":"0"},{"packageId":"1"},{"packageId":"2"},{"packageId":"3"},{"packageId":"4"},{"packageId":"5"},{"packageId":"6"},{"packageId":"7"},{"packageId":"8"},{"packageId":"9"},{"packageId":"10"}]

萌︼了一个春 2025-02-18 15:49:41

在Swift 5.0+中,

let ids = [1, 2]

let idsString = ids.map { id -> String in
    return "{\"packageId\"" + ":" + "\"\(id)\"}"
}.joined(separator: ",")

let result = "[" + idsString + "]"
print(result)

您将获得结果:

[{“ packageid”:“ 1”},{“ packageid”:“ 2”}]

In Swift 5.0+

let ids = [1, 2]

let idsString = ids.map { id -> String in
    return "{\"packageId\"" + ":" + "\"\(id)\"}"
}.joined(separator: ",")

let result = "[" + idsString + "]"
print(result)

you will get the results:

[{"packageId":"1"},{"packageId":"2"}]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文