delphi Delph Parse Json问题

gzjq41n4  于 2023-06-22  发布在  其他
关注(0)|答案(1)|浏览(131)

我在试着解码汽车的车牌号码。
我需要将查询发布到API并获得结果。
我在这里从一个API得到一个结果:
https://vpic.nhtsa.dot.gov/api/vehicles/decodevin/1gt125e85df163635?format=json &
下面是响应JSON:

{
"Count": 136,
"Message": "Results returned successfully. NOTE: Any missing decoded values should be interpreted as NHTSA does not have data on the specific variable. Missing value should NOT be interpreted as an indication that a feature or technology is unavailable for a vehicle.",
"SearchCriteria": "VIN:1gt125e85df163635",
"Results": [
    {
        "Value": "",
        "ValueId": "",
        "Variable": "Suggested VIN",
        "VariableId": 142
    },
    {
        "Value": "1",
        "ValueId": "1",
        "Variable": "Error Code",
        "VariableId": 143
    },
    {
        "Value": "",
        "ValueId": "",
        "Variable": "Possible Values",
        "VariableId": 144
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Additional Error Text",
        "VariableId": 156
    },
    {
        "Value": "1 - Check Digit (9th position) does not calculate properly",
        "ValueId": "",
        "Variable": "Error Text",
        "VariableId": 191
    },
    {
        "Value": "1GT125E8*DF",
        "ValueId": "",
        "Variable": "Vehicle Descriptor",
        "VariableId": 196
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Destination Market",
        "VariableId": 10
    },
    {
        "Value": "GMC",
        "ValueId": "472",
        "Variable": "Make",
        "VariableId": 26
    },
    {
        "Value": "GENERAL MOTORS LLC",
        "ValueId": "984",
        "Variable": "Manufacturer Name",
        "VariableId": 27
    },
    {
        "Value": "Sierra",
        "ValueId": "1857",
        "Variable": "Model",
        "VariableId": 28
    },
    {
        "Value": "2013",
        "ValueId": "",
        "Variable": "Model Year",
        "VariableId": 29
    },
    {
        "Value": "FLINT",
        "ValueId": "",
        "Variable": "Plant City",
        "VariableId": 31
    },
    {
        "Value": "2500",
        "ValueId": "",
        "Variable": "Series",
        "VariableId": 34
    },
    {
        "Value": "Denali",
        "ValueId": "",
        "Variable": "Trim",
        "VariableId": 38
    },
    {
        "Value": "TRUCK ",
        "ValueId": "3",
        "Variable": "Vehicle Type",
        "VariableId": 39
    },
    {
        "Value": "UNITED STATES (USA)",
        "ValueId": "6",
        "Variable": "Plant Country",
        "VariableId": 75
    },
    {
        "Value": "GMNA",
        "ValueId": "",
        "Variable": "Plant Company Name",
        "VariableId": 76
    },
    {
        "Value": "MICHIGAN",
        "ValueId": "",
        "Variable": "Plant State",
        "VariableId": 77
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Trim2",
        "VariableId": 109
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Series2",
        "VariableId": 110
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Note",
        "VariableId": 114
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Base Price ($)",
        "VariableId": 136
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Non-Land Use",
        "VariableId": 195
    },
    {
        "Value": "Pickup",
        "ValueId": "60",
        "Variable": "Body Class",
        "VariableId": 5
    },
    {
        "Value": "4",
        "ValueId": "",
        "Variable": "Doors",
        "VariableId": 14
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Windows",
        "VariableId": 40
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Wheel Base Type",
        "VariableId": 60
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Track Width (inches)",
        "VariableId": 159
    },
    {
        "Value": "Class 2H: 9,001 - 10,000 lb (4,082 - 4,536 kg)",
        "ValueId": "17",
        "Variable": "Gross Vehicle Weight Rating From",
        "VariableId": 25
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Bed Length (inches)",
        "VariableId": 49
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Curb Weight (pounds)",
        "VariableId": 54
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Wheel Base (inches) From",
        "VariableId": 111
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Wheel Base (inches) To",
        "VariableId": 112
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Gross Combination Weight Rating From",
        "VariableId": 184
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Gross Combination Weight Rating To",
        "VariableId": 185
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Gross Vehicle Weight Rating To",
        "VariableId": 190
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Bed Type",
        "VariableId": 3
    },
    {
        "Value": "Crew\/Super Crew\/Crew Max",
        "ValueId": "4",
        "Variable": "Cab Type",
        "VariableId": 4
    },
    {
        "Value": "Not Applicable",
        "ValueId": "0",
        "Variable": "Trailer Type Connection",
        "VariableId": 116
    },
    {
        "Value": "Not Applicable",
        "ValueId": "0",
        "Variable": "Trailer Body Type",
        "VariableId": 117
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Trailer Length (feet)",
        "VariableId": 118
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Other Trailer Info",
        "VariableId": 155
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Number of Wheels",
        "VariableId": 115
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Wheel Size Front (inches)",
        "VariableId": 119
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Wheel Size Rear (inches)",
        "VariableId": 120
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Entertainment System",
        "VariableId": 23
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Steering Location",
        "VariableId": 36
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Number of Seats",
        "VariableId": 33
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Number of Seat Rows",
        "VariableId": 61
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Transmission Style",
        "VariableId": 37
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Transmission Speeds",
        "VariableId": 63
    },
    {
        "Value": "4WD\/4-Wheel Drive\/4x4",
        "ValueId": "2",
        "Variable": "Drive Type",
        "VariableId": 15
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Axles",
        "VariableId": 41
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Axle Configuration",
        "VariableId": 145
    },
    {
        "Value": "Hydraulic",
        "ValueId": "2",
        "Variable": "Brake System Type",
        "VariableId": 42
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Brake System Description",
        "VariableId": 52
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Other Battery Info",
        "VariableId": 1
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Battery Type",
        "VariableId": 2
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Number of Battery Cells per Module",
        "VariableId": 48
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Battery Current (Amps) From",
        "VariableId": 57
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Battery Voltage (Volts) From",
        "VariableId": 58
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Battery Energy (kWh) From",
        "VariableId": 59
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "EV Drive Unit",
        "VariableId": 72
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Battery Current (Amps) To",
        "VariableId": 132
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Battery Voltage (Volts) To",
        "VariableId": 133
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Battery Energy (kWh) To",
        "VariableId": 134
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Number of Battery Modules per Pack",
        "VariableId": 137
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Number of Battery Packs per Vehicle",
        "VariableId": 138
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Charger Level",
        "VariableId": 127
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Charger Power (kW)",
        "VariableId": 128
    },
    {
        "Value": "8",
        "ValueId": "",
        "Variable": "Engine Number of Cylinders",
        "VariableId": 9
    },
    {
        "Value": "6600.0",
        "ValueId": "",
        "Variable": "Displacement (CC)",
        "VariableId": 11
    },
    {
        "Value": "402.75671102523",
        "ValueId": "",
        "Variable": "Displacement (CI)",
        "VariableId": 12
    },
    {
        "Value": "6.6",
        "ValueId": "",
        "Variable": "Displacement (L)",
        "VariableId": 13
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Engine Stroke Cycles",
        "VariableId": 17
    },
    {
        "Value": "LML",
        "ValueId": "",
        "Variable": "Engine Model",
        "VariableId": 18
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Engine Power (kW)",
        "VariableId": 21
    },
    {
        "Value": "Diesel",
        "ValueId": "1",
        "Variable": "Fuel Type - Primary",
        "VariableId": 24
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Valve Train Design",
        "VariableId": 62
    },
    {
        "Value": "V-Shaped",
        "ValueId": "2",
        "Variable": "Engine Configuration",
        "VariableId": 64
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Fuel Type - Secondary",
        "VariableId": 66
    },
    {
        "Value": "Common Rail Direct Injection Diesel (CRDI)",
        "ValueId": "6",
        "Variable": "Fuel Delivery \/ Fuel Injection Type",
        "VariableId": 67
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Engine Brake (hp) From",
        "VariableId": 71
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Cooling Type",
        "VariableId": 122
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Engine Brake (hp) To",
        "VariableId": 125
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Electrification Level",
        "VariableId": 126
    },
    {
        "Value": "Engine Model: LML, Engine Diesel, 8 Cyl, 6.6L, PTI, V8, Turbo HO, Aluminium  Engine Model: LUJ, Engine Gas, 4Cyl, L4,MFI, DOHC, Turbo HO, Aluminium",
        "ValueId": "",
        "Variable": "Other Engine Info",
        "VariableId": 129
    },
    {
        "Value": "Yes",
        "ValueId": "1",
        "Variable": "Turbo",
        "VariableId": 135
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Top Speed (MPH)",
        "VariableId": 139
    },
    {
        "Value": "DURAMAX",
        "ValueId": "",
        "Variable": "Engine Manufacturer",
        "VariableId": 146
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Pretensioner",
        "VariableId": 78
    },
    {
        "Value": "Manual",
        "ValueId": "1",
        "Variable": "Seat Belt Type",
        "VariableId": 79
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Other Restraint System Info",
        "VariableId": 121
    },
    {
        "Value": "All Rows",
        "ValueId": "6",
        "Variable": "Curtain Air Bag Locations",
        "VariableId": 55
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Seat Cushion Air Bag Locations",
        "VariableId": 56
    },
    {
        "Value": "1st Row (Driver and Passenger)",
        "ValueId": "3",
        "Variable": "Front Air Bag Locations",
        "VariableId": 65
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Knee Air Bag Locations",
        "VariableId": 69
    },
    {
        "Value": "1st Row (Driver and Passenger)",
        "ValueId": "3",
        "Variable": "Side Air Bag Locations",
        "VariableId": 107
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Anti-lock Braking System (ABS)",
        "VariableId": 86
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Electronic Stability Control (ESC)",
        "VariableId": 99
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Traction Control",
        "VariableId": 100
    },
    {
        "Value": "Direct",
        "ValueId": "1",
        "Variable": "Tire Pressure Monitoring System (TPMS) Type",
        "VariableId": 168
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Active Safety System Note",
        "VariableId": 169
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Auto-Reverse System for Windows and Sunroofs",
        "VariableId": 172
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Automatic Pedestrian Alerting Sound (for Hybrid and EV only)",
        "VariableId": 173
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Event Data Recorder (EDR)",
        "VariableId": 175
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Keyless Ignition",
        "VariableId": 176
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "SAE Automation Level From",
        "VariableId": 181
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "SAE Automation Level To",
        "VariableId": 182
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Adaptive Cruise Control (ACC)",
        "VariableId": 81
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Crash Imminent Braking (CIB)",
        "VariableId": 87
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Blind Spot Warning (BSW)",
        "VariableId": 88
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Forward Collision Warning (FCW)",
        "VariableId": 101
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Lane Departure Warning (LDW)",
        "VariableId": 102
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Lane Keeping Assistance (LKA)",
        "VariableId": 103
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Backup Camera",
        "VariableId": 104
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Parking Assist",
        "VariableId": 105
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Bus Length (feet)",
        "VariableId": 147
    },
    {
        "Value": "Not Applicable",
        "ValueId": "0",
        "Variable": "Bus Floor Configuration Type",
        "VariableId": 148
    },
    {
        "Value": "Not Applicable",
        "ValueId": "0",
        "Variable": "Bus Type",
        "VariableId": 149
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Other Bus Info",
        "VariableId": 150
    },
    {
        "Value": "Not Applicable",
        "ValueId": "0",
        "Variable": "Custom Motorcycle Type",
        "VariableId": 151
    },
    {
        "Value": "Not Applicable",
        "ValueId": "0",
        "Variable": "Motorcycle Suspension Type",
        "VariableId": 152
    },
    {
        "Value": "Not Applicable",
        "ValueId": "0",
        "Variable": "Motorcycle Chassis Type",
        "VariableId": 153
    },
    {
        "Value": null,
        "ValueId": "",
        "Variable": "Other Motorcycle Info",
        "VariableId": 154
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Dynamic Brake Support (DBS)",
        "VariableId": 170
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Pedestrian Automatic Emergency Braking (PAEB)",
        "VariableId": 171
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Automatic Crash Notification (ACN) \/ Advanced Automatic Crash Notification (AACN)",
        "VariableId": 174
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Daytime Running Light (DRL)",
        "VariableId": 177
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Headlamp Light Source",
        "VariableId": 178
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Semiautomatic Headlamp Beam Switching",
        "VariableId": 179
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Adaptive Driving Beam (ADB)",
        "VariableId": 180
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Rear Cross Traffic Alert",
        "VariableId": 183
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Rear Automatic Emergency Braking",
        "VariableId": 192
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Blind Spot Intervention (BSI)",
        "VariableId": 193
    },
    {
        "Value": null,
        "ValueId": null,
        "Variable": "Lane Centering Assistance",
        "VariableId": 194
    }
]

代码如下:

procedure TForm3.Button1Click(Sender: TObject);
 var MainJson, Result : Tjsonobject;
begin
  uJasonDataModule.RESTRequest1.Execute;
  memJasonResults.Text := uJasonDataModule.RESTResponse1.JSONText;
  Result := Tjsonobject.ParseJSONValue(uJasonDataModule.RESTResponse1.Content) as Tjsonobject ;
  MainJson := Result.GetValue<tjsonObject>('Results');
  Label12.Text := MainJson.GetValue<string>('VariableId');
end;

2013款GMC Sierra:

{
        "Value": "2013",
        "ValueId": "",
        "Variable": "Model Year",
        "VariableId": 29
  },
  {
        "Value": "Sierra",
        "ValueId": "1857",
        "Variable": "Model",
        "VariableId": 28
  },
  {
        "Value": "GMC",
        "ValueId": "472",
        "Variable": "Make",
        "VariableId": 26
  },

现在,我得到了一个错误:
无效的类类型转换

我从来没有使用过JSON。

qeeaahzv

qeeaahzv1#

首先,您不需要手动解析TRESTResponse.ContentTRESTResponse有属性可以为您处理。在这种情况下,请使用TRESTResponse.JSONValue属性,例如:

Result := uJasonDataModule.RESTResponse1.JSONValue as TJSONObject;

其次,Results字段不是JSON对象,因此将其强制转换为TJSONObject是错误的,这可能是错误的来源。Results是一个JSON数组,包含JSON对象元素。
因此,你需要将它转换为TJSONArray,然后枚举它的元素,将每个元素转换为TJONObject,这样你就可以根据需要检查它的Variable字段,例如:

procedure TForm3.Button1Click(Sender: TObject);
var
  Result, Elem: TJSONObject;
  MainJson: TJSONArray;
  Variable, VinMake, VinModel, VinYear: String;
  I: Integer;
begin
  uJasonDataModule.RESTRequest1.Execute;
  memJasonResults.Text := uJasonDataModule.RESTResponse1.JSONText;
  Result := uJasonDataModule.RESTResponse1.JSONValue as TJSONObject;
  MainJson := Result.GetValue<TJSONArray>('Results');
  for I := 0 to MainJson.Length-1 do
  begin
    Elem := MainJson[I] as TJSONObject;
    Variable := Elem.GetValue<string>('Variable');
    if Variable = 'Model Year' then begin
      VinYear := Elem.GetValue<string>('Value');
    end
    else if Variable = 'Model' then begin
      VinModel := Elem.GetValue<string>('Value');
    end
    else if Variable = 'Make' then begin
      VinMake := Elem.GetValue<string>('Value');
    end;
  end;
  Label12.Text := Format('%s %s %s', [VinYear, VinMake, VinModel]);
end;

相关问题