2016-08-03 12 views
2

へのアクセスI持って次のようなJSON構造:は、内部JSON値

concurrence: [ 
    { 
    id: "481-13", 
    price: "11.5", 
    concurrent: { 
     id: 13, 
     name: "NAME1", 
     price: "11.5" 
    } 
    }, 
    { 
    id: "481-14", 
    price: "12.25", 
    concurrent: { 
     id: 14, 
     name: "NAME2", 
     price: "12.25" 
    } 
    } 
] 
私は concurrent.id値を取得できますか

? RADStudioのドキュメントに記載されているFindValue()メソッドを使用しようとしましたが、少なくとも10.1ベルリンでは存在しません。

は、私はこのような同意アレイにアクセスしています:

concurrents := product.Get('concurrence').JSONValue as TJSONArray; 
for j := 0 to concurrents.Size-1 do 
begin 
    concurrent := concurrents.Get(j) as TJSONObject; 
    json_s := concurrent.Get('id').JsonValue as TJSONString; 
    my_id := json_s.Value; 
    json_s := concurrent.Get('price').JsonValue as TJSONString; 
    my_price := json_s.Value; 
    json_s := concurrent.FindValue('concurrent.id') as TJSONString;//NOT WORKING 
    my_concurrent_id := json_s.Value; 
end; 

インナーconcurrent値にアクセスするための別の方法はありますか?

+0

はそれだけではなくjson_sです:= concurrent.Get( 'idが')TJSONString – Dsm

+0

としてあなたの最初の「IDを与えること"フィールドではなく、"並行 "の下にあるフィールドです。 JSONの実装が不十分ですが、私はそれに対処しなければなりません。 –

+1

目的の項目を見つけてそのオブジェクトを読み取るまで、配列全体を繰り返します。 –

答えて

6

concurrentのアイテムには、他のすべてのJSONオブジェクトと同じ方法でアクセスします。

JSONオブジェクト自体を取得するだけで済みます。しかし、これは簡単です:

concurrent_sub:= concurrent.Get('concurrent').JsonValue as TJSONObject; 
json_s := concurrent_sub.Get('id').JsonValue as TJSONString; 
1

個人的に私はむしろJsonDataObjectsを使いたいと思います。

その後のコードは次のようになります。http://www.jsoneditoronline.org/あなたのJSONによると

var 
    product: TJsonObject; 
    concurrents: TJsonArray; 
    o: TJsonObject; 
begin 
    ... 
    concurrents := product.A['concurrence']; 
    for o in concurrents do 
    Writeln(o.O['concurrent'].I['id']); 
+0

なぜdownvote?問題は、 'System.Json'を使った解決策を求めていませんでしたか? –

+0

ちょうど迷信ですが、最近はとても慣れています:-) –

1

は不正な形式です。

正しいものは波括弧の中に置かれる必要があるでしょう:使用して、このあなただけJsonVar['concurrence[0].concurrent.id']として、それに対処することができます補正とJsonVar['concurrence[1].concurrent.id']

SuperObject parsing

{ 
    concurrence: [ 
    { 
     id: "481-13", 
     price: "11.5", 
     concurrent: { 
     id: 13, 
     name: "NAME1", 
     price: "11.5" 
     } 
    }, 
    { 
     id: "481-14", 
     price: "12.25", 
     concurrent: { 
     id: 14, 
     name: "NAME2", 
     price: "12.25" 
     } 
    } 
    ] 
    } 

https://github.com/hgourvest/superobject

var 
    obj, id: ISuperObject; 
    N, M: Integer; 
begin 
    obj := SO('{ concurrence: [ { id: ....... '); 

    id := obj[ 'concurrence[0].concurrent.id' ]; 
    if id <> nil then 
    N := id.AsInteger;       // N <== 13 

    M := obj.I[ 'concurrence[1].concurrent.id' ]; // M <== 14 

    obj := nil; 
    id := nil; 
end; 

後者のオプションは簡単ですが少しフレークです。 nilを返すことができないので、実際の値が見つからない場合は、default(Integer)ゼロを返します。

このクイック構文解析構文もJSON-が、-ない - JavaScriptのペイロードでfailes:https://github.com/hgourvest/superobject/issues/8

関連する問題