0

Delphi XE2 を使用して Web サイトから JSON を解析しようとしましたが、このスレッドで提案されているコードは hereです。回答者の投稿に表示されているとおりにコードを実装すると機能しますが、ほとんど同じものを実装すると、次の行でアクセス違反が発生します。

LParts:=LJsonObj.Get('parts').JsonValue;

そして、(実行時に) 毎回 AV で失敗します (正常にコンパイルされますが、実行されません)。コードを簡略化しました。以下は、動作すると思われるスニペットです。結局のところ、これは他の動作中のコードのコピー アンド ペーストです。

誰かが以下を見て、なぜ私のものが機能しないのに、参照された投稿のコードが機能するのかを教えていただければ、本当に感謝しています。デバッグすると、動作しているように見え、デバッグ情報がほとんどないため、私は主に困惑しています。

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.0,' +
'        "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"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741CN",' +
'      "description" : "OP Amp Single GP ±18V 8-Pin PDIP Rail",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 0.3633,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 4320,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741H",' +
'      "description" : "OP Amp Single GP ±22V 8-Pin TO-99 Box",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 5.22,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 3458,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741H",' +
'      "description" : "OP Amp Single GP ±22V 8-Pin TO-99 Box",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 5.71,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 0,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741CN/NOPB",' +
'      "description" : "OP Amp Single GP ±18V 8-Pin PDIP Rail",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 0.2977,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 6486,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741J",' +
'      "description" : "OP Amp Single GP ±22V 8-Pin CDIP Rail",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 7.21,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 362,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741H/NOPB",' +
'      "description" : "OP Amp Single GP ±22V 8-Pin TO-99 Box",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 5.22,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 1378,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741J/883",' +
'      "description" : "OP Amp Single GP ±22V 8-Pin CDIP Rail",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 11.8,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 989,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741H/883",' +
'      "description" : "OP Amp Single GP ±22V 8-Pin TO-99 Tray",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 15.74,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 4252,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741CHNOPB",' +
'      "description" : "OP Amp Single GP ±18V 8-Pin TO-99 Box",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 5.22,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 785,' +
'      "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.
4

1 に答える 1

1

への呼び出しParseJSONValueは nil を返します。ドキュメントはこれを呼び出します:

ParseJSONValue は、解析されたデータに対応する JSON 値を返します。解析に失敗した場合は null を返します。

このような状態をチェックしないと、アクセス違反は避けられない結果になります。JSON が事前に検証されていない限り、有効性を確認する必要があります。

JSONをオンラインバリデーターに貼り付けることで、これを自分で再確認できます。例: http://jsonlint.com/これを行うと、JSON が無効であることがわかります。

有効な JSON を次に示します。

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.0,' +
'        "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"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741CN",' +
'      "description" : "OP Amp Single GP ±18V 8-Pin PDIP Rail",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 0.3633,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 4320,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741H",' +
'      "description" : "OP Amp Single GP ±22V 8-Pin TO-99 Box",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 5.22,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 3458,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741H",' +
'      "description" : "OP Amp Single GP ±22V 8-Pin TO-99 Box",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 5.71,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 0,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741CN/NOPB",' +
'      "description" : "OP Amp Single GP ±18V 8-Pin PDIP Rail",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 0.2977,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 6486,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741J",' +
'      "description" : "OP Amp Single GP ±22V 8-Pin CDIP Rail",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 7.21,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 362,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741H/NOPB",' +
'      "description" : "OP Amp Single GP ±22V 8-Pin TO-99 Box",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 5.22,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 1378,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741J/883",' +
'      "description" : "OP Amp Single GP ±22V 8-Pin CDIP Rail",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 11.8,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 989,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741H/883",' +
'      "description" : "OP Amp Single GP ±22V 8-Pin TO-99 Tray",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 15.74,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 4252,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }, {' +
'      "manufacturer" : "Texas Instruments",' +
'      "part" : "LM741CHNOPB",' +
'      "description" : "OP Amp Single GP ±18V 8-Pin TO-99 Box",' +
'      "price" : [ {' +
'        "quantity" : 0,' +
'        "price" : 5.22,' +
'        "currency" : "USD"' +
'      } ],' +
'      "stock" : 785,' +
'      "lastUpdated" : "2013-11-04 18:27:16 UTC"' +
'    }' +
'    ]' +
'  }' +
'}';

それがあなたの望むものかどうか、私には確信が持てません。

この定数をプログラムにプラグインすると、さらに進みますが、途中で失敗しGet('parts')ます。それも返さnilれ、プロパティを読み取ろうとすると別の AV が取得されJsonValueます。

あなたが何をしているのか正確にはわからないので、コード全体をデバッグする方法を教えることはできません. あなたがしなければならないことは、これらの JSON メソッドとプロパティがnil. したがって、参照のメソッドとプロパティにアクセスする前に、それを確認してnilください。

もう 1 つのアドバイスは、JSON をファイルに入れることです。これにより、作業が容易になります。このように定数を構築すると、デバッグが非常に難しくなります。

于 2013-11-04T22:25:25.163 に答える