1

SQL を使用して、以下の JSON から「conversion_event」の値を選択するにはどうすればよいですか?

{"str":[1,1342886173,100000627571405,"offsite_conversion.lead",{"action.type":"offsite_conversion","conversion_event":387756207950188,"tag":"lead"},["conversion_event"],[],{"amount":12623486},"1:11:1:0:0:1:0"]}  

ここには、JSON 内の JSON や角かっこなど、いくつかの珍しいものがあります。すべての値の長さが行ごとに異なるため、設定された数の文字位置でスライスすることはできないと仮定します。

4

1 に答える 1

1

コメントに基づく:

declare @astr varchar(max);
declare @start int;
declare @end int;
set @astr = '{"str":[1,1342886173,100000627571405,"offsite_conversion.lead",{"action.type":"offsite_conversion","conversion_event":387756207950188,"tag":"lead"},["conversion_event"],[],{"amount":12623486},"1:11:1:0:0:1:0"]}';

select @start =  charindex('"conversion_event":',@astr)
select @end = charindex(',"tag":',@astr)

select substring(@astr,@start,@end-@start);

戻り値

"conversion_event":387756207950188

追加

set @start = @start + 19;

番号だけを取得します。


SELECT substring('{"str":[1,1342886173,100000627571405,"offsite_conversion.lead",{"action.type":"offsite_conversion","conversion_event":387756207950188,"tag":"lead"},["conversion_event"],[],{"amount":12623486},"1:11:1:0:0:1:0"]}',
                  101,16);

また

select substring('{"str":[1,1342886173,100000627571405,"offsite_conversion.lead",{"action.type":"offsite_conversion","conversion_event":387756207950188,"tag":"lead"},["conversion_event"],[],{"amount":12623486},"1:11:1:0:0:1:0"]}',
                 151,16)

これはオブジェクトの構造です:

{
  "str":[1,
         1173,
         10005,
         "offsite_conversion.lead",
         {"action.type":"offsite_conversion",
          "conversion_event":387756207950188,
          "tag":"lead"},
        ["conversion_event"],
        [],
        {"amount":14486},
        "1:11:1:0:0:1:0"
]}

配列である属性を持つオブジェクトstr

5 番目の要素には 2 番目の属性変換イベントがあります

6番目の要素は、変換イベントである1つの要素の配列です。

問題は... この構造は同じで、どちらが必要ですか?

于 2012-07-23T23:28:53.517 に答える