在 C# 中创建嵌套的 JSON 对象
本文关键字:JSON 对象 嵌套 创建 | 更新日期: 2023-09-27 17:57:05
使用 BOOMLAGOON 库。
我正在尝试使用 Unity3d 在 C# 中生成一个 JSON 对象。
string GroupName="Night Fighters";
string GroupMemberID="TG1";
string GroupMemberName="Tommy Gun";
string GroupEquipmentDescription="Tommy Gun";
public JSONObject Testjson()
{
var data = new JSONObject
{
{"Group Name" , GroupName},
{"Group Member" , GroupMemberID, GroupMemberName},
{"Group Equipment" , GroupEquipmentID, GroupEquipmentDescription}
};
Debug.Log(data);
return data;
}
它告诉我重载是错误的 - add 需要三个参数。不确定如何做嵌套成员或设备。我已经尝试过 () 和 {} 和 []。什么都没用。
更有可能说明它不需要 3 个参数:
public void Add(string key, JSONValue value) {
values[key] = value;
}
public void Add(KeyValuePair<string, JSONValue> pair) {
values[pair.Key] = pair.Value;
}
但这是你要做的:
var data = new JSONObject
{
{"Group Name" , GroupName},
{"Group Member" , GroupMemberID, GroupMemberName}, // here three
{"Group Equipment" , GroupEquipmentID, GroupEquipmentDescription} // Here three
};
您必须首先创建一个 json 对象,在其中添加两个值,然后在 ctor 中传递该单个顶部对象。