使用以下代码:
var button = Value.ForStruct(new Struct{
Fields={
["type"] = Value.ForString("postback"),
["title"] = Value.ForString("Call Representative"),
["payload"] = Value.ForString("+15105551234"),
}
});
var inPayload = Value.ForStruct(new Struct{
Fields ={
["buttons"] = Value.ForList(button),
["text"] = Value.ForString("try the postback"),
["template_type"] = Value.ForString("button"),
}
});
var attachment = Value.ForStruct(new Struct{
Fields ={
["payload"] = inPayload,
["type"] = Value.ForString("template"),
}
});
var msg = Value.ForStruct(new Struct{
Fields ={
["attachment"] = attachment,
});
Payload = new Struct{
Fields ={
["facebook"] = msg
}我能够创建以下json:
"payload": {
"facebook": {"attachment": {
"payload": {
"buttons": [ {
"type": "postback",
"title": "Call Representative",
"payload": "+15105551234"
}],
"text": "try the postback",
"template_type": "button"
},
"type": "template"
}}现在我需要创建以下其他格式,但我不知道如何做:
"payload": {
"message": "Yes I did it"
"platform": "kommunicate",
"attachment": {
"payload": {
"buttons": [ {
"type": "postback",
"title": "Call Representative",
"payload": "+15105551234"
}],
"text": "try the postback",
"template_type": "button"
},
"type": "template"
}我真的不知道如何消除第一个"facebook":{元素,只留下:
{
"message": "Yes I did it",
"platform": "kommunicate",
"attachment":并包括同一级别的消息和平台。下面是我想要生成的完整json:
"payload": {
"platform": "kommunicate",
"message": "Yes I did it",
"attachment": {
"payload": {
"buttons": [ {
"type": "postback",
"title": "Call Representative",
"payload": "+15105551234"
}],
"text": "try the postbackggggggg",
"template_type": "button"
},
"type": "template"
}发布于 2020-02-21 23:03:24
如果你想把一个对象转换成json,我建议你看看Newtonsoft Json.Net library。他们有很多examples可以帮助你。还有一个protobuf.net库,用于序列化到protobuf而不是json。
这两个库的使用方式类似,您可以创建一个具有适当属性的类,并设置所需的值。如示例中所示,嵌套类型需要多个类。Protobuf要求您使用属性对属性进行注释,而对于json.net,这是可选的。然后将对象发送到序列化库,并获取表示对象的字符串或二进制数据。这种类型的对象通常被称为Data Transfer Object (DTO),因为它的唯一目的是帮助序列化或/和将数据传输到另一个系统。
https://stackoverflow.com/questions/60341013
复制相似问题