arrays - 获取数组而不是字符串

标签 arrays json swift string

解析 JSON 后,我得到以下值:

myArray = "[63, 83, 87, 71]"

如何获取数组而不是字符串?我需要的是:

myArray = [63, 83, 87, 71]

更新:

这是我的简单 json:

{
    "0": "[31,47,51]",
    "1": "[74, 47, 51, 78]",
    "2": "[72, 65, 69, 80]",
    "3": "[63, 83, 87, 71]"
}

这是一个解析:

class gameModel: NSObject {
    func getCurrentArrays() -> NSDictionary {
        let appBundlePath:String? = Bundle.main.path(forResource: "testJsonPrepared", ofType: "json")

        if let actualBundlePath = appBundlePath {
            let urlPath:NSURL? = NSURL(fileURLWithPath: actualBundlePath)
            if let actualUrlPath = urlPath {
                let jsonData:NSData? = NSData(contentsOf: actualUrlPath as URL)
                if let actualJsonData = jsonData {
                    let dict: NSDictionary? = (try? JSONSerialization.jsonObject(with: actualJsonData as Data, options: JSONSerialization.ReadingOptions.mutableContainers)) as! NSDictionary?
                    if let currentArrays = dict {
                        return currentArrays
                    }
                }
            }
        }
        return NSDictionary()
    }
}

这是最终结果:

class GameScene: SKScene {
    let model = gameModel()
    var arrays: NSDictionary = ["":""]

    override func didMove(to view: SKView) {
        arrays = model.getCurrentArrays()
        print("# func viewDidLoad arrays: \(arrays)")

        let testKey = String(3)

        let testArray = arrays.value(forKey: testKey) as! String
        print("# func viewDidLoad testArray: \(testArray)")
    }
}

==========

更新2 谢谢,rmaddy。这是我的解决方案:

testJson.json

{
  "0": [31,47,51],
  "1": [74, 47, 51, 78],
  "2": [72, 65, 69, 80],
  "3": [63, 83, 87, 71]
 }

这里是从 json 读取的内容:

class GameScene: SKScene {



override func didMove(to view: SKView) {

    if let path = Bundle.main.path(forResource: "testJson", ofType: "json") {
        do {
            let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
            let jsonResult = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves)
            if let jsonResult = jsonResult as? Dictionary<String, [Int]>, let array = jsonResult["3"] {
                print("# func didMove array: \(array)")
            }
        } catch {
            // handle error
        }
      }
    }
 }

最佳答案

正如 @Andy 所提到的,数组值没有以正确的格式进行编码,因此被解码为字符串。格式应如下

{
    "0": [31,47,51],
    "1": [74, 47, 51, 78],
    "2": [72, 65, 69, 80],
    "3": [63, 83, 87, 71]
}

如果你无法修复 json,下面的扩展应该可以解决问题

extension String{
    func toArray()->[Int]{
        var trimmedStr = self.replacingOccurrences(of: "[", with: "")
        trimmedStr = self.replacingOccurrences(of: "]", with: "")
        let strArray = trimmedStr.components(separatedBy: ",")
        return strArray.flatMap({Int($0)})

    }
}

并在你的 arrayString 上使用它,如下所示

myArray = myArrayString.toArray()

关于arrays - 获取数组而不是字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52158454/

相关文章:

arrays - R 中长度不同的数组的数组

javascript - 如何在 Javascript 中更改 JSON 数据的格式?

ios - 在 View 中安装包含更多 stackview 的 stackview

swift - Xcode 需要很长时间才能打印调试结果。

ios - 单击按钮时修改生成的 imageViews 的图像

arrays - 返回 NULL 的空数组的 array_length()

javascript - 如何将数组中的值放入 td 元素中

Java - 来自 JSONArray 的字符串在 if 语句中不匹配

javascript - npm Node 编码

javascript - npm install -SE 带有次要版本符号