解析不起作用“for”

时间:2017-05-25 09:37:33

标签: json swift

解析不适用于“for”

可能出现错误?

之前这段代码使用了这样的JSON,但是当我改变JSON“for”停止工作时 enter image description here

function GetCountry() {
    var CountrySer = Myservice.GetCountryData();
    CountrySer.then(function(d) {
        $scope.countryData = d.data;
        $scope.StateVal = "Please Select Staes";
    }).then(function(res) {
        console.log('Error....')
    })
}

$scope.GetState = function() {
    var StateSer = Myservice.GetstaeService($scope.Cnt_Id)
    StateSer.then(function(d) {
        $scope.StateData = d.data;
        $scope.CityVal = "Please Select City";

    })
}
$scope.GetCity = function() {
    var Cityser = Myservice.Getcity($scope.Sts_Id);
    Cityser.then(function(d) {
        $scope.CityData = d.data;
    })
}

这是我目前的JSON

enter image description here

2 个答案:

答案 0 :(得分:2)

库键包含字典而不是数组,字典的每个键也包含值作为标题,所以这样做。

models[[i]]$outcome

注意:如果此if let main = json["LIBRARY"] as? [String:[String : Any]]{ //Now loop though the dictionary for (_, data) in main { //Now access data dictionary same way you are accessing previously var info = Modal() info.name = data["NAME"] as? String info.id = data["ID"] as? String info.ImageViewURL = data["PICTURE"] as! String print(info.id) if let filesArray = data["FILES"] as? [[String : Any]] { for file in filesArray { var info = Modal() info.name = file["NAME"] as? String info.audioUrl = file["SRC"] as? String print(info.name! as Any) print(info.audioUrl! as Any, "\n") self.modals.append(info) } } print(info.name) self.modals.append(info) } } 工作,则JSON回复看起来IDNumber而非String确定的数字将其转换为info.id = data["ID"] as? String

答案 1 :(得分:0)

你有另一个标签" LIBRARY"这是" 47234",您需要先解析此标记的数据,然后才能应用此代码

 for data in main {
                var info = Modal()
                info.name = data["NAME"] as? String
                info.id = data["ID"] as? String
                info.ImageViewURL = data["PICTURE"] as! String
                print(info.id)

                if let filesArray = data["FILES"] as? [[String : Any]] {
                    for file in filesArray {
                        var info = Modal()
                        info.name = file["NAME"] as? String
                        info.audioUrl = file["SRC"] as? String

                        print(info.name! as Any)
                        print(info.audioUrl! as Any, "\n")

                        self.modals.append(info)
                    }

                }

                print(info.name)

                self.modals.append(info)

            }
        }