Arrays Swift:检查jsonObjectWithData是否为json

Arrays Swift:检查jsonObjectWithData是否为json,arrays,json,swift,dictionary,Arrays,Json,Swift,Dictionary,我有一个通过http与服务器通信的swift应用程序 我从这个服务器得到的答案可能是json,也可能不是。我需要检查它们是什么,以便将答案打印为字典或数组,以避免致命错误:在NSJSONSerialization.JSONObjectWithData(dataVal,选项:NSJSONReadingOptions.MutableContainers,错误:nil)处展开可选值时意外发现nil!NSDictionary 这是我的jsonObjectWithData: var dataVal: NS

我有一个通过http与服务器通信的swift应用程序

我从这个服务器得到的答案可能是json,也可能不是。我需要检查它们是什么,以便将答案打印为
字典
数组
,以避免
致命错误:在
NSJSONSerialization.JSONObjectWithData(dataVal,选项:NSJSONReadingOptions.MutableContainers,错误:nil)处展开可选值时意外发现nil!NSDictionary

这是我的
jsonObjectWithData

var dataVal: NSData =  NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error:nil)!
elimResponse = response?.description        
elim = NSJSONSerialization.JSONObjectWithData(dataVal, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary
我想得到的是:

if dataVal is Json{
    elim = NSJSONSerialization.JSONObjectWithData(dataVal, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary
    println(elimJson)
    }else{
    elim = NSJSONSerialization.JSONObjectWithData(dataVal, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSArray
    println(elim)
    }

谢谢你的帮助。问候。

您可以在
中尝试强制转换,如果让
这样做:

if let elim = NSJSONSerialization.JSONObjectWithData(dataVal, options: nil, error: nil) as? NSDictionary {
    println("elim is a dictionary")
    println(elim)
} else if let elim = NSJSONSerialization.JSONObjectWithData(dataVal, options: nil, error: nil) as? NSArray {
    println("elim is an array")
    println(elim)
} else {
    println("dataVal is not valid JSON data")
}

Swift 2.0的更新

do {
    if let elim = try NSJSONSerialization.JSONObjectWithData(dataVal, options: []) as? NSDictionary {
        print("elim is a dictionary")
        print(elim)
    } else if let elim = try NSJSONSerialization.JSONObjectWithData(dataVal, options: []) as? NSArray {
        print("elim is an array")
        print(elim)
    } else {
        print("dataVal is not valid JSON data")
    }
} catch let error as NSError {
    print(error)
}

如果让
这样做,您可以在
中尝试强制转换:

if let elim = NSJSONSerialization.JSONObjectWithData(dataVal, options: nil, error: nil) as? NSDictionary {
    println("elim is a dictionary")
    println(elim)
} else if let elim = NSJSONSerialization.JSONObjectWithData(dataVal, options: nil, error: nil) as? NSArray {
    println("elim is an array")
    println(elim)
} else {
    println("dataVal is not valid JSON data")
}

Swift 2.0的更新

do {
    if let elim = try NSJSONSerialization.JSONObjectWithData(dataVal, options: []) as? NSDictionary {
        print("elim is a dictionary")
        print(elim)
    } else if let elim = try NSJSONSerialization.JSONObjectWithData(dataVal, options: []) as? NSArray {
        print("elim is an array")
        print(elim)
    } else {
        print("dataVal is not valid JSON data")
    }
} catch let error as NSError {
    print(error)
}

我为Swift 3.0做过类似的事情,它对我很有用

func isValidJson(check data:Data) -> Bool
    {
        do{
        if let _ = try JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary {
           return true
        } else if let _ = try JSONSerialization.jsonObject(with: data, options: []) as? NSArray {
            return true
        } else {
            return false
        }
        }
        catch let error as NSError {
            print(error)
            return false
        }

    }

我为Swift 3.0做过类似的事情,它对我很有用

func isValidJson(check data:Data) -> Bool
    {
        do{
        if let _ = try JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary {
           return true
        } else if let _ = try JSONSerialization.jsonObject(with: data, options: []) as? NSArray {
            return true
        } else {
            return false
        }
        }
        catch let error as NSError {
            print(error)
            return false
        }

    }

嗨,Eric,我会尝试这个解决方案,我现在有一些错误。elim已经初始化,如果让我执行
,我无法执行
,如果我只执行
,如果elim
无效,elim无法识别,我尝试解决此问题。好吧,用此初始化替换您以前的初始化?我看不出有什么问题。您要求一种测试方法,这就是测试方法。:)无论如何,如果您的
elim
已经初始化,这意味着它已成功创建,那么您不需要测试是否可以创建它,不是吗?…;)嗨,Eric,我会尝试这个解决方案,我现在有一些错误。elim已经初始化,如果让我执行
,我无法执行
,如果我只执行
,如果elim
无效,elim无法识别,我尝试解决此问题。好吧,用此初始化替换您以前的初始化?我看不出有什么问题。您要求一种测试方法,这就是测试方法。:)无论如何,如果您的
elim
已经初始化,这意味着它已成功创建,那么您不需要测试是否可以创建它,不是吗?…;)