gpt4 book ai didi

ios - JSON解析(快速)

转载 作者:行者123 更新时间:2023-12-01 19:59:55 25 4
gpt4 key购买 nike

我正在尝试解析此JSON

["Items": <__NSSingleObjectArrayI 0x61000001ec20>(

{

AccountBalance = 0;

AlphabetType = 3;

Description = "\U0631\U06cc\U0648";

FullCode = "P_21_JIM_456_IR_25";

IRNumber = 25;

LeftNumber = 21;

RightNumber = 456;

}

)

, "ErrorCode": 0, "ErrorMessage": , "Result": 1]

在这种情况下如何访问items参数?我正在尝试为Items创建结构,但出现以下错误:无法将类型'(key:String,value:AnyObject)'的值转换为预期的参数类型'[String:AnyObject]'
access Items参数的解决方案是什么?

有我的Items结构并解析JSON代码:
 struct PalletItems {

let accountBalance : Int
let alphabetType : Int
let description : String
let fullCode : String
let irNumber : Int
let leftNumber : Int
let rightNumber : Int

init? (accountBalance: Int, alphabetType: Int, description : String, fullCode: String, irNumber: Int, leftNumber: Int, rightNumber: Int ) {


self.accountBalance = accountBalance
self.alphabetType = alphabetType
self.description = description
self.fullCode = fullCode
self.irNumber = irNumber
self.leftNumber = leftNumber
self.rightNumber = rightNumber

}



func palletListFromJSONData(_ data : Data) -> PaletListResult {



do{
let jsonresult : [String : AnyObject]
= try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) as! [String:AnyObject]
print("json : \(jsonresult)")
let success = jsonresult["Items"] as? PalletItems
print("items is : \(success)")

for paletJson in jsonresult {
if let palet = getBalanceOfWalletFromJsonObject(paletJson) {

finalResult.append(palet)
}
}


let palet = getBalanceOfWalletFromJsonObject(jsonresult)

finalResult.append(palet!)

return .success(finalResult)

}

catch let error as NSError{
print("that is parsing json error : \(error)")

return .failure(error)
}
}


func getBalanceOfWalletFromJsonObject(_ json: [String: AnyObject]) -> ListOfPlates?{

guard let
errorCode = json["ErrorCode"] as? Int,
let errorMessage = json["ErrorMessage"] as? String,
let result = json["Result"] as? Int,
let item = json["Items"] as? PalletItems

else {
return nil
}
let obj = ListOfPlates(errorCode: errorCode, errorMessage: errorMessage, result: result, items: item)

return obj
}

最佳答案

问题是您尝试将其作为键值对进行访问,但将其作为数组获取。所以json应该是[[String:Any]]
现在,此json应该解析为json.first!给你[String:Any]。现在,您可以获得任何键的值。

关于ios - JSON解析(快速),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40327013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com