【发布时间】:2015-08-23 11:19:33
【问题描述】:
我正在创建 Json 文件,在创建之前我想检查是否有任何属性为空。我想为此创建抽象方法。所以我不必一次又一次地写。
public JObject CreatUserJson(Account lacc)
{
JObject pin = new JObject(
new JProperty("email", lacc.email),
new JProperty("fullname", lacc.fullname),
new JProperty("phonenumber", lacc.phonenumber),
new JProperty("ip_address", lacc.ip_address),
new JProperty("password", lacc.password),
new JProperty("client_id", Settings.Globals.CLIENT_ID),
new JProperty("client_secret", Settings.Globals.CLIENT_SECRET)
);
return pin;
}
这就是我定义我的方法的方式,并且有类似的方法,我想要标准的方法来检查并在缺少任何值时抛出异常..
public JObject IncomingWireNoticeJson(SyanpasePayLib.Resources.Wire lWire)
{
JObject pin = new JObject(
new JProperty("amount", lWire.amount),
new JProperty("status_url", lWire.status_url),
new JProperty("memo", lWire.memo),
new JProperty("oauth_consumer_key", lWire.oauth_consumer_key)
);
return pin;
}
这是方法的另一个例子,没有相似之处。如果缺少任何值,我只想循环并抛出异常。
例如,我知道CreatUserJson 我需要最少 4 个输入和最多 8 个输入..
IncomingWireNoticeJson 的方法相同,我需要最少 2 个输入和最多 4 个输入..
如果范围大于或小于最小值和最大值,那么它应该抛出错误..(这部分我可以管理,但我不知道如何定义循环遍历该对象的标准方式)
有人可以帮我解决这个问题吗?
【问题讨论】:
-
@Tom 但我想要最小值和最大值,我不想遍历类 account= 的所有属性,我只想遍历我在方法中定义的属性。
-
听起来你需要Json Schema。
标签: c# asp.net .net json properties