代码之家  ›  专栏  ›  技术社区  ›  Roman

获取数组而不是字符串

  •  0
  • Roman  · 技术社区  · 6 年前

    在分析了我的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。这是我的解决方案:

    测试json.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
            }
          }
        }
     }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   CrazyProgrammer    6 年前

    正如@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)})
    
        }
    }
    

    在排列时使用它,如下所示

    myArray = myArrayString.toArray()