Arrays 获取数组而不是字符串

Arrays 获取数组而不是字符串,arrays,json,swift,string,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:

解析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)")
    }
}
===========

谢谢你,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
        }
      }
    }
 }
class游戏场景:SKScene{
覆盖func didMove(到视图:SKView){
如果let path=Bundle.main.path(forResource:“testJson”,类型为:“json”){
做{
let data=try data(contentsOf:URL(fileURLWithPath:path),选项:.mappedIfSafe)
让jsonResult=try JSONSerialization.jsonObject(使用:data,options:.mutableLeaves)
如果让jsonResult=jsonResult作为字典,则让数组=jsonResult[“3”]{
打印(“func didMove数组:\(数组)”)
}
}抓住{
//处理错误
}
}
}
}

如@Andy所述,数组值未按正确格式编码,因此被解码为字符串。格式如下

{
    "0": [31,47,51],
    "1": [74, 47, 51, 78],
    "2": [72, 65, 69, 80],
    "3": [63, 83, 87, 71]
}
myArray = myArrayString.toArray()
如果您无法修复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)})

    }
}
并在您的阵列上使用它,如下所示

{
    "0": [31,47,51],
    "1": [74, 47, 51, 78],
    "2": [72, 65, 69, 80],
    "3": [63, 83, 87, 71]
}
myArray = myArrayString.toArray()

后端未正确编码这些值。你需要让你的后端团队来修复它,或者自己修复后端,或者手动解析该数组。是的,你需要修复JSON。整数数组在JSON中不应是字符串。在Swift中,不应使用
NSDictionary
NSArray
NSData
NSURL
等。请使用适当的Swift字典和数组。使用
数据
。使用
URL
。您可能不需要扩展
NSObject
。它也可以工作。谢谢。现在我也用rmaddy建议的方法解决了这个问题,数组字符串本身就是有效的JSON,而不是手动处理字符串。就像解析普通JSON一样解析它。