C# JSON为空,在MVC中使用JsonResult类型
我正在尝试输出一个JSON文件。我正在使用netcoreapp3.1和Newtonsoft.Json NuGet。这是我输入所有信息的代码:C# JSON为空,在MVC中使用JsonResult类型,c#,json,model-view-controller,slack,C#,Json,Model View Controller,Slack,我正在尝试输出一个JSON文件。我正在使用netcoreapp3.1和Newtonsoft.Json NuGet。这是我输入所有信息的代码: private BlockWrapper GetBlock() { var blockWrapper = new BlockWrapper { blocks = new List<IntBlock> { new Sec
private BlockWrapper GetBlock()
{
var blockWrapper = new BlockWrapper
{
blocks = new List<IntBlock>
{
new SectionBlockBlock
{
text = new TextBlock
{
type = "mrkdwn",
text = "Hello! I am Multiuse Kat. How can I help you ?"
}
},
new ActionsBlockBlock
{
elements = new ElementBlock[]
{
new ElementBlock
{
type = "button",
text = new TextBlock
{
type = "plain_text",
text = "Help"
},
style = "primary",
value = "click_me_123"
}
}
},
}
};
return blockWrapper;
}
private BlockWrapper GetBlock()
{
var blockWrapper=新的blockWrapper
{
块=新列表
{
新分段块
{
text=新文本块
{
type=“mrkdwn”,
text=“你好!我是多用途Kat。有什么可以帮你的吗?”
}
},
新动作锁定块
{
元素=新元素块[]
{
新元素块
{
type=“button”,
text=新文本块
{
type=“纯文本”,
text=“帮助”
},
style=“primary”,
value=“单击“我”123”
}
}
},
}
};
返回区块包装器;
}
我的模特看起来像这样。请注意,我正在使用IntBlock的接口:
public class BlockWrapper
{
public List<IntBlock> blocks { get; set; }
}
public class SectionBlockBlock : IntBlock
{
public string type { get; } = "section";
public string blockId { get; set; }
public TextBlock text { get; set; }
}
public class ActionsBlockBlock : IntBlock
{
public string type { get; } = "actions";
public string blockId { get; set; }
public ElementBlock[] elements { get; set; }
}
public class TextBlock
{
public string type { get; set; }
public string text { get; set; }
public bool? emoji { get; set; }
}
public class ElementBlock
{
public string type { get; set; }
public string action_id { get; set; }
public TextBlock text { get; set; }
public string value { get; set; }
public string style { get; set; }
}
public interface IntBlock { }
公共类区块包装器
{
公共列表块{get;set;}
}
公共类SectionBlock:IntBlock
{
公共字符串类型{get;}=“section”;
公共字符串块ID{get;set;}
公共文本块文本{get;set;}
}
公共类操作BlockBlock:IntBlock
{
公共字符串类型{get;}=“actions”;
公共字符串块ID{get;set;}
public ElementBlock[]元素{get;set;}
}
公共类文本块
{
公共字符串类型{get;set;}
公共字符串文本{get;set;}
公共bool?emoji{get;set;}
}
公共类元素块
{
公共字符串类型{get;set;}
公共字符串操作\u id{get;set;}
公共文本块文本{get;set;}
公共字符串值{get;set;}
公共字符串样式{get;set;}
}
公共接口IntBlock{}
当我返回新的JsonResult(GetBlock())时;我输出这个:
我假设这与IntBlock的接口有关。有人能告诉我怎么解决这个问题吗?谢谢 试试这个
return JsonConvert.SerializeObject(GetBlock());
我总是使用上述方法,所有的工作都很好
而且,您的接口
属性是null
public interface IntBlock { }
所以,您的集合类型是空接口,序列化程序将无法找到任何属性来序列化它
我认为下面的代码应该可以工作
public class ActionsBlockBlock : IntBlock
{
IntBlock.type { get; } = "actions";
IntBlock.blockId { get; set; }
IntBlock.ElementBlock[] elements { get; set; }
}
public interface IntBlock
{
public string type { get; set; }
public string blockId { get; set; }
public ElementBlock[] elements { get; set; }
}
请为所有类执行上述模式,然后重试。我认为它是有效的。您使用的是什么MVC框架和版本?是还是?您几乎已经提供了一个框架,有关框架的详细信息将使您的问题得到解答。我们基本上需要能够弄清楚您使用的是什么JSON序列化程序。谢谢。我已经编辑了以下详细信息:我正在使用netcoreapp3.1以及Newtonsoft.Json NuGet以及Newtonsoft.Json NuGet-OK,但是您是否也添加了
Microsoft.AspNetCore.Mvc.NewtonsoftJson
NuGet并调用.AddNewtonsoftJson()代码>在ConfigureServices()
中?如果没有,您将使用System.Text.Json
,我相信它将根据属性值声明的类型而不是实际类型序列化属性值。看到了吗?真管用!谢谢。返回JsonConvert.SerializeObject(GetBlock())代码>将导致双重序列化JSON。请参阅:.@abc它不会将其双重序列化为响应类型json格式,如果不这样做,则必须使用流格式。