You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

[ |../../../index.htm#lansa/jsoneng01_0025.htm]
現在地: Webサービス > JSON データの作業 > XPRIM_RandomAccessJsonReader を使用した JSON 値の読み込み

XPRIM_RandomAccessJsonReader を使用した、JSON 値の読み込み

XPRIM_RandomAccessJsonReader により、JSON 文字列または HTTP 応答本文の JSON 値に効率よくアクセスできるようになります。XPRIM_RandomAccessJsonReader の「ランダム・アクセス」は、このリーダーの性質を表しています。つまり、JSON 文字列内に存在するエレメントの値ならどれでも、いつでも取得することができます。JSON 値を順番に読み込む必要がないのが、XPRIM_JsonReader です。(これについては次に説明します)。
まず、XPRIM_RandomAccessJsonReader オブジェクトを作成します。
Define_Com Class(#XPRIM_RandomAccessJsonReader) Name(#Reader)
次のいずれかのメソッドを使って、リーダーのソースを設定します。

  • SetSourceString (文字列からの読み込み)
  • SetSourceHttpResponse (HTTP 要求からの応答本文の読み込み)
  • SetSourceFile (ファイルからの読み込み)

例として、前のセクションで使用した JSON 文字列を使用します。
{
    "name":     {
        "given":"John",
        "surname":"Smith"
    },
    "age":45,
    "contactNo":
    [
        { area:"02", no:"9378 2867", type:"landline" },
        { no:"0468 732 371", type:"mobile" }
    ]
}
これは #Request という名前の HTTP 要求からの応答だとしましょう。SetSourceHttpResponse を使って、この応答本文から読み込みます。
#Reader.SetSourceHttpResponse HttpResponse(#Request.Response)
JSON から値を抽出する際に利用できる Read… メソッドには、以下のよう な 3 種類があります。

  • Read…WithPath
  • Read…WithName
  • Read…AtIndex

それぞれに以下のようなバリアントが含まれています。

  • ReadString…
  • ReadNumber…
  • ReadBoolean…
  • ReadNull…
  • ReadObject…
  • ReadArray…
  • ReadElement…

オブジェクトまたはアレイに「入る」ために、次の 2 セットの Begin… メソッドがあります。

  • BeginObjectWithPath
  • BeginObjectWithName
  • BeginObjectAtIndex
  • BeginArrayWithPath
  • BeginArrayWithName
  • BeginArrayAtIndex

The Begin… methods navigates into the specified element.終了して、親エレメントに戻るには、対応する End… メソッドを使用します。( EndObject と EndArray)。
All Read…/Begin… メソッドには、Found というブール値の出力パラメーターがあり、これが値の取得に成功したかどうかを示します。
次は: Read…WithPath メソッド
[ |../../../index.htm#lansa/jsoneng01_0025.htm]

  • No labels