C# 如何在Json.Net对象中添加基于条件语句的JProperty
我想根据条件语句的结果在C# 如何在Json.Net对象中添加基于条件语句的JProperty,c#,json.net,C#,Json.net,我想根据条件语句的结果在JObject中添加JProperty字段,但在格式化代码时遇到问题 string zip = "00000"; bool isNull = string.IsNullOrEmpty(str); JObject jsonContent = new JObject( new JProperty("email_address", subscriber.Email), new JProperty("status", "subsc
JObject
中添加JProperty
字段,但在格式化代码时遇到问题
string zip = "00000";
bool isNull = string.IsNullOrEmpty(str);
JObject jsonContent = new JObject(
new JProperty("email_address", subscriber.Email),
new JProperty("status", "subscribed"),
if(!isNull)
{
new JProperty("ZIP", str),
}
new JProperty("state": "NY")
);
问题是如何处理前一行上的逗号,以及如何特别格式化JSON对象中的条件语句。您可以稍后根据您的条件添加属性,以下内容如何
string zip = "00000";
bool isNull = string.IsNullOrEmpty(str);
JObject jsonContent = new JObject(
new JProperty("email_address", subscriber.Email),
new JProperty("status", "subscribed"),
new JProperty("state": "NY")
);
if(isNull) {
jsonContent["ZIP"] = str;
}