使用 Newtonsoft.Json 库反序列化 Firebase 用户数据响应
Deserializing Firebase User Data response using Newtonsoft.Json library
我正在使用 C# 反序列化 Firebase Auth REST API 使用 Newtonsoft.Json 库获取用户响应。下面是 JSON 输出的示例:
{
"kind": "identitytoolkit#GetAccountInfoResponse",
"users": [
{
"localId": "asdfsdsfs",
"email": "x.y@g.com",
"passwordHash": "asdsdfsdfd",
"emailVerified": false,
"passwordUpdatedAt": 1985545511525,
"providerUserInfo": [
{
"providerId": "password",
"federatedId": "x.y@g.com",
"email": "x.y@g.com",
"rawId": "x.y@g.com"
}
],
"validSince": "16496321050",
"lastLoginAt": "16874526844",
"createdAt": "164123654725",
"lastRefreshAt": "2022-03-19T16:53:56.844Z"
}
]
}
我使用这段代码来尝试反序列化它:
Dictionary<string, List<Dictionary<string, object>>> responseText = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, List<Dictionary<string, object>>>>(request.downloadHandler.text);
但是,我得到这个错误:
ArgumentException: Could not cast or convert from System.String to
System.Collections.Generic.List1[System.Collections.Generic.Dictionary
2[System.String,System.Object]].
Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable
(System.Object value, System.Type initialType, System.Type targetType)
(at <7ca8898b690a4181a32a9cf767cedb1e>:0)
Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast (System.Object
initialValue, System.Globalization.CultureInfo culture, System.Type
targetType) (at <7ca8898b690a4181a32a9cf767cedb1e>:0)
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType
(Newtonsoft.Json.JsonReader reader, System.Object value,
System.Globalization.CultureInfo culture,
Newtonsoft.Json.Serialization.JsonContract contract, System.Type
targetType) (at <7ca8898b690a4181a32a9cf767cedb1e>:0) Rethrow as
JsonSerializationException: Error converting value
"identitytoolkit#GetAccountInfoResponse" to type
'System.Collections.Generic.List1[System.Collections.Generic.Dictionary
2[System.String,System.Object]]'.
Path 'kind', line 2, position 50.
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType
(Newtonsoft.Json.JsonReader reader, System.Object value,
System.Globalization.CultureInfo culture,
Newtonsoft.Json.Serialization.JsonContract contract, System.Type
targetType) (at <7ca8898b690a4181a32a9cf767cedb1e>:0)
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal
(Newtonsoft.Json.JsonReader reader, System.Type objectType,
Newtonsoft.Json.Serialization.JsonContract contract,
Newtonsoft.Json.Serialization.JsonProperty member,
Newtonsoft.Json.Serialization.JsonContainerContract containerContract,
Newtonsoft.Json.Serialization.JsonProperty containerMember,
System.Object existingValue) (at <7ca8898b690a4181a32a9cf767cedb1e>:0)
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateDictionary
(System.Collections.IDictionary dictionary, Newtonsoft.Json.JsonReader
reader, Newtonsoft.Json.Serialization.JsonDictionaryContract contract,
Newtonsoft.Json.Serialization.JsonProperty containerProperty,
System.String id) (at <7ca8898b690a4181a32a9cf767cedb1e>:0)
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject
(Newtonsoft.Json.JsonReader reader, System.Type objectType,
Newtonsoft.Json.Serialization.JsonContract contract,
Newtonsoft.Json.Serialization.JsonProperty member,
Newtonsoft.Json.Serialization.JsonContainerContract containerContract,
Newtonsoft.Json.Serialization.JsonProperty containerMember,
System.Object existingValue) (at <7ca8898b690a4181a32a9cf767cedb1e>:0)
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal
(Newtonsoft.Json.JsonReader reader, System.Type objectType,
Newtonsoft.Json.Serialization.JsonContract contract,
Newtonsoft.Json.Serialization.JsonProperty member,
Newtonsoft.Json.Serialization.JsonContainerContract containerContract,
Newtonsoft.Json.Serialization.JsonProperty containerMember,
System.Object existingValue) (at <7ca8898b690a4181a32a9cf767cedb1e>:0)
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize
(Newtonsoft.Json.JsonReader reader, System.Type objectType,
System.Boolean checkAdditionalContent) (at
<7ca8898b690a4181a32a9cf767cedb1e>:0)
Newtonsoft.Json.JsonSerializer.DeserializeInternal
(Newtonsoft.Json.JsonReader reader, System.Type objectType) (at
<7ca8898b690a4181a32a9cf767cedb1e>:0)
Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader
reader, System.Type objectType) (at
<7ca8898b690a4181a32a9cf767cedb1e>:0)
Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value,
System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) (at
<7ca8898b690a4181a32a9cf767cedb1e>:0)
Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value,
Newtonsoft.Json.JsonSerializerSettings settings) (at
<7ca8898b690a4181a32a9cf767cedb1e>:0)
Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value)
(at <7ca8898b690a4181a32a9cf767cedb1e>:0)
错误告诉您它无法将字符串转换为 List<Dictionary<string, object>>
- json 值不代表 List<Dictionary<,>>
.
您可以将 json 反序列化为 Dictionary<string, object>
或 dynamic
:
Dictionary<string, object> responseText = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
// or
dynamic responseText = JsonConvert.DeserializeObject<dynamic>(json);
但是,除非 json 是动态的并且不断变化,否则更好的选择是反序列化为具体的 class 或 class 模型 json。有在线应用程序可帮助您将 json 转换为 C# classes。您可以使用 json2csharp.com, app.quicktype.io,或 VisualStudio 将 JSON 粘贴为 类。
我使用了json2csharp,输出如下(包括如何反序列化json):
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class ProviderUserInfo
{
public string providerId { get; set; }
public string federatedId { get; set; }
public string email { get; set; }
public string rawId { get; set; }
}
public class User
{
public string localId { get; set; }
public string email { get; set; }
public string passwordHash { get; set; }
public bool emailVerified { get; set; }
public long passwordUpdatedAt { get; set; }
public List<ProviderUserInfo> providerUserInfo { get; set; }
public string validSince { get; set; }
public string lastLoginAt { get; set; }
public string createdAt { get; set; }
public DateTime lastRefreshAt { get; set; }
}
public class Root
{
public string kind { get; set; }
public List<User> users { get; set; }
}
使用此 site 将您的 json 转换为 c# class
这是你的模特
public class ModelDeserialize
{
[JsonProperty("kind")]
public string Kind { get; set; }
[JsonProperty("users")]
public User[] Users { get; set; }
}
public class User
{
[JsonProperty("localId")]
public string LocalId { get; set; }
[JsonProperty("email")]
public string Email { get; set; }
[JsonProperty("passwordHash")]
public string PasswordHash { get; set; }
[JsonProperty("emailVerified")]
public bool EmailVerified { get; set; }
[JsonProperty("passwordUpdatedAt")]
public long PasswordUpdatedAt { get; set; }
[JsonProperty("providerUserInfo")]
public ProviderUserInfo[] ProviderUserInfo { get; set; }
[JsonProperty("validSince")]
public string ValidSince { get; set; }
[JsonProperty("lastLoginAt")]
public string LastLoginAt { get; set; }
[JsonProperty("createdAt")]
public string CreatedAt { get; set; }
[JsonProperty("lastRefreshAt")]
public DateTimeOffset LastRefreshAt { get; set; }
}
public class ProviderUserInfo
{
[JsonProperty("providerId")]
public string ProviderId { get; set; }
[JsonProperty("federatedId")]
public string FederatedId { get; set; }
[JsonProperty("email")]
public string Email { get; set; }
[JsonProperty("rawId")]
public string RawId { get; set; }
}
最终反序列化
var dese = JsonConvert.DeserializeObject<ModelDeserialize>(json);
(var datas in dese.Users){
Console.WriteLine(datas.Email);
检查 example
我正在使用 C# 反序列化 Firebase Auth REST API 使用 Newtonsoft.Json 库获取用户响应。下面是 JSON 输出的示例:
{
"kind": "identitytoolkit#GetAccountInfoResponse",
"users": [
{
"localId": "asdfsdsfs",
"email": "x.y@g.com",
"passwordHash": "asdsdfsdfd",
"emailVerified": false,
"passwordUpdatedAt": 1985545511525,
"providerUserInfo": [
{
"providerId": "password",
"federatedId": "x.y@g.com",
"email": "x.y@g.com",
"rawId": "x.y@g.com"
}
],
"validSince": "16496321050",
"lastLoginAt": "16874526844",
"createdAt": "164123654725",
"lastRefreshAt": "2022-03-19T16:53:56.844Z"
}
]
}
我使用这段代码来尝试反序列化它:
Dictionary<string, List<Dictionary<string, object>>> responseText = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, List<Dictionary<string, object>>>>(request.downloadHandler.text);
但是,我得到这个错误:
ArgumentException: Could not cast or convert from System.String to System.Collections.Generic.List
1[System.Collections.Generic.Dictionary
2[System.String,System.Object]]. Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable (System.Object value, System.Type initialType, System.Type targetType) (at <7ca8898b690a4181a32a9cf767cedb1e>:0) Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast (System.Object initialValue, System.Globalization.CultureInfo culture, System.Type targetType) (at <7ca8898b690a4181a32a9cf767cedb1e>:0) Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType (Newtonsoft.Json.JsonReader reader, System.Object value, System.Globalization.CultureInfo culture, Newtonsoft.Json.Serialization.JsonContract contract, System.Type targetType) (at <7ca8898b690a4181a32a9cf767cedb1e>:0) Rethrow as JsonSerializationException: Error converting value "identitytoolkit#GetAccountInfoResponse" to type 'System.Collections.Generic.List1[System.Collections.Generic.Dictionary
2[System.String,System.Object]]'. Path 'kind', line 2, position 50. Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType (Newtonsoft.Json.JsonReader reader, System.Object value, System.Globalization.CultureInfo culture, Newtonsoft.Json.Serialization.JsonContract contract, System.Type targetType) (at <7ca8898b690a4181a32a9cf767cedb1e>:0) Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) (at <7ca8898b690a4181a32a9cf767cedb1e>:0) Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateDictionary (System.Collections.IDictionary dictionary, Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonDictionaryContract contract, Newtonsoft.Json.Serialization.JsonProperty containerProperty, System.String id) (at <7ca8898b690a4181a32a9cf767cedb1e>:0) Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) (at <7ca8898b690a4181a32a9cf767cedb1e>:0) Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) (at <7ca8898b690a4181a32a9cf767cedb1e>:0) Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Boolean checkAdditionalContent) (at <7ca8898b690a4181a32a9cf767cedb1e>:0) Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) (at <7ca8898b690a4181a32a9cf767cedb1e>:0) Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) (at <7ca8898b690a4181a32a9cf767cedb1e>:0) Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) (at <7ca8898b690a4181a32a9cf767cedb1e>:0) Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value, Newtonsoft.Json.JsonSerializerSettings settings) (at <7ca8898b690a4181a32a9cf767cedb1e>:0) Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value) (at <7ca8898b690a4181a32a9cf767cedb1e>:0)
错误告诉您它无法将字符串转换为 List<Dictionary<string, object>>
- json 值不代表 List<Dictionary<,>>
.
您可以将 json 反序列化为 Dictionary<string, object>
或 dynamic
:
Dictionary<string, object> responseText = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
// or
dynamic responseText = JsonConvert.DeserializeObject<dynamic>(json);
但是,除非 json 是动态的并且不断变化,否则更好的选择是反序列化为具体的 class 或 class 模型 json。有在线应用程序可帮助您将 json 转换为 C# classes。您可以使用 json2csharp.com, app.quicktype.io,或 VisualStudio 将 JSON 粘贴为 类。
我使用了json2csharp,输出如下(包括如何反序列化json):
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class ProviderUserInfo
{
public string providerId { get; set; }
public string federatedId { get; set; }
public string email { get; set; }
public string rawId { get; set; }
}
public class User
{
public string localId { get; set; }
public string email { get; set; }
public string passwordHash { get; set; }
public bool emailVerified { get; set; }
public long passwordUpdatedAt { get; set; }
public List<ProviderUserInfo> providerUserInfo { get; set; }
public string validSince { get; set; }
public string lastLoginAt { get; set; }
public string createdAt { get; set; }
public DateTime lastRefreshAt { get; set; }
}
public class Root
{
public string kind { get; set; }
public List<User> users { get; set; }
}
使用此 site 将您的 json 转换为 c# class
这是你的模特
public class ModelDeserialize
{
[JsonProperty("kind")]
public string Kind { get; set; }
[JsonProperty("users")]
public User[] Users { get; set; }
}
public class User
{
[JsonProperty("localId")]
public string LocalId { get; set; }
[JsonProperty("email")]
public string Email { get; set; }
[JsonProperty("passwordHash")]
public string PasswordHash { get; set; }
[JsonProperty("emailVerified")]
public bool EmailVerified { get; set; }
[JsonProperty("passwordUpdatedAt")]
public long PasswordUpdatedAt { get; set; }
[JsonProperty("providerUserInfo")]
public ProviderUserInfo[] ProviderUserInfo { get; set; }
[JsonProperty("validSince")]
public string ValidSince { get; set; }
[JsonProperty("lastLoginAt")]
public string LastLoginAt { get; set; }
[JsonProperty("createdAt")]
public string CreatedAt { get; set; }
[JsonProperty("lastRefreshAt")]
public DateTimeOffset LastRefreshAt { get; set; }
}
public class ProviderUserInfo
{
[JsonProperty("providerId")]
public string ProviderId { get; set; }
[JsonProperty("federatedId")]
public string FederatedId { get; set; }
[JsonProperty("email")]
public string Email { get; set; }
[JsonProperty("rawId")]
public string RawId { get; set; }
}
最终反序列化
var dese = JsonConvert.DeserializeObject<ModelDeserialize>(json);
(var datas in dese.Users){
Console.WriteLine(datas.Email);
检查 example