我试图制作下面的Json
{
"retailer": "The retailer",
"sites": [{
"id": "1234",
"Sitename": "Microsoft",
"website": "www.microsoft.com"
}],
"Products": [{
"Name": "Visual Studio",
"Year": "2017"
}]
}
网站(数组,但我使用过List)。 产品(阵列,但我使用过List)
我的代码(不产生上述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(来自数据库的数据,但是现在我已经对这些值进行了硬编码)并将其发布到第三方服务但是我不能让Json采用我的格式要求?我怎么能实现这个目标?
答案 0 :(得分:2)
对于像这样的东西,我喜欢将json转换为类的网站,然后你就知道你正在使用什么。
对于你的例子,有点谷歌和我得到这些课程。
import boto3
import datetime
from dateutil.tz import tzlocal
idp = boto3.client("cognito-idp")
identity = boto3.client("cognito-identity")
# login into user pool
response = idp.admin_initiate_auth(AuthFlow='ADMIN_NO_SRP_AUTH', UserPoolId='us-east-1_4xnGZyQWy', AuthParameters={'USERNAME':'admin', 'PASSWORD':'Ka12345'}, ClientId='25pafveaaladb2qalfh3i7s4h9')
logins = {
"cognito-idp.us-east-1.amazonaws.com/us-east-1_4xkGZxQWy":response["AuthenticationResult"]["IdToken"]
}
# get identity_id
get_id_response = identity.get_id(IdentityPoolId="us-east-1:bfd63186-8e01-462a-8a5b-db58d7d4af2a", Logins=logins)
identity_id = get_id_response["IdentityId"]
# get credentials
credentials = identity.get_credentials_for_identity(IdentityId=identity_id, Logins=logins)
print(credentials)
credentials["Credentials"]["Expiration"] = str(credentials["Credentials"]["Expiration"])
# get open id token
oidToken = identity.get_open_id_token(IdentityId=identity_id, Logins=logins)
output = dict()
output["identityId"] = identity_id
output["token"] = oidToken["Token"]
output["credentials"] = credentials["Credentials"]
print(output)
您现在应该能够在RootObject中设置数据并正确序列化。
答案 1 :(得分:1)
创建像这样的类
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);
然后在Newtonsoft
的帮助下序列化您的对象string ser = JsonConvert.SerializeObject(par);
Console.WriteLine(ser);
这就是ser
看起来