Delphi - Naruszenie dostępu przy analizowaniu danych JSON @ Run Time
Nowe na forach, więc przepraszam, jeśli moje posty nie są dobrze sformatowane lub nie są zgodne z wytycznymi. Szybko to „dostanę”. Oto mój problem. Spójrz na poniższy kod. Usunąłem prawie wszystkie zewnętrzne bity, aby skupić uwagę na jednej linii klucza -
LParts:=LJsonObj.Get('parts').JsonValue;
Mam kilka danych sformatowanych w JSON na górze (Const), które zostały przetestowane pod kątem poprawności. Gdy próbuję go przeanalizować, nie działa w czasie wykonywania (czas kompilacji jest prawidłowy). Zmniejszyłem to wszystko do mniejszych i łatwiejszych w zarządzaniu.
Kiedy uruchamiam to w debuggerze, wszystko wydaje się ważne, ale nawet dodanie testów dla wartości Nil nie przynosi wiele w moim zrozumieniu problemu.
Mam nadzieję, że ktoś wie, na czym polega problem. Oto fragment kodu:
program ReadJSONConsoleApp;
{$APPTYPE CONSOLE}
{$R *.res}
uses
DBXJSON,
System.SysUtils;
Const
StrJson=
'{' +
' "response": {' +
' "distributor": {' +
' "id": 1538,' +
' "name": "Arrow Electronics",' +
' "authorized": true,' +
' "logoUrl": "this is normally a URL but I cut it out"' +
' },' +
' "parts": [' +
' {' +
' "manufacturer": "National Semiconductor",' +
' "part": "LM741WG/883",' +
' "description": "OP Amp Single GP ±22V 10-Pin CFPAK Tray",' +
' "price": [' +
' {' +
' "quantity": 0,' +
' "price": 65,' +
' "currency": "USD"' +
' }' +
' ],' +
' "stock": 88,' +
' "lastUpdated": "2013-11-04 18:27:16 UTC"' +
' },' +
' {' +
' "manufacturer": "National Semiconductor",' +
' "part": "LM741W/883",' +
' "description": "OP Amp Single GP ±22V 10-Pin CPAK Rail",' +
' "price": [' +
' {' +
' "quantity": 0,' +
' "price": 40.5,' +
' "currency": "USD"' +
' }' +
' ],' +
' "stock": 1464,' +
' "lastUpdated": "2013-11-04 18:27:16 UTC"' +
' },' +
' {' +
' "manufacturer": "Texas Instruments",' +
' "part": "LM741CH",' +
' "description": "OP Amp Single GP ±18V 8-Pin TO-99 Box",' +
' "price": [' +
' {' +
' "quantity": 0,' +
' "price": 5.22,' +
' "currency": "USD"' +
' }' +
' ],' +
' "stock": 95,' +
' "lastUpdated": "2013-11-04 18:27:16 UTC"' +
' }' +
' ]' +
' }' +
'}';
procedure ParseJson;
var
LJsonObj : TJSONObject;
LJPair : TJSONPair;
LParts : TJSONValue;
LPart : TJSONValue;
LItem : TJSONValue;
LIndex : Integer;
LSize : Integer;
begin
LJsonObj := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(StrJson),0) as TJSONObject;
try
LParts:=LJsonObj.Get('parts').JsonValue;
{LSize:=TJSONArray(LParts).Size;
for LIndex:=0 to LSize-1 do
begin
LPart := TJSONArray(LParts).Get(LIndex);
LJPair := TJSONPair(LPart);
Writeln(Format('Part Name %s',[LJPair.JsonString.Value]));
for LItem in TJSONArray(LJPair.JsonValue) do
begin
if TJSONPair(LItem).JsonValue is TJSONFalse then
Writeln(Format(' %s : %s',[TJSONPair(LItem).JsonString.Value, 'false']))
else
if TJSONPair(LItem).JsonValue is TJSONTrue then
Writeln(Format(' %s : %s',[TJSONPair(LItem).JsonString.Value, 'true']))
else
Writeln(Format(' %s : %s',[TJSONPair(LItem).JsonString.Value, TJSONPair(LItem).JsonValue.Value]));
end;
end; }
finally
LJsonObj.Free;
end;
end;
begin
try
ParseJson;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
Readln;
end.
Jeszcze raz dziękuję i dziękuję za cierpliwość.
Z poważaniem,
EEJoe