我有这个json数据要解析,但我得到了解析错误,一些可以解释一些吗?
这是我希望在收到时解析的Json数据:
{
"id": "/subscriptions/xxxx",
"type": "Microsoft.ApiManagement/service/users",
"name": "bbbbb",
"properties": {
"firstName": "aaaa",
"lastName": "cccc",
"email": "someone@somewhere.xyz",
"state": "active",
"registrationDate": "somedate",
"note": null,
"groups": [],
"identities": [
{
"provider": "Basic",
"id": "someone@somewhere.xyz"
}
]
}
}
这些是我创建的类,用于将数据反序列化为:
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=新的StringContent(json,encoding.utf8,“application/json”); var newUserResult=新的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]Executed'TestCreateAPImuser'[09/06/2020 13:34:13]System.Private.Corelib:执行函数时异常:TestCreateAPImuser。 newtonSoft.json:解析值时遇到意外字符:{。Path“properties.groups”,第13行,位置7。
您需要更改以下属性的声明。 因为note
和group
可以是组,可以是其他对象,所以string
和ilist
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"": ""someone@somewhere.xyz"",
""state"": ""active"",
""registrationDate"": ""somedate"",
""note"": null,
""groups"": [],
""identities"": [
{
""provider"": ""Basic"",
""id"": ""someone@somewhere.xyz""
}
]
}
}";
var result = JsonConvert.DeserializeObject<NewUserAPIMResultingData>(json);
Assert.IsNotNull(result);
Assert.IsTrue(result.properties.identities.Count() == 1);
}
我复制了类和通过的测试,唯一的区别是我将json粘贴为常量,因此Visual Studio自动对其进行正确编码。