如何从字典输出中删除方括号并用大括号替换?

时间:2022-09-15 16:00:30

I am a newbie to Swift so kindly excuse me if this question sounds too silly. I am trying to create a JSON object from a dictionary Array output which must have Curly Brackets("{}") after each entity rather than Square brackets ("[]"). My Code is given below.

我是斯威夫特的新手,请原谅我,如果这个问题听起来太傻了。我试图从字典数组输出创建一个JSON对象,每个实体后面必须有Curly Brackets(“{}”)而不是方括号(“[]”)。我的守则如下。

import UIKit

var locations = Array<Dictionary<String, String>>()

var myLocations = ["pqr","xyz"]

myLocations.forEach {_ in 
    var dictionary = Dictionary<String, String>()
    dictionary["string1"] = "hello"
    dictionary["string2"] = "world"
    locations.append(dictionary)

}
print(locations)

The output to this is:- [["string2": "world", "string1": "hello"], ["string2": "world", "string1": "hello"]]\n

输出到: - [[“string2”:“world”,“string1”:“hello”],[“string2”:“world”,“string1”:“hello”]] \ n

However I require it as:- [{"string2": "world", "string1": "hello"}, {"string2": "world", "string1": "hello"}]\n

但是我需要它: - [{“string2”:“world”,“string1”:“hello”},{“string2”:“world”,“string1”:“hello”}] \ n

I know one way of doing this is by using filter Arrays but I suspect there could be an easier way to this which I am not able to find after searching through various documentations on Swift. Could you please help me with this. Thanks in advance.

我知道这样做的一种方法是使用过滤器数组,但我怀疑可能有一种更简单的方法,我在搜索Swift上的各种文档后无法找到。你能帮我解决这个问题吗?提前致谢。

1 个解决方案

#1


2  

The output is

输出是

[["string2": "world", "string1": "hello"], ["string2": "world", "string1": "hello"]]

because this is a Swift array of Swift dictionaries.

因为这是一个Swift数组的Swift词典。

To convert this object to JSON, do not parse and replace the characters yourself, use NSJSONSerialization instead:

要将此对象转换为JSON,请不要自己解析和替换字符,而是使用NSJSONSerialization:

do {
    let jsonData = try NSJSONSerialization.dataWithJSONObject(locations, options: [])
    if let jsonString = String(data: jsonData, encoding: NSUTF8StringEncoding) {
        print(jsonString)
    }
} catch {
    print(error)
}

Prints:

打印:

[{"string2":"world","string1":"hello"},{"string2":"world","string1":"hello"}]

[{ “字符串2”: “世界”, “字符串1”: “你好”},{ “字符串2”: “世界”, “字符串1”: “你好”}]

We use dataWithJSONObject to convert your Swift object to JSON data, then we use String(data:, encoding:) to convert this JSON data to a JSON string.

我们使用dataWithJSONObject将您的Swift对象转换为JSON数据,然后我们使用String(data:,encoding :)将此JSON数据转换为JSON字符串。

#1


2  

The output is

输出是

[["string2": "world", "string1": "hello"], ["string2": "world", "string1": "hello"]]

because this is a Swift array of Swift dictionaries.

因为这是一个Swift数组的Swift词典。

To convert this object to JSON, do not parse and replace the characters yourself, use NSJSONSerialization instead:

要将此对象转换为JSON,请不要自己解析和替换字符,而是使用NSJSONSerialization:

do {
    let jsonData = try NSJSONSerialization.dataWithJSONObject(locations, options: [])
    if let jsonString = String(data: jsonData, encoding: NSUTF8StringEncoding) {
        print(jsonString)
    }
} catch {
    print(error)
}

Prints:

打印:

[{"string2":"world","string1":"hello"},{"string2":"world","string1":"hello"}]

[{ “字符串2”: “世界”, “字符串1”: “你好”},{ “字符串2”: “世界”, “字符串1”: “你好”}]

We use dataWithJSONObject to convert your Swift object to JSON data, then we use String(data:, encoding:) to convert this JSON data to a JSON string.

我们使用dataWithJSONObject将您的Swift对象转换为JSON数据,然后我们使用String(data:,encoding :)将此JSON数据转换为JSON字符串。