SwiftyJSON打印字典数组中的所有值

时间:2022-09-28 13:28:15

I have a dictionary array:

我有一个字典数组:

var dictArray = [[String:String]]()

I append a few dictionaries and turn dictArray into JSON:

我附加了几个词典并将dictArray转换为JSON:

let json = JSON(dictArray)

Then I encounter trouble trying to actually print these values in JSON format with this loop:

然后我遇到了尝试使用此循环以JSON格式实际打印这些值的问题:

for (index,subJson):(String, JSON) in json {
    print(index)
}

The above loop is taken from the SwiftyJSON documentation but it doesn't seem to be working for me. I want the output to look like this:

上面的循环取自SwiftyJSON文档,但它似乎对我不起作用。我希望输出看起来像这样:

 {
"dictArray": [
  {
  "a": "a stuff 1",
  "b": "b stuff 1",
  "c": "c stuff 1",
  "d": "d stuff 1"
},
{
  "a": "a stuff 2",
  "b": "b stuff 2",
  "c": "c stuff 2",
  "d": "d stuff 2"
},
{
  "a": "a stuff 3",
  "b": "b stuff 3",
  "c": "c stuff 3",
  "d": "d stuff 3"
    }
  ]
}

All help is greatly appreciated.

非常感谢所有帮助。

2 个解决方案

#1


0  

You don't need to iterate the dictionary to print it. You could simply print it with print(json)

您不需要迭代字典来打印它。你可以用print(json)打印它

If you want to loop the dictionary, you should go with something like this:

如果你想循环字典,你应该使用这样的东西:

for dictionaries in json {
    for (key, value) in dictionaries {
        print("\(key): \(value)")
    }
}

#2


0  

you can use like this :

你可以像这样使用:

var array = [String:[[String:String]]]()

var dictArray = [[String:String]]()

var stuffArray1 = [String: String]()
stuffArray1["a"] = "a stuff 1"
stuffArray1["b"] = "b stuff 1"
stuffArray1["c"] = "c stuff 1"
stuffArray1["d"] = "d stuff 1"
dictArray.append(stuffArray1)

var stuffArray2 = [String: String]()
stuffArray2["a"] = "a stuff 2"
stuffArray2["b"] = "b stuff 2"
stuffArray2["c"] = "c stuff 2"
stuffArray2["d"] = "d stuff 2"
dictArray.append(stuffArray2)

var stuffArray3 = [String: String]()
stuffArray3["a"] = "a stuff 3"
stuffArray3["b"] = "b stuff 3"
stuffArray3["c"] = "c stuff 3"
stuffArray3["d"] = "d stuff 3"
dictArray.append(stuffArray3)

array["dictArray"] = dictArray

let json = JSON(array)
print(json)

#1


0  

You don't need to iterate the dictionary to print it. You could simply print it with print(json)

您不需要迭代字典来打印它。你可以用print(json)打印它

If you want to loop the dictionary, you should go with something like this:

如果你想循环字典,你应该使用这样的东西:

for dictionaries in json {
    for (key, value) in dictionaries {
        print("\(key): \(value)")
    }
}

#2


0  

you can use like this :

你可以像这样使用:

var array = [String:[[String:String]]]()

var dictArray = [[String:String]]()

var stuffArray1 = [String: String]()
stuffArray1["a"] = "a stuff 1"
stuffArray1["b"] = "b stuff 1"
stuffArray1["c"] = "c stuff 1"
stuffArray1["d"] = "d stuff 1"
dictArray.append(stuffArray1)

var stuffArray2 = [String: String]()
stuffArray2["a"] = "a stuff 2"
stuffArray2["b"] = "b stuff 2"
stuffArray2["c"] = "c stuff 2"
stuffArray2["d"] = "d stuff 2"
dictArray.append(stuffArray2)

var stuffArray3 = [String: String]()
stuffArray3["a"] = "a stuff 3"
stuffArray3["b"] = "b stuff 3"
stuffArray3["c"] = "c stuff 3"
stuffArray3["d"] = "d stuff 3"
dictArray.append(stuffArray3)

array["dictArray"] = dictArray

let json = JSON(array)
print(json)