如何从swift中的json数据中获取特定值

时间:2017-10-20 20:48:48

标签: php json swift

我试图从json中提取某个值。在这种情况下,我试图获得' isethanawake'在这种情况下应该是1:

可选([{" isethanawake":1,"名称":"探"},{" ismadisonawake":0,& #34;名称":"麦迪逊"},{" ismomawake":0,"名称":"妈妈"},{ " isdadawake":0,"名称":"爸爸"}]

这是我的代码,它没有将变量设置为1

var isethanawake = 0
var ismadisonawake = 0
var ismomawake = 0
var isdadawake = 0

request2.httpBody = postString2.data(using: String.Encoding.utf8)

let task2 = URLSession.shared.dataTask(with: request2 as URLRequest){
    data, response, error in

    if error != nil{
        print("error = \(error)")
        return
    }

    print("response = \(response)")

    let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
    print("responseString = \(responseString)")

    do{
        let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary

        if let parseJSON1 = json {
            isethanawake = parseJSON1["isethanawake"] as! Int

        }
        if let parseJSON2 = json{
            ismadisonawake = parseJSON2["ismadisonawake"] as! Int
        }


        if let parseJSON3 = json{
            ismomawake = parseJSON3["ismomawake"] as! Int
        }

        if let parseJSON4 = json{
            isdadawake = parseJSON4["isdadawake"] as! Int
        }

        print("hello" + String(isethanawake))
    }catch{
        print(error)
    }

它从这个PHP脚本获取数据:

$jsonarray = array();

if(mysqli_num_rows($ethanresult)){
    $returnValue1 = array("isethanawake"=> 1, "name"=> "Ethan");
    array_push($jsonarray, $returnValue1);
}else{
    $returnValue1 = array("isethanawake"=> 0, "name"=> "Ethan");
    array_push($jsonarray, $returnValue1);
}
if(mysqli_num_rows($madisonresult)){
    $returnValue2 = array("ismadisonawake"=> 1, "name"=> "Madison");
    array_push($jsonarray, $returnValue2);
}else{
    $returnValue2 = array("ismadisonawake"=> 0, "name"=> "Madison");
    array_push($jsonarray, $returnValue2);
}
if(mysqli_num_rows($momresult)){
    $returnValue3 = array("ismomawake"=> 1, "name"=> "Mom");
    array_push($jsonarray, $returnValue3);
}else{
    $returnValue3 = array("ismomawake"=> 0, "name"=> "Mom");
    array_push($jsonarray, $returnValue3);
}
if(mysqli_num_rows($dadresult)){
    $returnValue4 = array("isdadawake"=> 1, "name"=> "Dad");
    array_push($jsonarray, $returnValue4);
}else{
    $returnValue4 = array("isdadawake"=> 0, "name"=> "Dad");
    array_push($jsonarray, $returnValue4);

}
echo json_encode($jsonarray);

}

由于

1 个答案:

答案 0 :(得分:0)

正如您所说,您的JSON结构是:

  

可选([{ “isethanawake”:1, “名称”: “探”},{ “ismadisonawake”:0, “姓名”: “麦迪逊”},{ “ismomawake”:0, “姓名”:”妈妈 “},{” isdadawake “:0,” 姓名 “:” 爸爸“}]

在Swift中,json的类型为[[String:Any]]?这意味着你有一个Dictionarys数组。该错误告诉您不能使用String下标数组。 这意味着您需要遍历您的子数组来执行您想要的代码:

 do{
        let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary

if let json as? [[String: Any]]{
for arrayJSONObj in json {


            isethanawake = arrayJSONObj["isethanawake"] as? Int ?? 0

            ismadisonawake = arrayJSONObj["ismadisonawake"] as? Int ?? 0

            ismomawake = arrayJSONObj["ismomawake"] as? Int ?? 0

            isdadawake = arrayJSONObj["isdadawake"] as? Int ?? 0

}
}
        print("hello" + String(isethanawake))
    }catch{
        print(error)
    }

我还建议您使用值的默认值(这两个??)而不是强制解包它们以防止您的应用崩溃。