Newtonsoft 中的 JSON 空处理问题
Issue with JSON null handling in Newtonsoft
我在交易 Newtonsoft.json
时遇到 null
处理问题。
我想检查结果是否 null
。基于此,我想处理一些情况。
我的代码如下:
try {
var response = GetApiData.Post(_getApiBaseUrl, data.ToString());
var jsonString = response.ResultString;
var jsonContent = JObject.Parse(jsonString);
if (jsonContent["User"] != null) // <-- null handling
{
var user = JToken.Parse(jsonContent["User"].ToString());
membershipUser = GetMembershipUser(user);
}
}
jsonContent
和null
如下:
{
"User": null
}
如果 "User": null
jsonContent["User"]
returns {}
和 jsonContent["User"] != null
条件没有抛出任何错误,但它不会跳过块,而是执行内线。
所以对于 null
处理,我使用了这个代码:
if (jsonContent["User"].Value<string>() != null)
如果"User": null
,上面的代码工作正常。
但是如果jsonContent["User"]
有有效数据,它会抛出一个错误。
Cannot cast Newtonsoft.Json.Linq.JObject to Newtonsoft.Json.Linq.JToken
有效数据jsonContent
如下:
{
"User": {
"Claims": [],
"Logins": [],
"Roles": [],
"FirstName": "Unknown",
"LastName": "Unknown",
"IsApproved": true,
"IsDeleted": false,
"Email": "testuser@user.com",
"EmailConfirmed": false,
"PasswordHash": "AC/qXxxxxxxxxx==",
"SecurityStamp": "001f3500-0000-0000-0000-00f92b524700",
"PhoneNumber": null,
"PhoneNumberConfirmed": false,
"TwoFactorEnabled": false,
"LockoutEndDateUtc": null,
"LockoutEnabled": false,
"AccessFailedCount": 0,
"Id": "00f50a00-0000-0000-0000-000b97bf2800",
"UserName": "testUser"
}
}
如何使用有效数据和空值实现这种空值处理?
您可以检查 JToken.Type
being JTokenType.Null
:
var jsonContent = JObject.Parse(jsonString);
var user = jsonContent["User"];
if (user != null && user.Type != JTokenType.Null)
{
membershipUser = GetMembershipUser(user);
}
为了方便检查,可以引入扩展方法:
public static partial class JTokenExtensions
{
public static bool IsNull(this JToken token)
{
return token == null || token.Type == JTokenType.Null;
}
}
然后做:
if (!user.IsNull())
{
membershipUser = GetMembershipUser(user);
}
我添加以扩展上面的 dbc 的答案,因为我发现 JToken 被字符串变量分配给 null 的情况。然后 JTokenType 为 JTokneType.String,但该值仍然为 null。
using Newtonsoft.Json.Linq;
namespace Common
{
public static class JTokenExtensions
{
public static bool IsNull(this JToken token)
{
if (token == null || token.Type == JTokenType.Null)
{
return true;
}
// Catch the case where a user uses a NULL string variable to set the JToken to null
// If it was directly assigned to a null, the test above will work; however, if you
// set the JToken with a string variable that is null, you need this test!
if (token.Type == JTokenType.String)
{
string result = token.Value<string>();
if (result == null)
{
return true;
}
}
return false;
}
}
}
我在交易 Newtonsoft.json
时遇到 null
处理问题。
我想检查结果是否 null
。基于此,我想处理一些情况。
我的代码如下:
try {
var response = GetApiData.Post(_getApiBaseUrl, data.ToString());
var jsonString = response.ResultString;
var jsonContent = JObject.Parse(jsonString);
if (jsonContent["User"] != null) // <-- null handling
{
var user = JToken.Parse(jsonContent["User"].ToString());
membershipUser = GetMembershipUser(user);
}
}
jsonContent
和null
如下:
{
"User": null
}
如果 "User": null
jsonContent["User"]
returns {}
和 jsonContent["User"] != null
条件没有抛出任何错误,但它不会跳过块,而是执行内线。
所以对于 null
处理,我使用了这个代码:
if (jsonContent["User"].Value<string>() != null)
如果"User": null
,上面的代码工作正常。
但是如果jsonContent["User"]
有有效数据,它会抛出一个错误。
Cannot cast Newtonsoft.Json.Linq.JObject to Newtonsoft.Json.Linq.JToken
有效数据jsonContent
如下:
{
"User": {
"Claims": [],
"Logins": [],
"Roles": [],
"FirstName": "Unknown",
"LastName": "Unknown",
"IsApproved": true,
"IsDeleted": false,
"Email": "testuser@user.com",
"EmailConfirmed": false,
"PasswordHash": "AC/qXxxxxxxxxx==",
"SecurityStamp": "001f3500-0000-0000-0000-00f92b524700",
"PhoneNumber": null,
"PhoneNumberConfirmed": false,
"TwoFactorEnabled": false,
"LockoutEndDateUtc": null,
"LockoutEnabled": false,
"AccessFailedCount": 0,
"Id": "00f50a00-0000-0000-0000-000b97bf2800",
"UserName": "testUser"
}
}
如何使用有效数据和空值实现这种空值处理?
您可以检查 JToken.Type
being JTokenType.Null
:
var jsonContent = JObject.Parse(jsonString);
var user = jsonContent["User"];
if (user != null && user.Type != JTokenType.Null)
{
membershipUser = GetMembershipUser(user);
}
为了方便检查,可以引入扩展方法:
public static partial class JTokenExtensions
{
public static bool IsNull(this JToken token)
{
return token == null || token.Type == JTokenType.Null;
}
}
然后做:
if (!user.IsNull())
{
membershipUser = GetMembershipUser(user);
}
我添加以扩展上面的 dbc 的答案,因为我发现 JToken 被字符串变量分配给 null 的情况。然后 JTokenType 为 JTokneType.String,但该值仍然为 null。
using Newtonsoft.Json.Linq;
namespace Common
{
public static class JTokenExtensions
{
public static bool IsNull(this JToken token)
{
if (token == null || token.Type == JTokenType.Null)
{
return true;
}
// Catch the case where a user uses a NULL string variable to set the JToken to null
// If it was directly assigned to a null, the test above will work; however, if you
// set the JToken with a string variable that is null, you need this test!
if (token.Type == JTokenType.String)
{
string result = token.Value<string>();
if (result == null)
{
return true;
}
}
return false;
}
}
}