Swift 1.2에서 JSON 구문 분석 문제

JB29.4

Swift 1.2에서 JSON 구문 분석 문제

JSON 구문 분석에 문제가 있습니다. 매개 변수에 대한 올바른 데이터 응답을받지 못하고 있습니다. 나는 지금 꽤 오랫동안 문제에 갇혀 있습니다. 나는 그것을 고치는 방법을 모른다. iOS 개발이 처음입니다. 또한 타사 라이브러리를 사용하지 않았습니다. [사용 방법을 모릅니다.]

첫 번째 반복에 대해 올바른 응답을 얻고 있지만 다음 반복에 대해서는 이전 반복의 결과가 새 결과와 함께 추가됩니다. 각 루프 결과가 이전 반복의 결과로 끝나는 것처럼

내 JSON 샘플. [링크 제공 가능, 알려주세요]

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을 PARSING하는 방법입니다.

 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
    }

내 출력

누군가가 이것을 고칠 수 있도록 도와주세요 . 어떤 제안도 할 것입니다.

감사

JB29.4

@SMi에게 특별히 감사합니다. 그의 GuidLines를 통해 제 문제를 해결할 수있었습니다. 나는 루프의 끝에서 내부 배열을 지워야한다는 것을 알고 있었다 [하지만 그것을 지우는 방법을 몰랐다.]

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

내가해야 할 일은 .removeAll () 메서드를 사용하여 assetContents 배열 을 지우는 것 입니다.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

API에서 Swift로 JSON 데이터 구문 분석 문제

분류에서Dev

Swift에서 Json 구문 분석

분류에서Dev

AFNetworking에서 JSON 구문 분석-Swift

분류에서Dev

Swift의 OpenWeatherMap에서 JSON 구문 분석

분류에서Dev

Swift 2 구문 분석 및 JSON 읽기

분류에서Dev

PHP에서 JSON 구문 분석 문제

분류에서Dev

powershell에서 json 구문 분석 문제

분류에서Dev

Rails 문제에서 JSON 구문 분석

분류에서Dev

Swift로 Json 구문 분석

분류에서Dev

JSON Swift 구문 분석

분류에서Dev

JSON Swift TableView 구문 분석

분류에서Dev

Swift JSON 구문 분석

분류에서Dev

Swift Alamofire JSON 구문 분석

분류에서Dev

SWIFT 구문 분석 문제

분류에서Dev

JSON 구문 분석 문제

분류에서Dev

JSON 구문 분석 문제

분류에서Dev

Json Object Swift 5 내에서 Json 객체 구문 분석

분류에서Dev

Swift에서 JSON 데이터 (URL에서) 구문 분석

분류에서Dev

angular2에서 JSON 구문 분석

분류에서Dev

JSON.parse에서 문자열 구문 분석 문제

분류에서Dev

Swift에서 동적 선택적 키-값 JSON 구문 분석

분류에서Dev

Swift에서 JSON 배열의 구문 분석 시도

분류에서Dev

Swift에서 Alamofire로 JSON 응답 구문 분석

분류에서Dev

Swift에서 Alamofire로 JSON 응답 구문 분석

분류에서Dev

Swift에서 json 배열을 통한 루핑 구문 분석

분류에서Dev

Swift Issue에서 iOS 앱용 JSON 구문 분석

분류에서Dev

Swift에서 JSON을 구문 분석하는 동안 nil

분류에서Dev

Swift 3에서 임베디드 Json 사전 구문 분석

분류에서Dev

Swift 3에서 JSON 구문 분석시 오류