C# c语言中带数组的Json#
我开始学习C#和一些Json,我试图获得这种形式的Json格式: 我试过这个:C# c语言中带数组的Json#,c#,json,C#,Json,我开始学习C#和一些Json,我试图获得这种形式的Json格式: 我试过这个: static void Main(string[] args) { var myjason = new myJson { ContentDisposition = "", md5 = "da855ff838250f45d528a5a05692f14e", f
static void Main(string[] args)
{
var myjason = new myJson
{
ContentDisposition = "",
md5 = "da855ff838250f45d528a5a05692f14e",
file_name = "MyFile.docx",
features = new[] { "te" },
te = new te { reports = new[] { "pdf", "xml" } },
// images = new img { { a.id = "7e6fe36e-889e-4c25-8704-56378f0830df", a.revision = 1 }, { a.id = "e50e99f3-5963-4573-af9e-e3f4750b55e2", a.revision = 1 } }
};
string json = JsonConvert.SerializeObject(myjason, Formatting.Indented);
Console.WriteLine(json);
}
public class myJson
{
public string ContentDisposition{ get; set; }
public string md5 { get; set; }
public string file_name { get; set; }
public string[] features { get; set; }
public te te { get; set; }
public img images { get; set; }
}
public class a
{
public string id { get; set; }
public int revision { get; set; }
}
public class te
{
public string[] reports { get; set; }
}
public class img
{
public a[] images { get; set; }
}
这是我当前的输出:
请帮忙,非常感谢 我想你对这里发生的事情有点困惑。看起来您正试图将一些JSON发布到某个端点
内容配置
和内容类型
是HTTP头。它们不是JSON
JSON以第一个{
开头,这是文章的主体。要创建主体,可以使用C#对象,如:
public class MyJson {
public class MyRequest request {get ;set;}
}
public class MyRequest {
public string md5 {get;set;}
public string file_name {get;set;}
public string file_type {get;set;}
public List<string> features {get;set;}
public MyTe te {get;set;}
}
public class MyTe {
public List<string> reports {get;set;}
public List<MyImages> images {get;set;}
}
public class MyImages {
public string id {get;set;}
public int revision {get;set;}
}
公共类MyJson{
公共类MyRequest请求{get;set;}
}
公共类MyRequest{
公共字符串md5{get;set;}
公共字符串文件名{get;set;}
公共字符串文件\u类型{get;set;}
公共列表功能{get;set;}
公共MyTe{get;set;}
}
公共类MyTe{
公共列表报告{get;set;}
公共列表图像{get;set;}
}
公共类MyImages{
公共字符串id{get;set;}
公共int修订版{get;set;}
}
然后在MyJson
对象上使用jsonvert.SerializeObject
。设置HTTP头取决于您尝试执行的操作以及使用的工具,这可能属于另一个问题
编辑:我说“等等”,因为这实际上只是一个死记硬背的练习,还有一些,但我已经更新了。内容配置和内容类型是请求头,所以它们不需要在json正文中 这里我还演示了如何使用JsonProperty属性设置自定义json属性名
static void Main(string[] args)
{
var myjason = new myJsonClass
{
Request = new requestClass
{
md5 = "da855ff838250f45d528a5a05692f14e",
file_name = "MyFile.docx",
file_type = "docx",
features = new[] { "te" },
te = new te
{
reports = new[] { "pdf", "xml" },
images = new a[] { new a { id = "7e6fe36e-889e-4c25-8704-56378f0830df", revision = 1 }, new a { id = "e50e99f3-5963-4573-af9e-e3f4750b55e2", revision = 1 } }
},
}
};
string json = JsonConvert.SerializeObject(myjason, Newtonsoft.Json.Formatting.Indented);
Console.WriteLine(json);
}
public class myJsonClass
{
[JsonProperty("request")]
public requestClass Request { get; set; }
}
public class requestClass
{
public string md5 { get; set; }
public string file_name { get; set; }
public string file_type { get; set; }
public string[] features { get; set; }
public te te { get; set; }
}
public class a
{
public string id { get; set; }
public int revision { get; set; }
}
public class te
{
public string[] reports { get; set; }
public a[] images { get; set; }
}
输出:
{
"request": {
"md5": "da855ff838250f45d528a5a05692f14e",
"file_name": "MyFile.docx",
"file_type": "docx",
"features": [
"te"
],
"te": {
"reports": [
"pdf",
"xml"
],
"images": [
{
"id": "7e6fe36e-889e-4c25-8704-56378f0830df",
"revision": 1
},
{
"id": "e50e99f3-5963-4573-af9e-e3f4750b55e2",
"revision": 1
}
]
}
}
}
你能添加一个完整的代码吗?你说“等等”是什么意思?这正是我得到Stuck的原因。至少15个代表承诺会得到完整的回答。提前感谢15个代表,这将对我的生活产生巨大的影响。请包括代码,而不是代码图片,特别是如果你要求其他人向你提供代码。