我需要像这样创建 JSON:
{
"files": [
{
"file_path": "example.txt",
"content" : "source code \n with multiple lines\n"
}
]
}
但是我的代码(后来序列化成JSON)和上面这个例子不符
var requestBody = new
{
files = new string[] { snippet.FileName, snippet.Content }
};
有人可以帮我 :) 吗?
编辑:
我的序列化方法:
protected string serializeToJson( object obj )
{
return JsonConvert.SerializeObject( obj, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() } );
}
最佳答案
试试看:
using System.Text.Json;
var obj = new
{
files = new[]
{
new
{
file_path = "example.txt",
content ="source code \n with multiple lines\n"
}
}
};
var json = JsonSerializer.Serialize(obj);
Console.WriteLine(json);
结果:
https://stackoverflow.com/questions/70632236/