Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 我无法从swift 3中给定的url获取子名称?_Ios_Json_Swift - Fatal编程技术网

Ios 我无法从swift 3中给定的url获取子名称?

Ios 我无法从swift 3中给定的url获取子名称?,ios,json,swift,Ios,Json,Swift,在api中,我获取全局数组,但在此情况下,我无法获取作为单独数组的子名称,只有最后加载的数组名称已获取到数组中如何获取数组中的所有子名称请帮助我如何获取其中的所有名称,这是我已经尝试过的代码 var detailsArray = NSArray() var globalArray = NSMutableArray() let url = "http://www.json-generator.com/api/json/get/cwqUAMjKGa?indent=2" func downloadJs

在api中,我获取全局数组,但在此情况下,我无法获取作为单独数组的子名称,只有最后加载的数组名称已获取到数组中如何获取数组中的所有子名称请帮助我如何获取其中的所有名称,这是我已经尝试过的代码

var detailsArray = NSArray()
var globalArray = NSMutableArray()
let url = "http://www.json-generator.com/api/json/get/cwqUAMjKGa?indent=2"
func downloadJsonWithURL() {
    let url = NSURL(string: self.url)
    URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in
        if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary
        {
            self.detailsArray = (jsonObj?.value(forKey: "data") as? [[String: AnyObject]])! as NSArray
            print(self.detailsArray)
            for item in self.detailsArray
            {
                let majorDic = NSMutableDictionary()
                let detailDict = item as! NSDictionary
                print(detailDict["name"]!)
                majorDic .setValue(detailDict["name"]!, forKey: "name")
                print(detailDict["children"]!)
                if !(detailDict["children"]! is NSNull)
                {
                    let children = detailDict["children"]! as! NSArray
                    let childrenstring = NSMutableString()
                    if children.count > 0 {
                        for item in children{
                            let chilDic = item as! NSDictionary
                            print(chilDic["name"]!)
                            print(chilDic["products"]!)
                            majorDic.setValue(chilDic["name"]!, forKey: "Childernname")
                            let products = chilDic["products"]! as! NSArray
                            if products.count > 0
                            {
                                for item in products
                                {
                                    var sr = String()
                                    sr = (item as AnyObject) .value(forKey: "name") as! String
                                    childrenstring.append(sr)
                                    childrenstring.append("*")
                                }
                                majorDic.setValue(childrenstring, forKey: "Childernproducts")
                            }
                            else
                            {
                                print("products.count\(products.count)")
                                majorDic.setValue("NO DATA", forKey: "Childernproducts")
                            }
                        }
                    }
                    else
                    {
                        print("childernw.count\(children.count)")
                        majorDic.setValue("NO DATA", forKey: "Childernname")
                    }
                }
                else
                {
                    majorDic.setValue("NO DATA", forKey: "Childernname")
                    majorDic.setValue("NO DATA", forKey: "Childernproducts")
                }
                self.globalArray.add(majorDic)
            }
            print("TOTAL ASSECTS\(self.globalArray)")
            OperationQueue.main.addOperation({
                self.tableView.reloadData()
                print(self.globalArray)
                print(self.detailsArray)
            })
        }
    }).resume()
}

我也有同样的问题。不要使用for循环,请尝试使用flatMap(),然后使用
value(forKey:)
函数提取值

let productNames = productValues.flatMap() {$0.value}   
let names = (productNames as AnyObject).value(forKey: "name")
这对我的json很有用。我的Json和你的相似。 我在数组中得到了单独的值

试试这个:

var detailsArray = [DataList]()
func downloadJsonWithURL() {
    let url = NSURL(string: "http://www.json-generator.com/api/json/get/cwqUAMjKGa?indent=2")
    URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in
        if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary
        {
            let objArr = (jsonObj?["data"]  as? [[String: AnyObject]])! as NSArray
            for obj in objArr {
                self.detailsArray.append(DataList(json: obj as! [String : AnyObject]))
            }
            print(self.detailsArray)
        }
    }).resume()
}
模型类

class DataList: NSObject {

    var count: Int
    var category_id: Int
    var childern:[Children]
    var name:String

    init (json: [String: AnyObject]){

        if let childrenList = json["children"] as? [[String: AnyObject]] {
            var result = [Children]()
            for obj in childrenList {
                result.append(Children(json: obj))
            }
            self.childern = result
        } else {
            self.childern = [Children]()
        }

        if let count = json["count"] as? Int { self.count = count }
        else { self.count = 0 }
        if let category_id = json["category_id"] as? Int { self.category_id = category_id }
        else { self.category_id = 0 }
        if let name = json["name"] as? String { self.name = name }
        else { self.name = "" }
    }
}

class Children:NSObject{
    var count:Int
    var category_id:Int
    var products:[Products]
    var name:String

    init (json: [String: AnyObject]){

        if let productList = json["products"] as? [[String: AnyObject]] {
            var result = [Products]()
            for obj in productList {
                result.append(Products(json: obj))
            }
            self.products = result
        } else {
            self.products = [Products]()
        }

        if let count = json["count"] as? Int { self.count = count }
        else { self.count = 0 }
        if let category_id = json["category_id"] as? Int { self.category_id = category_id }
        else { self.category_id = 0 }
        if let name = json["name"] as? String { self.name = name }
        else { self.name = "" }

    }
}


class Products:NSObject{
    var product_id:Int
    var name:String

    init (json: [String: AnyObject]){

        if let product_id = json["product_id"] as? Int { self.product_id = product_id }
        else { self.product_id = 0 }

        if let name = json["name"] as? String { self.name = name }
        else { self.name = "" }

    }
}

注意:解析时请检查您的数据类型

如何使用flatMap(),我不知道?您得到的
踢脚板面板
重复正确??在数组中{childername=Kickboards;Childernproducts=“基础端面板墙壁端面板餐具室端面板基础填充墙填充墙填充墙填充板kickboard基板面板”name=“面板和填充物”}在这个儿童名称中,我只得到一个加载了Astropuring的名称,但我无法获得儿童中的所有名称,在儿童产品中,我获得所有数据@Bella数据分离非常重要。制作另一个类,如@yutsu所述。你可以把它命名为DataListModel.Swifta你能显示你的详细信息吗?这是一个大数组,它由api中的@yuyutsu组成如果你签入api,你将得到所有的@yuyutsu你能显示self.globalArray包含的内容吗?请显示格式它是一个大数组,我不能在这里显示,你可以在api@user1000中看到我需要插入的这个模型类同一个班级,或者我需要把它分成单独的基础课?@YouYutuUu可以使用两种方式,但使用单独的基础类容易理解你和你的队友。我不知道如何采取,请告诉我一个明确的方式?@ YuututSU检查,并告诉我,如果有任何问题。瓦姆西克里希纳苏拉提布罗我现在检查了你的答案,昨天下午我因为健康问题离开了办公室