无法将我的 class 序列化为所需的 json 文件
Can not serialize my class to the desired json file
我使用 Newtonsoft.Json 库进行反序列化和序列化。我正在尝试将 C# class 序列化为 json,我已经将许多 classes 序列化为 json 但我从未遇到过这样的 json文件。问题是第一个 "text" 属性后面没有跟一个开括号,而且 "platform" 属性在 fulfillmentMessage 中重复(已经存在)。
我的Class:
public class Fulfillmentmessage
{
public string platform { get; set; }
[JsonProperty("simpleResponses ", NullValueHandling = NullValueHandling.Ignore)]
public Simpleresponses simpleResponses { get; set; }
[JsonProperty("basicCard", NullValueHandling = NullValueHandling.Ignore)]
public Basiccard basicCard { get; set; }
}
public class Simpleresponses
{
public Simplerespons[] simpleResponses { get; set; }
}
public class Simplerespons
{
public string textToSpeech { get; set; }
}
public class Texts
{
public Text text { get; set; }
}
public class Text
{
public List<string> text { get; set; }
}
public class Basiccard
{
public string title { get; set; }
public string subtitle { get; set; }
public string formattedText { get; set; }
public Image image { get; set; }
public Button[] buttons { get; set; }
}
public class Image
{
public string imageUri { get; set; }
public string accessibilityText { get; set; }
}
public class Button
{
public string title { get; set; }
public Openuriaction openUriAction { get; set; }
}
public class Openuriaction
{
public string uri { get; set; }
}
预期结果:
{
"fulfillmentMessages": [
{
"platform": "ACTIONS_ON_GOOGLE",
"simpleResponses": {
"simpleResponses": [
{
"textToSpeech": "Did you meant?"
}
]
}
},
{
"text": {
"text": [
"Did you meant?"
]
}
},
{
"platform": "ACTIONS_ON_GOOGLE",
"basicCard": {
"title": "Title",
"subtitle": "Subtitle",
"formattedText": "A text to enter here is nn hiusn uboub bubub mio",
"image": {
"imageUri": "https://www.eg.com",
"accessibilityText": "Accessibility Text"
},
"buttons": [
{
"title": "Weblink Title",
"openUriAction": {
"uri": "https://www.eg.com"
}
}
]
}
}
]
}
实际结果:
{
"fulfillmentMessages": [
{
"platform": "ACTIONS_ON_GOOGLE",
"simpleResponses": {
"simpleResponses": [
{
"textToSpeech": "Test fulfillment Text"
}
]
},
"text": {
"text": [
"Test fulfillment Text"
]
},
"basicCard": {
"title": "Test Title",
"subtitle": "Test subtitle",
"formattedText": "Test Discription",
"image": {
"imageUri": "https://www.eg.com",
"accessibilityText": "Test"
},
"buttons": [
{
"title": "Test weblink",
"openUriAction": {
"uri":"https://www.eg.com"
}
}
]
}
}
]
}
我假设你有一个 parent/root class 声明这样的东西 -
public class Root
{
List<Fulfillmentmessage> fulfillmentMessages {get; set;}
}
并创建这样的对象 -
var root = new Root();
root.fulfillmentMessages = new List<Fulfillmentmessage>();
root.fulfillmentMessages.Add(new Fulfillmentmessage()); // <- Simple response
root.fulfillmentMessages.Add(new Fulfillmentmessage()); // <- Basic Card
但是,根据您的预期 json
,您的 class 结构应该是 -
对于Fulfillmentmessage
class -
public class Fulfillmentmessage
{
public string platform { get; set; }
public Simpleresponses simpleResponses { get; set; }
//public Text text { get; set; } <- No Text property here
public string platform2 { get; set; }
public Basiccard basicCard { get; set; }
}
对于root
class -
public class Root
{
List<object> fulfillmentMessages {get; set;} // Replaced Fulfillmentmessage with object
}
并像这样创建对象 -
var root = new Root();
root.fulfillmentMessages = new List<Fulfillmentmessage>();
root.fulfillmentMessages.Add(new Fulfillmentmessage()); // <- Simple response
root.fulfillmentMessages.Add(new Text()); // <- Text
root.fulfillmentMessages.Add(new Fulfillmentmessage()); // <- Basic Card
我使用 Newtonsoft.Json 库进行反序列化和序列化。我正在尝试将 C# class 序列化为 json,我已经将许多 classes 序列化为 json 但我从未遇到过这样的 json文件。问题是第一个 "text" 属性后面没有跟一个开括号,而且 "platform" 属性在 fulfillmentMessage 中重复(已经存在)。
我的Class:
public class Fulfillmentmessage
{
public string platform { get; set; }
[JsonProperty("simpleResponses ", NullValueHandling = NullValueHandling.Ignore)]
public Simpleresponses simpleResponses { get; set; }
[JsonProperty("basicCard", NullValueHandling = NullValueHandling.Ignore)]
public Basiccard basicCard { get; set; }
}
public class Simpleresponses
{
public Simplerespons[] simpleResponses { get; set; }
}
public class Simplerespons
{
public string textToSpeech { get; set; }
}
public class Texts
{
public Text text { get; set; }
}
public class Text
{
public List<string> text { get; set; }
}
public class Basiccard
{
public string title { get; set; }
public string subtitle { get; set; }
public string formattedText { get; set; }
public Image image { get; set; }
public Button[] buttons { get; set; }
}
public class Image
{
public string imageUri { get; set; }
public string accessibilityText { get; set; }
}
public class Button
{
public string title { get; set; }
public Openuriaction openUriAction { get; set; }
}
public class Openuriaction
{
public string uri { get; set; }
}
预期结果:
{
"fulfillmentMessages": [
{
"platform": "ACTIONS_ON_GOOGLE",
"simpleResponses": {
"simpleResponses": [
{
"textToSpeech": "Did you meant?"
}
]
}
},
{
"text": {
"text": [
"Did you meant?"
]
}
},
{
"platform": "ACTIONS_ON_GOOGLE",
"basicCard": {
"title": "Title",
"subtitle": "Subtitle",
"formattedText": "A text to enter here is nn hiusn uboub bubub mio",
"image": {
"imageUri": "https://www.eg.com",
"accessibilityText": "Accessibility Text"
},
"buttons": [
{
"title": "Weblink Title",
"openUriAction": {
"uri": "https://www.eg.com"
}
}
]
}
}
]
}
实际结果:
{
"fulfillmentMessages": [
{
"platform": "ACTIONS_ON_GOOGLE",
"simpleResponses": {
"simpleResponses": [
{
"textToSpeech": "Test fulfillment Text"
}
]
},
"text": {
"text": [
"Test fulfillment Text"
]
},
"basicCard": {
"title": "Test Title",
"subtitle": "Test subtitle",
"formattedText": "Test Discription",
"image": {
"imageUri": "https://www.eg.com",
"accessibilityText": "Test"
},
"buttons": [
{
"title": "Test weblink",
"openUriAction": {
"uri":"https://www.eg.com"
}
}
]
}
}
]
}
我假设你有一个 parent/root class 声明这样的东西 -
public class Root
{
List<Fulfillmentmessage> fulfillmentMessages {get; set;}
}
并创建这样的对象 -
var root = new Root();
root.fulfillmentMessages = new List<Fulfillmentmessage>();
root.fulfillmentMessages.Add(new Fulfillmentmessage()); // <- Simple response
root.fulfillmentMessages.Add(new Fulfillmentmessage()); // <- Basic Card
但是,根据您的预期 json
,您的 class 结构应该是 -
对于Fulfillmentmessage
class -
public class Fulfillmentmessage
{
public string platform { get; set; }
public Simpleresponses simpleResponses { get; set; }
//public Text text { get; set; } <- No Text property here
public string platform2 { get; set; }
public Basiccard basicCard { get; set; }
}
对于root
class -
public class Root
{
List<object> fulfillmentMessages {get; set;} // Replaced Fulfillmentmessage with object
}
并像这样创建对象 -
var root = new Root();
root.fulfillmentMessages = new List<Fulfillmentmessage>();
root.fulfillmentMessages.Add(new Fulfillmentmessage()); // <- Simple response
root.fulfillmentMessages.Add(new Text()); // <- Text
root.fulfillmentMessages.Add(new Fulfillmentmessage()); // <- Basic Card