简体   繁体   English

如何在Delphi中使用值(而不是对)填充JSON数组?

[英]How do I populate a JSON array with values (not pairs) in Delphi?

I'm trying to create JSON that looks like this: 我正在尝试创建如下所示的JSON:

{ "guestlist": ["alice","bob","charlie"] }

Typical examples I see for populating a JSON array look like this: 我看到用于填充JSON数组的典型示例如下所示:

var 
  jsobj: TJsonObject;      
  jso : TJsonObject;      
  jsa : TJsonArray;
  jsp : TJsonPair;

begin
    jsObj := TJsonObject.Create();
    jsa := TJsonArray.Create();
    jsp := TJSONPair.Create('guestlist', jsa);
    jsObj.AddPair(jsp);

    jso := TJsonObject.Create();
    jso.AddPair(TJSONPair.Create('person', 'alice'));
    jsa.AddElement(jso);

    jso := TJsonObject.Create();
    jso.AddPair(TJSONPair.Create('person', 'bob'));
    jsa.AddElement(jso);

    jso := TJsonObject.Create();
    jso.AddPair(TJSONPair.Create('person', 'charlie'));
    jsa.AddElement(jso);
end;

But that would result in something like this: 但这会导致类似这样的事情:

{ "guestlist": [{"person":"alice"},{"person":"bob"},{"person":"charlie"}] }

How can I add a single value to the array instead of a pair? 如何将单个值添加到数组而不是一对? I see nothing in the documentation for TJsonObject on how to do this, 我在TJsonObject的文档中没有看到如何做到这一点,

This is actually a lot simpler than you're making it out to be. 这实际上比你想要的要简单得多。 A TJSONArray can happily contain any TJSONValue as elements so the solution is really quite straightforward. TJSONArray可以愉快地包含任何TJSONValue作为元素,因此解决方案非常简单。

program Project1;
{$APPTYPE CONSOLE}

uses
  JSON;

var
  LJObj : TJSONObject;
  LGuestList : TJSONArray;
begin

  LGuestlist := TJSONArray.Create();
  LGuestList.Add('alice');
  LGuestList.Add('bob');
  LGuestList.Add('charlie');

  LJObj := TJSONObject.Create;
  LJObj.AddPair(TJSONPair.Create('guestlist', LGuestList));

  WriteLn(LJObj.ToString);
  ReadLn;
end.

Produces output : 产生输出:

{"guestlist":["alice","bob","charlie"]}

Just in case you'd be interested in looking at an alternative: I created jsonDoc , primarily because I like COM interfaces and OleVariants, and dislike long lists of overloads. 如果你有兴趣看一个替代方案:我创建了jsonDoc ,主要是因为我喜欢COM接口和OleVariants,并且不喜欢长的重载列表。 Then the above code could like like this: 那么上面的代码可能是这样的:

JSON(['guestlist',
        VarArrayOf([JSON(['person','alice']),
                    JSON(['person','bob']),
                    JSON(['person','charlie'])
                    ])
    ])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何与FDBatchMoveJsonWriter一起在RAD Studio / Delphi中添加其他JSON对? - How do I add additional JSON pairs in RAD Studio/Delphi in conjunction with FDBatchMoveJsonWriter? 如何将 JSON 对象数据的值填充到选择框中? - How do I populate values of an JSON object data into a selectbox? 如何迭代此json数组并填充选择列表? - How do I iterate over this json array and populate a selectlist? 如何将 JSON 数组填充到 ASP:repeater - How do I populate JSON array to ASP:repeater 如何用JSON数组中的数据填充UITableView? - How do I populate my UITableView with data from JSON array? 如何使用 golang 在 JSON 中填充和附加嵌套数组? - How do I populate and append a nested array in JSON using golang? 如何使用 json 数组创建一个新的 object,只有名称-值对和 javascript? - How do I use a json array to create a new object with only name-value pairs with javascript? 我如何…通过多个cookie的JSON数组循环获取键值对? - How do I for…in loop though this JSON array of multiple cookies to get the key value pairs? 如何通过 json 将键值对数组传递给结构体的 golang 切片 - How do I pass array of key,value pairs to golang slice of struct through json 如何通过在 Flutter 中以 JSON_Serializable 方式遍历 JSON 数组来填充 GridView? - How do I populate a GridView by traversing a JSON array in Flutter the JSON_Serializable way?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM