嗨Delphi xe8
System.JSON
无法解析jsonarray
的Json
{" data":{" current_condition":[{" cloudcover":" 0", " FeelsLikeC":" -9"," FeelsLikeF":" 15","湿度":" 93&#34 ;, " observation_time":" 04:10 AM"," degraMM":" 0.0","压力":& #34; 1007&#34 ;, " temp_C":" -6"," temp_F":" 21","可见性":" 10"," weatherCode":" 113", " weatherDesc":[]," weatherIconUrl":[]," winddir16Point":" SE", " winddirDegree":" 130"," windspeedKmph":" 7"," windspeedMiles":" 4& #34; }]} }
memores: TStringList;
currcond: TJSONObject;
memores2.Text := http.Get ('url');
JSONObject := TJSONObject.ParseJSONValue(memores2.Text) as TJSONObject;
Memores1.add('current_condition');
JSONObject2 := JSONObject.GetValue('data') as TJSONObject;
arrayjson := JSONObject2.ParseJSONValue('current_condition') as TJSONArray;
currcond := arrayjson.Items[0] as TJSONObject;
memores1.Add((currcond.GetValue('cloudcover').Value));
答案 0 :(得分:2)
arrayjson:=JSONObject2.ParseJSONValue('current_condition') as TJSONArray;
这会尝试解析文本
current_condition
好像是JSON。它不是。因此arrayjson
是nil
,因此导致运行时错误。用以下代码替换该行代码:
arrayjson := JSONObject2.GetValue('current_condition') as TJSONArray;
例如,这个程序:
{$APPTYPE CONSOLE}
uses
System.SysUtils,
System.JSON;
const
Text =
'{ "data":{ "current_condition":[ { "cloudcover":"0", "FeelsLikeC":"-9", "FeelsLikeF":"15", "humidity":"93", ' +
'"observation_time":"04:10 AM", "precipMM":"0.0", "pressure":"1007", "temp_C":"-6", "temp_F":"21", "visibility":"10", ' +
'"weatherCode":"113", "weatherDesc":[], "weatherIconUrl":[], "winddir16Point":"SE", "winddirDegree":"130", "windspeedKmph":"7", "windspeedMiles":"4" } ] } }';
procedure Main;
var
JSONObject, JSONObject2, currcond: TJSONObject;
arrayjson: TJSONArray;
begin
JSONObject := TJSONObject.ParseJSONValue(Text) as TJSONObject;
JSONObject2 := JSONObject.GetValue('data') as TJSONObject;
arrayjson := JSONObject2.GetValue('current_condition') as TJSONArray;
currcond := arrayjson.Items[0] as TJSONObject;
Writeln(currcond.GetValue('observation_time').Value);
end;
begin
try
Main;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
Readln;
end.
输出
04:10 AM
在上面的简单代码中,我没有尝试添加错误检查。你应该这样做。
答案 1 :(得分:0)
作为提供的解决方案的替代方案,但使用 mORMot:
{$APPTYPE CONSOLE}
uses
SynCommons;
const
Text = '{ "data":{ "current_condition":[ { "cloudcover":"0", "FeelsLikeC":"-9", "FeelsLikeF":"15", "humidity":"93", ' +
'"observation_time":"04:10 AM", "precipMM":"0.0", "pressure":"1007", "temp_C":"-6", "temp_F":"21", "visibility":"10", ' +
'"weatherCode":"113", "weatherDesc":[], "weatherIconUrl":[], "winddir16Point":"SE", "winddirDegree":"130", "windspeedKmph":"7", "windspeedMiles":"4" } ] } }';
var Data , CurrentConditionArray : TDocVariantData;
Current_Condition , Json : Variant;
begin
Json := _Json(Text);
// Alternative 1
Current_Condition := Json.data.current_condition._(0);
Assert( Current_Condition.observation_time = '04:10 AM' );
// Alternative 2 slightly faster
Data := TDocVariantData(Json).O['data']^;
CurrentConditionArray := Data.A['current_condition']^;
Current_Condition := CurrentConditionArray.Values[0];
Assert( TDocVariantData(Current_Condition).Value['precipMM'] = '0.0' );
end.
这应该适用于 Delphi 7 到 10.4。请在 amazing documentation
中找到更多详细信息和替代方案