繁体   English   中英

解码 JSON 问题。 swift中的keyNotFound错误消息

[英]Decoding JSON issue. keyNotFound error message in swift

这是一种 JSON 格式。 我正在尝试从listings读取值 -> images -> firstPhoto -> large

{
"backfillCount": 150,
"breadCrumbs": [3 items],
"dealerNewCount": 420,
"dealerUsedCount": 319,
"enhancedCount": 589,
"facetCountMap": {25 items},
"listings": [
{
"accidentHistory": {4 items},
"advantage": true,
"backfill": false,
"badge": "GREAT",
"bedLength": "Unspecified",
"bodytype": "Sedan",
"cabType": "Unspecified",
"certified": false,
"currentPrice": 31985,
"dealer": {
"address": "10660 Automotive Dr",
"backfill": false,
"carfaxId": "CDOTUSNR41",
"cfxMicrositeUrl": "https://www.carfax.com/Dealer-Joyce-Koons-Honda-Buick-GMC-Manassas-VA_DCTOUSNR41",
"city": "Manassas",
"dealerAverageRating": 4.7,
"dealerInventoryUrl": "http://www.joycekoonshonda.com",
"dealerLeadType": "",
"dealerReviewComments": "Excellent job and excellent customer service. Very good to communicate with the service advisor(Mo Merghane).",
"dealerReviewCount": 354,
"dealerReviewDate": "2020-02-05 08:03:40",
"dealerReviewRating": 5,
"dealerReviewReviewer": "2019 HONDA ACCORD LX Owner",
"dealerReviewTitle": "",
"latitude": "38.799414",
"longitude": "-77.517735",
"name": "Joyce Koons Honda Buick GMC",
"onlineOnly": false,
"phone": "8334980574",
"state": "VA",
"zip": "20109-2643"
},
"dealerType": "NEW",
"displacement": "2.4 L",
"distanceToDealer": 4.410725307988554,
"drivetype": "FWD",
"engine": "4 Cyl",
"exteriorColor": "Blue",
"firstSeen": "2019-12-12",
"followCount": 5,
"following": false,
"fuel": "Gasoline",
"hasViewed": false,
"id": "19UUB1F69LA000169CDOTUSNR4120191212",
"imageCount": 25,
"images": {
"baseUrl": "https://carfax-img.vast.com/carfax/4550688195707539809/",
"firstPhoto": {3 items},
"large": [
"https://carfax-img.vast.com/carfax/4550688195707539809/1/640x480"
],
"medium": [1 item],
"small": [1 item]
},
"interiorColor": "Red",
"isEnriched": false,
"listPrice": 31985,
"make": "Acura",
"mileage": 1550,
"model": "TLX",
"monthlyPaymentEstimate": {7 items},
"mpgCity": 23,
"mpgHighway": 32,
"newTopOptions": [10 items],
"noAccidents": true,
"oneOwner": true,
"onePrice": 33640,
"onePriceArrows": [4 items],
"onlineOnly": false,
"ownerHistory": {4 items},
"personalUse": true,
"recordType": "ENHANCED",
"sentLead": false,
"serviceHistory": {5 items},
"serviceRecords": true,
"sortScore": 191.65,
"stockNumber": "H200327A",
"subTrim": "Unspecified",
"topOptions": [10 items],
"transmission": "Automatic",
"trim": "Technology",
"vdpUrl": "https://www.carfax.com/vehicle/19UUB1F69LA000169",
"vehicleCondition": "Used",
"vehicleUseHistory": {4 items},
"vin": "19UUB1F69LA000169",
"year": 2020
},
{56 items},
{56 items},
{56 items},
{57 items},
{56 items},
{56 items},
{56 items},
{56 items},
{56 items},
{56 items},
{56 items},
{56 items},
{56 items},
{56 items},
{56 items},
{56 items},
{57 items},
{56 items},
{56 items},
{56 items},
{56 items},
{56 items},
{56 items},
{56 items}
],
"page": 1,
"pageSize": 25,
"relatedLinks": {2 items},
"searchArea": {8 items},
"searchRequest": {8 items},
"seoUrl": "Used-Acura_m1",
"totalListingCount": 739,
"totalPageCount": 30
}

我的结构格式是:

struct CarInfoResponse: Decodable {
    var listings: [CarInfoDetail]
}

struct CarInfoDetail: Decodable {
    var year: Int
    var make: String
    var model: String
    var mileage: Int
    var trim: String
    var currentPrice: Int
    var dealer: Address
    var images: Images
}

struct Address: Decodable {
    var address: String
    var phone: String
}
struct Images: Decodable {
    var baseUrl: String
    var firstPhoto: FirstPhoto
}

struct FirstPhoto: Decodable {
    var large: String
}

我从获得价值baseUrl但不能从firstPhoto

我收到此错误:

我的错误:keyNotFound(CodingKeys(stringValue: "firstPhoto", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "listings", intValue: nil), _JSONKey(stringValue: "Index 21", intValue : 21), CodingKeys(stringValue: "images", intValue: nil)], debugDescription: "No value with key CodingKeys(stringValue: "firstPhoto", intValue: nil) ("firstPhoto").",underlyingError: nil) )

如何获取大图网址? 提前致谢。

基于解码错误, listings列表的第 22 个元素对于images对象中的firstPhoto键没有值(或键不存在)。

所以。 你的Images结构应该是这样的:

struct Images: Decodable {
    var baseUrl: String
    var firstPhoto: FirstPhoto?
}

firstPhoto属性更改为可选。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM