以正确的格式生成 Json
Producing Json in correct format
我正在尝试制作以下内容 Json
{
"retailer": "The retailer",
"sites": [{
"id": "1234",
"Sitename": "Microsoft",
"website": "www.microsoft.com"
}],
"Products": [{
"Name": "Visual Studio",
"Year": "2017"
}]
}
sites(数组,但我使用了列表)。
产品(数组,但我使用了列表)
我的代码(不产生上述 Json)
Parent par = new Parent();
List<Sites> parList = new List<Sites>();
Sites site = new Sites();
Software sw = new Software();
List<Software> swList = new List<Software>();
par.retailer = "The retailer";
site.Id = "1234";
site.Sitename = "Microsoft";
site.Website = "www.microsoft.com";
par.sites = parList;
sw.Name = "Visual Studio";
sw.Year = DateTime.Year;
swList.Add(sw);
site.Products = swList;
parList.Add(site);
MemoryStream stream1 = new MemoryStream();
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Parent));
ser.WriteObject(stream1, par);
stream1.Position = 0;
StreamReader sr = new StreamReader(stream1);
Console.WriteLine(op);
我需要做的是生成 Json(数据来自数据库,但现在我已经对值进行了硬编码)和 post 它到第三方服务,但我无法让 Json 成为我需要的格式?我怎样才能做到这一点?
对于这样的东西,我喜欢将 json 转换为 类 的网站,这样您就知道自己在做什么。
以你的例子为例,有点 google-fu 我得到了这些 类。
public class Site
{
public string id { get; set; }
public string Sitename { get; set; }
public string website { get; set; }
}
public class Product
{
public string Name { get; set; }
public string Year { get; set; }
}
public class RootObject
{
public string retailer { get; set; }
public List<Site> sites { get; set; }
public List<Product> Products { get; set; }
}
您现在应该能够在 RootObject 中设置数据并正确序列化它。
像这样创建类
public class Site
{
public string id { get; set; }
public string Sitename { get; set; }
public string website { get; set; }
}
public class Product
{
public string Name { get; set; }
public string Year { get; set; }
}
public class Example
{
public string retailer { get; set; }
public List<Site> sites { get; set; }
public List<Product> Products { get; set; }
}
然后像这样将所有数据放入对象中
Example par = new Example();
List<Site> parList = new List<Site>();
Site site = new Site();
Product sw = new Product();
List<Product> swList = new List<Product>();
par.retailer = "The retailer";
site.id = "1234";
site.Sitename = "Microsoft";
site.website = "www.microsoft.com";
par.sites = parList;
sw.Name = "Visual Studio";
sw.Year = DateTime.Now.Year.ToString();
swList.Add(sw);
par.Products = swList;
parList.Add(site);
然后在
的帮助下序列化您的对象
string ser = JsonConvert.SerializeObject(par);
Console.WriteLine(ser);
这就是 ser
的样子
我正在尝试制作以下内容 Json
{
"retailer": "The retailer",
"sites": [{
"id": "1234",
"Sitename": "Microsoft",
"website": "www.microsoft.com"
}],
"Products": [{
"Name": "Visual Studio",
"Year": "2017"
}]
}
sites(数组,但我使用了列表)。 产品(数组,但我使用了列表)
我的代码(不产生上述 Json)
Parent par = new Parent();
List<Sites> parList = new List<Sites>();
Sites site = new Sites();
Software sw = new Software();
List<Software> swList = new List<Software>();
par.retailer = "The retailer";
site.Id = "1234";
site.Sitename = "Microsoft";
site.Website = "www.microsoft.com";
par.sites = parList;
sw.Name = "Visual Studio";
sw.Year = DateTime.Year;
swList.Add(sw);
site.Products = swList;
parList.Add(site);
MemoryStream stream1 = new MemoryStream();
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Parent));
ser.WriteObject(stream1, par);
stream1.Position = 0;
StreamReader sr = new StreamReader(stream1);
Console.WriteLine(op);
我需要做的是生成 Json(数据来自数据库,但现在我已经对值进行了硬编码)和 post 它到第三方服务,但我无法让 Json 成为我需要的格式?我怎样才能做到这一点?
对于这样的东西,我喜欢将 json 转换为 类 的网站,这样您就知道自己在做什么。
以你的例子为例,有点 google-fu 我得到了这些 类。
public class Site
{
public string id { get; set; }
public string Sitename { get; set; }
public string website { get; set; }
}
public class Product
{
public string Name { get; set; }
public string Year { get; set; }
}
public class RootObject
{
public string retailer { get; set; }
public List<Site> sites { get; set; }
public List<Product> Products { get; set; }
}
您现在应该能够在 RootObject 中设置数据并正确序列化它。
像这样创建类
public class Site
{
public string id { get; set; }
public string Sitename { get; set; }
public string website { get; set; }
}
public class Product
{
public string Name { get; set; }
public string Year { get; set; }
}
public class Example
{
public string retailer { get; set; }
public List<Site> sites { get; set; }
public List<Product> Products { get; set; }
}
然后像这样将所有数据放入对象中
Example par = new Example();
List<Site> parList = new List<Site>();
Site site = new Site();
Product sw = new Product();
List<Product> swList = new List<Product>();
par.retailer = "The retailer";
site.id = "1234";
site.Sitename = "Microsoft";
site.website = "www.microsoft.com";
par.sites = parList;
sw.Name = "Visual Studio";
sw.Year = DateTime.Now.Year.ToString();
swList.Add(sw);
par.Products = swList;
parList.Add(site);
然后在
string ser = JsonConvert.SerializeObject(par);
Console.WriteLine(ser);
这就是 ser
的样子