如何从 Delphi 中的 JSON 获取 @odata.etag 值
How to get @odata.etag value from JSON in Delphi
谁能解释一下,为什么@odata.etag 键的行为与其他键不同?为什么我不能像从其他密钥中那样从中获取价值(以及如何获取)?
using ... System.Generics.Collections, System.JSON
procedure TFMain.Button1Click(Sender: TObject);
var
JSonValue, SingleValue: TJSonValue;
ValueArray: TJsonArray;
begin
JSONValue := TJSONObject.ParseJSONValue('{"@odata.context": "https://blablabla", "value": [{"@odata.etag": "W/\"blablan\"", "No": "X0001" }]}');
ValueArray := (JSONValue as TJSONObject).GetValue('value') as TJSONArray;
SingleValue := ValueArray.Items[0];
memo1.Lines.Add(JSONValue.GetValue<string>('value[0].No')); //works , no double quotes
memo1.Lines.Add(JSONValue.GetValue<string>('value[0].@odata.etag')); //not works
memo1.Lines.Add((SingleValue as TJSONObject).GetValue('No').ToString); //works, double quotes
memo1.Lines.Add((SingleValue as TJSONObject).GetValue('@odata.etag').ToString); //works, double quotes
memo1.lines.Add(SingleValue.GetValue<string>('No')); //works , no double quotes
memo1.lines.Add(SingleValue.GetValue<string>('@odata.etag')); //not works
end;
此致。
答案很简单 - 因为键包含点字符 (.
),它在 JSON 路径中用作键分隔符,因此需要特殊处理。
要访问 JSON 路径中的此类键,请使用带引号的索引器:
Memo1.Lines.Add(JSONValue.GetValue<string>('value[0]["@odata.etag"]'));
单引号索引器也可以。您还可以在 TJSONPathParser
文档中找到它。
另一方面TJSONObject.GetValue
(non-generic method of TJSONObject
) works because it takes single key, not the whole path as an argument. You observed double quotes when you used ToString
which is expected and very well documented:
...
This format is more readable than the one returned by .ToJSON.
The string is double-quoted.
要获取原始字符串值,请使用 Value
属性:
memo1.Lines.Add((SingleValue as TJSONObject).GetValue('@odata.etag').Value);
谁能解释一下,为什么@odata.etag 键的行为与其他键不同?为什么我不能像从其他密钥中那样从中获取价值(以及如何获取)?
using ... System.Generics.Collections, System.JSON
procedure TFMain.Button1Click(Sender: TObject);
var
JSonValue, SingleValue: TJSonValue;
ValueArray: TJsonArray;
begin
JSONValue := TJSONObject.ParseJSONValue('{"@odata.context": "https://blablabla", "value": [{"@odata.etag": "W/\"blablan\"", "No": "X0001" }]}');
ValueArray := (JSONValue as TJSONObject).GetValue('value') as TJSONArray;
SingleValue := ValueArray.Items[0];
memo1.Lines.Add(JSONValue.GetValue<string>('value[0].No')); //works , no double quotes
memo1.Lines.Add(JSONValue.GetValue<string>('value[0].@odata.etag')); //not works
memo1.Lines.Add((SingleValue as TJSONObject).GetValue('No').ToString); //works, double quotes
memo1.Lines.Add((SingleValue as TJSONObject).GetValue('@odata.etag').ToString); //works, double quotes
memo1.lines.Add(SingleValue.GetValue<string>('No')); //works , no double quotes
memo1.lines.Add(SingleValue.GetValue<string>('@odata.etag')); //not works
end;
此致。
答案很简单 - 因为键包含点字符 (.
),它在 JSON 路径中用作键分隔符,因此需要特殊处理。
要访问 JSON 路径中的此类键,请使用带引号的索引器:
Memo1.Lines.Add(JSONValue.GetValue<string>('value[0]["@odata.etag"]'));
单引号索引器也可以。您还可以在 TJSONPathParser
文档中找到它。
另一方面TJSONObject.GetValue
(non-generic method of TJSONObject
) works because it takes single key, not the whole path as an argument. You observed double quotes when you used ToString
which is expected and very well documented:
...
This format is more readable than the one returned by .ToJSON.
The string is double-quoted.
要获取原始字符串值,请使用 Value
属性:
memo1.Lines.Add((SingleValue as TJSONObject).GetValue('@odata.etag').Value);