我有要解析的json data,但是遇到解析错误,请问能否说明一下?
这是我想在收到时解析的Json数据:
{
"id": "/subscriptions/xxxx",
"type": "Microsoft.ApiManagement/service/users",
"name": "bbbbb",
"properties": {
"firstName": "aaaa",
"lastName": "cccc",
"email": "[email protected]",
"state": "active",
"registrationDate": "somedate",
"note": null,
"groups": [],
"identities": [
{
"provider": "Basic",
"id": "[email protected]"
}
]
}
}
这些是我创建的将数据反序列化为的类:
public class NewUserAPIMResultingData
{
[JsonProperty("id")]
public string id { get; set; }
[JsonProperty("type")]
public string thisType { get; set; }
[JsonProperty("name")]
public string name { get; set; }
[JsonProperty("properties")]
public NewUserAPIMResultingDataProperties properties { get; set; }
}
public class NewUserAPIMResultingDataProperties
{
[JsonProperty("firstName")]
public string userFirstName { get; set; }
[JsonProperty("lastName")]
public string userLastName { get; set; }
[JsonProperty("email")]
public string userEmail { get; set; }
[JsonProperty("state")]
public string state { get; set; }
[JsonProperty("registrationDate")]
public string registrationDate { get; set; }
[JsonProperty("note")]
public string note { get; set; }
[JsonProperty("groups")]
public IEnumerable<string> groups { get; set; }
[JsonProperty("identities")]
public IEnumerable<NewUserAPIMResultingDataPropertyIdentity> identities { get; set; }
}
public class NewUserAPIMResultingDataPropertyIdentity
{
[JsonProperty("provider")]
public string provider { get; set; }
[JsonProperty("id")]
public string id { get; set; }
}
这是我用来读取已接收和已解析的json数据的.NET c#代码:
var formCreateUserContent = new StringContent(json,Encoding.UTF8,“ application / json”); var newUserResult = new NewUserAPIMResultingData();
using (HttpClient client2 = new HttpClient())
{
using (HttpResponseMessage response = await client2.PutAsync(url, formCreateUserContent))
{
using (HttpContent content = response.Content)
{
var stringContent = await content.ReadAsStringAsync();
newUserResult = JsonConvert.DeserializeObject<NewUserAPIMResultingData>(stringContent);
}
foreach (var z in newUserResult.properties.identities)
Console.WriteLine(z);
}
}
这是我在控制台上收到的错误:[09/06/2020 13:34:13]执行了'TestCreateAPIMUser'[09/06/2020 13:34:13] System.Private.CoreLib:执行函数TestCreateAPIMUser时发生异常。 Newtonsoft.Json:解析值{时遇到意外字符。路径“ properties.groups”,第13行,位置7。
您需要更改以下属性的声明。由于note
和groups
可以是组,因此可以是其他对象,因此string
和IList<string>
public object note { get; set; }
public IList<object> groups { get; set; }
可能与源json的编码有关?您可以使用以下测试代码来验证您的C#类定义是否正确...
[Test]
public void Test()
{
const string json = @"{
""id"": ""/subscriptions/xxxx"",
""type"": ""Microsoft.ApiManagement/service/users"",
""name"": ""bbbbb"",
""properties"": {
""firstName"": ""aaaa"",
""lastName"": ""cccc"",
""email"": ""[email protected]"",
""state"": ""active"",
""registrationDate"": ""somedate"",
""note"": null,
""groups"": [],
""identities"": [
{
""provider"": ""Basic"",
""id"": ""[email protected]""
}
]
}
}";
var result = JsonConvert.DeserializeObject<NewUserAPIMResultingData>(json);
Assert.IsNotNull(result);
Assert.IsTrue(result.properties.identities.Count() == 1);
}
我复制了类并通过了测试,唯一的区别是我将json粘贴为常量,因此Visual Studio自动正确地对其进行编码。