在swift 1.2中解决JSON问题

时间:2016-08-09 06:35:38

标签: ios json swift parsing

在swift 1.2中解决JSON问题

我遇到了JSON解析的问题,我的参数没有得到正确的数据响应。我已经坚持这个问题很长一段时间了。我不知道如何解决它。我是iOS开发的新手。我也没有使用任何第三方库。[不知道如何使用它们]

我在第一次迭代时获得了正确的响应,但是对于下一次迭代,上一次迭代的结果与新结果一起被添加。就像每个循环结果以上一次迭代的结果结束

我的JSON SAMPLE。 [可提供链接,请告知我们]

struct AssetItems {
    
    var Name:String
    var Desc:String
    var Image:String
    var entityId:String
    
    var aFileType:String
    var aFileSize:String
    var aFileCreatedDate:String
    var aFileRevisionDate:String
    var aFileModifiedDate:String
    var aProductName:String
    var aVersion:String
    var aAssetType:String
    var aIsFolder:String
    
    init(assetName:String,assetDescription:String,assetImage:String,entityId:String,FileType:String,FileSize:String,FileCretedDate:String,FileReveisionDate:String,FileModifiedDate:String,ProductName:String,Version:String,AssetType:String,IsFolder:String) {
        
        
        
        self.Name = assetName
        self.Desc = assetDescription
        self.Image = assetImage
        self.entityId = entityId
        self.aFileType = FileType
        self.aFileSize = FileSize
        self.aFileCreatedDate = FileCretedDate
        self.aFileRevisionDate = FileReveisionDate
        self.aFileModifiedDate = FileModifiedDate
        self.aProductName = ProductName
        self.aVersion = Version
        self.aAssetType = AssetType
        self.aIsFolder = IsFolder
    }
}



struct AssetModel {
    var aName:String
    var aDesc:String
    var aCount:String
    var aEntityId:String
    var items:[AssetItems]
    
    init(rackName:String,rackDescription:String,totalNoAssets:String,EntityId:String,rackItems:[AssetItems]) {
        self.aName = rackName
        self.aDesc = rackDescription
        self.aCount = totalNoAssets
        self.aEntityId = EntityId
        self.items = rackItems
    }
    
}

  
var assetData:Array<AssetModel> = Array<AssetModel>()
var assetContents:Array<AssetItems> = Array<AssetItems>()

这就是我如何参与JSON

 func get_data_from_Url(url:String) {
        
        
        let url = NSURL(string: url)
        var request:NSMutableURLRequest = NSMutableURLRequest(URL: url!)
        request.HTTPMethod = "GET"
        
        request.addValue("application/json", forHTTPHeaderField: "Content_Type")
        request.addValue("application/json", forHTTPHeaderField: "Accept")
        
        
        var responseError : NSError?
        var response : NSURLResponse?
        
        var urlData: NSData? = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: &responseError)
        
        if(urlData != nil) {
            
            var responseData:NSString = NSString(data: urlData!, encoding: NSUTF8StringEncoding)!
            let res = response as! NSHTTPURLResponse
            var err:NSError?
            if (res.statusCode == 200)
            {
                var parseError: NSError?
                let json:AnyObject = NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers , error: &err) as AnyObject!
                if (parseError == nil) // no error while parsing
                {
                    if let Asset_list = json as? NSArray
                    {
                    for (var i = 0; i < Asset_list.count ; i++ )
                    {
                    if let Asset_obj = Asset_list[i] as? NSDictionary
                    {
                    if let AssetGroupName = Asset_obj["Name"] as? String
                    {
                    if let AssetGroupDescription = Asset_obj["Description"] as? String
                    {
                    if let entityId = Asset_obj["EntityId"] as? String
                    {
                    if let totalAssets = Asset_obj["_NoOfAssets"] as? String
                    {
                    if let items = Asset_obj["Items"] as? NSArray
                    {
                    for (var j=0 ; j < items.count; j++)
                    {
                    if let asset_items = items[j] as? NSDictionary
                    {
                    if let AbsolutePath = asset_items["AbsolutePath"] as? String
                    {
                    if let Description = asset_items["_Description"] as? String
                    {
                    if let Name = asset_items["_Name"] as? String {
                    if let entityId = asset_items["EntityId"] as? String
                    {
                    if let FileType = asset_items["_FileType"] as? String
                    {
                    if let FileSize = asset_items["_FileSize"] as? String
                    {
                    if let FileCreatedDate = asset_items["CreatedDate"] as? String
                    {
                    if let FileModifiedDate = asset_items["ModifiedDate"] as? String
                    {
                    if let RevisionDate = asset_items["RevisionDate"] as? String
                    {
                    if let productName = asset_items["ProductName"] as? String
                    {
                    if let version = asset_items["Version"] as? String
                    {
                    if let AssetType = asset_items["_AssetType"] as? String
                    {
                    if let isFolder = asset_items["IsFolder"] as? String
                    {
                        
                                                                                                                    
                var add = AssetItems(assetName: Name, assetDescription: Description, assetImage: AbsolutePath, entityId: entityId, FileType: FileType, FileSize: FileSize, FileCretedDate: FileCreatedDate, FileReveisionDate: RevisionDate, FileModifiedDate: FileModifiedDate, ProductName: productName, Version: version, AssetType: AssetType, IsFolder: isFolder)
                    assetContents.append(add)

                        }
                        }
                        }
                        }
                        }
                       }
                    }
                    }
                   }
                }
                }
                                                                    
                }
                }
                        
                }
                        
                }
            var add_it = AssetModel(rackName: AssetGroupName, rackDescription: AssetGroupDescription, totalNoAssets: totalAssets, EntityId: entityId, rackItems: assetContents)
                assetData.append(add_it)
                        
                        }
                    }
                                            
                    }
                        
                    }
                        
                    }
                        
                    }
                    }
                    }
                }
                
            }
        }
        
        
        
        do_table_refresh()
    }

func do_table_refresh()
    {
        dispatch_async(dispatch_get_main_queue(),
            {
                self.tableView.reloadData()
                
        })
        
        return
    }

我的输出

请有人帮我解决这个问题,任何建议都可以。

感谢

1 个答案:

答案 0 :(得分:1)

特别感谢@SMi,凭借他的GuidLines,我能够解决我的问题。 我知道我必须在循环结束时清除内部数组[但是,我不知道如何清除它。]

 var add_it = AssetModel(rackName: AssetGroupName, rackDescription: AssetGroupDescription, totalNoAssets: totalAssets, EntityId: entityId, rackItems: assetContents)
       assetData.append(add_it)
assetContents.removeAll()

我所要做的就是使用 .removeAll()方法清除 assetContents 数组。