我正在尝试在我的项目中构建一个json,但让它为每个变量重新创建它自己。 请有人告诉我如何才能让所有人进入同一组?
类:
public class GetClient_Secrets
{
public List<Client_Secret> web {get; set;}
}
public class Client_Secret
{
public string auth_uri {get; set;}
public string client_secret {get; set;}
public string token_uri {get; set;}
public string client_email {get; set;}
public string redirect_uris {get; set;}
public string client_x509_cert_url {get; set;}
public string client_id {get; set;}
public string auth_provider_x509_cert_url {get; set;}
public string javascript_origins {get; set;}
}
我如何建立我的Json:
var jsonResult = new GetClient_Secrets
{
web = new List<Client_Secret>
{
new Client_Secret {auth_uri = "https://accounts.google.com/o/oauth2/auth1"},
new Client_Secret {client_secret = "jCTUEYcUUKFnbFqLuPmnC-ZO2"},
new Client_Secret {token_uri = "https://accounts.google.com/o/oauth2/token3"},
new Client_Secret {client_email = "789526175471-mh3o48ooi8va0mr0lke62pvqnf3kd2fj@developer.gserviceaccount.com"},
new Client_Secret {redirect_uris = "http://www.hlhstp.com/oauth2callback5"},
new Client_Secret {client_x509_cert_url = "https://www.googleapis.com/robot/v1/metadata/x509/789526175471-mh3o48ooi8va0mr0lke62pvqnf3kd2fj@developer.gserviceaccount.com"},
new Client_Secret {client_id = "789526175471-mh3o48ooi8va0mr0lke62pvqnf3kd2fj.apps.googleusercontent.com"},
new Client_Secret {auth_provider_x509_cert_url = "https://www.googleapis.com/oauth2/v1/certs6"},
new Client_Secret {javascript_origins = "http://www.hlhstp.com"}
}
};
return JsonConvert.SerializeObject(jsonResult);//.Dump();
结果:
{
"web": [
{
"auth_uri": "https://accounts.google.com/o/oauth2/auth1",
"client_secret": null,
"token_uri": null,
"client_email": null,
"redirect_uris": null,
"client_x509_cert_url": null,
"client_id": null,
"auth_provider_x509_cert_url": null,
"javascript_origins": null
},
{
"auth_uri": null,
"client_secret": "jCTUEYcUUKFnbFqLuPmnC-ZO2",
"token_uri": null,
"client_email": null,
"redirect_uris": null,
"client_x509_cert_url": null,
"client_id": null,
"auth_provider_x509_cert_url": null,
"javascript_origins": null
},
{
"auth_uri": null,
"client_secret": null,
"token_uri": "https://accounts.google.com/o/oauth2/token3",
"client_email": null,
"redirect_uris": null,
"client_x509_cert_url": null,
"client_id": null,
"auth_provider_x509_cert_url": null,
"javascript_origins": null
},
{
"auth_uri": null,
"client_secret": null,
"token_uri": null,
"client_email": "789526175471-mh3o48ooi8va0mr0lke62pvqnf3kd2fj@developer.gserviceaccount.com",
"redirect_uris": null,
"client_x509_cert_url": null,
"client_id": null,
"auth_provider_x509_cert_url": null,
"javascript_origins": null
},
{
"auth_uri": null,
"client_secret": null,
"token_uri": null,
"client_email": null,
"redirect_uris": "http://www.hlhstp.com/oauth2callback5",
"client_x509_cert_url": null,
"client_id": null,
"auth_provider_x509_cert_url": null,
"javascript_origins": null
},
{
"auth_uri": null,
"client_secret": null,
"token_uri": null,
"client_email": null,
"redirect_uris": null,
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/789526175471-mh3o48ooi8va0mr0lke62pvqnf3kd2fj@developer.gserviceaccount.com",
"client_id": null,
"auth_provider_x509_cert_url": null,
"javascript_origins": null
},
{
"auth_uri": null,
"client_secret": null,
"token_uri": null,
"client_email": null,
"redirect_uris": null,
"client_x509_cert_url": null,
"client_id": "789526175471-mh3o48ooi8va0mr0lke62pvqnf3kd2fj.apps.googleusercontent.com",
"auth_provider_x509_cert_url": null,
"javascript_origins": null
},
{
"auth_uri": null,
"client_secret": null,
"token_uri": null,
"client_email": null,
"redirect_uris": null,
"client_x509_cert_url": null,
"client_id": null,
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs6",
"javascript_origins": null
},
{
"auth_uri": null,
"client_secret": null,
"token_uri": null,
"client_email": null,
"redirect_uris": null,
"client_x509_cert_url": null,
"client_id": null,
"auth_provider_x509_cert_url": null,
"javascript_origins": "http://www.hlhstp.com"
}
]
}
答案 0 :(得分:1)
每次传递一个不同的初始化参数时,您都会反复调用构造函数。调用构造函数一次,传入所有初始化参数。
var jsonResult = new GetClient_Secrets
{
web = new List<Client_Secret>
{
new Client_Secret {
auth_uri = "https://accounts.google.com/o/oauth2/auth1"},
client_secret = "jCTUEYcUUKFnbFqLuPmnC-ZO2",
token_uri = "https://accounts.google.com/o/oauth2/token3",
client_email = "789526175471-mh3o48ooi8va0mr0lke62pvqnf3kd2fj@developer.gserviceaccount.com",
redirect_uris = "http://www.hlhstp.com/oauth2callback5",
client_x509_cert_url = "https://www.googleapis.com/robot/v1/metadata/x509/789526175471-mh3o48ooi8va0mr0lke62pvqnf3kd2fj@developer.gserviceaccount.com",
client_id = "789526175471-mh3o48ooi8va0mr0lke62pvqnf3kd2fj.apps.googleusercontent.com",
auth_provider_x509_cert_url = "https://www.googleapis.com/oauth2/v1/certs6",
javascript_origins = "http://www.hlhstp.com"
}
}
};
return JsonConvert.SerializeObject(jsonResult);//.Dump();
即使它只有一个条目,它仍然会产生一个列表。如果你只需要一个条目,也可以删除列表,但我认为你的GetClient_Secrets
对象的初始化程序需要它。
答案 1 :(得分:1)
您遇到的问题是您要为每个属性创建一个新的Client_Secret
,您要做的只是创建一个并设置所有属性:
web = new List<Client_Secret>
{
new Client_Secret
{
auth_uri = "https://accounts.google.com/o/oauth2/auth1",
client_secret = "jCTUEYcUUKFnbFqLuPmnC-ZO2",
token_uri = "https://accounts.google.com/o/oauth2/token3",
client_email = "789526175471-mh3o48ooi8va0mr0lke62pvqnf3kd2fj@developer.gserviceaccount.com",
redirect_uris = "http://www.hlhstp.com/oauth2callback5",
client_x509_cert_url = "https://www.googleapis.com/robot/v1/metadata/x509/789526175471-mh3o48ooi8va0mr0lke62pvqnf3kd2fj@developer.gserviceaccount.com",
client_id = "789526175471-mh3o48ooi8va0mr0lke62pvqnf3kd2fj.apps.googleusercontent.com",
auth_provider_x509_cert_url = "https://www.googleapis.com/oauth2/v1/certs6",
javascript_origins = "http://www.hlhstp.com"
}
}
答案 2 :(得分:1)
您正在为每个字段创建一个对象。您应该初始化一个对象中的所有字段。
var jsonResult = new GetClient_Secrets
{
web = new List<Client_Secret>
{
new Client_Secret {
auth_uri = "https://accounts.google.com/o/oauth2/auth1",
client_secret = "jCTUEYcUUKFnbFqLuPmnC-ZO2",
token_uri = "https://accounts.google.com/o/oauth2/token3",
client_email = "789526175471-mh3o48ooi8va0mr0lke62pvqnf3kd2fj@developer.gserviceaccount.com",
redirect_uris = "http://www.hlhstp.com/oauth2callback5",
client_x509_cert_url = "https://www.googleapis.com/robot/v1/metadata/x509/789526175471-mh3o48ooi8va0mr0lke62pvqnf3kd2fj@developer.gserviceaccount.com",
client_id = "789526175471-mh3o48ooi8va0mr0lke62pvqnf3kd2fj.apps.googleusercontent.com",
auth_provider_x509_cert_url = "https://www.googleapis.com/oauth2/v1/certs6",
javascript_origins = "http://www.hlhstp.com"
}
}
};
return JsonConvert.SerializeObject(jsonResult);//.Dump();