无法反序列化当前 JSON 对象(例如 {"name":"value"})
Cannot deserialize the current JSON object (e.g. {"name":"value"})
请帮助我,我是 xamarin.forms 和 C# 的新手,我已经尝试了 Whosebug 中给出的所有解决方案,但无法解决
using (var httpClient = new HttpClient())
{
var response = httpClient.GetAsync(Url).Result;
if (response.IsSuccessStatusCode)
{
var responseContent = response.Content;
string contents = await responseContent.ReadAsStringAsync();
List<abcModel> tm = JsonConvert.DeserializeObject<List<abcModel>>(contents);
abcMaster = new ObservableCollection<SummaryModel>();
var c = tm[0].SSum.Count();
}
}
型号
public class abcModel
{
public List<SSum> SSum { get; set; }
}
public class SSum
{
public string Name{ get; set; }
}
我的Json
{"a":[{"SSum":[{"Name":"Earth"}]}]}
错误:-
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[abcModel]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
因为您显然只想反序列化 json 的嵌套部分,请按如下方式进行:
var result = JsonConvert.DeserializeObject<Dictionary<string, List<abcModel>>>(json);
您的 JSON 中缺少 a
属性。您可以反序列化为具有 属性:
的 class
public class MyType
{
public List<abcModel> A { get; set; }
}
JsonConvert.DeserializeObject<MyType>(json);
或者一起跳过 属性( 的答案很有效),这是另一种选择:
JObject obj = JObject.Parse(json);
List<abcModel> model = obj["a"].ToObject<List<abcModel>>();
请注意:通常 C# classes 是 PascalCased
.
如果您已经有了 JSON 字符串,您应该使用像 json2csharp 这样的生成器来创建响应 DTO。这将防止在什么是集合与单个对象方面出现错误。
public class SSum
{
public string Name { get; set; }
}
public class A
{
public List<SSum> SSum { get; set; }
}
public class RootObject
{
public List<A> A { get; set; }
}
现在您可以反序列化整个对象:
tm = JsonConvert.DeserializeObject<RootObject>(contents);
请帮助我,我是 xamarin.forms 和 C# 的新手,我已经尝试了 Whosebug 中给出的所有解决方案,但无法解决
using (var httpClient = new HttpClient())
{
var response = httpClient.GetAsync(Url).Result;
if (response.IsSuccessStatusCode)
{
var responseContent = response.Content;
string contents = await responseContent.ReadAsStringAsync();
List<abcModel> tm = JsonConvert.DeserializeObject<List<abcModel>>(contents);
abcMaster = new ObservableCollection<SummaryModel>();
var c = tm[0].SSum.Count();
}
}
型号
public class abcModel
{
public List<SSum> SSum { get; set; }
}
public class SSum
{
public string Name{ get; set; }
}
我的Json
{"a":[{"SSum":[{"Name":"Earth"}]}]}
错误:-
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[abcModel]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
因为您显然只想反序列化 json 的嵌套部分,请按如下方式进行:
var result = JsonConvert.DeserializeObject<Dictionary<string, List<abcModel>>>(json);
您的 JSON 中缺少 a
属性。您可以反序列化为具有 属性:
public class MyType
{
public List<abcModel> A { get; set; }
}
JsonConvert.DeserializeObject<MyType>(json);
或者一起跳过 属性(
JObject obj = JObject.Parse(json);
List<abcModel> model = obj["a"].ToObject<List<abcModel>>();
请注意:通常 C# classes 是 PascalCased
.
如果您已经有了 JSON 字符串,您应该使用像 json2csharp 这样的生成器来创建响应 DTO。这将防止在什么是集合与单个对象方面出现错误。
public class SSum
{
public string Name { get; set; }
}
public class A
{
public List<SSum> SSum { get; set; }
}
public class RootObject
{
public List<A> A { get; set; }
}
现在您可以反序列化整个对象:
tm = JsonConvert.DeserializeObject<RootObject>(contents);