Json 用 in a List 反序列化列表

发布于 2024-11-06 01:22:15 字数 812 浏览 1 评论 0原文

是否可以使用 JsonConvert.DeserializeObject 反序列化列表中的列表?

Object: 
public class BusinessList
{
    public long Number{ get; set; }
    public string BusinessUnitCode { get; set; }
    public List<string> Codes { get; set; }
}

Json:

{"Number":111121,"BusinessUnitCode":"ABC","Codes":["11111-2","14333-8"]}

Deserialization: 

var dataList = (List<BusinessList>)JsonConvert.DeserializeObject(reciveObject, typeof(List<BusinessList>), jsonSerializerSettings);

错误:

"Expected a JsonObjectContract or JsonDictionaryContract for 
type 'System.Collections.Generic.List`1
[PathResultsPortal.UI.Controllers.AnalyteDetails]', 
got 'Newtonsoft.Json.Serialization.JsonArrayContract'."

如果您从 BusinessList 对象中删除代码列表,它工作正常,但似乎无法处理列表中的列表,这是否可能?

Is it possible to deserialise a list with in a list using the JsonConvert.DeserializeObject?

Object: 
public class BusinessList
{
    public long Number{ get; set; }
    public string BusinessUnitCode { get; set; }
    public List<string> Codes { get; set; }
}

Json:

{"Number":111121,"BusinessUnitCode":"ABC","Codes":["11111-2","14333-8"]}

Deserialization: 

var dataList = (List<BusinessList>)JsonConvert.DeserializeObject(reciveObject, typeof(List<BusinessList>), jsonSerializerSettings);

Error:

"Expected a JsonObjectContract or JsonDictionaryContract for 
type 'System.Collections.Generic.List`1
[PathResultsPortal.UI.Controllers.AnalyteDetails]', 
got 'Newtonsoft.Json.Serialization.JsonArrayContract'."

If you remove the Codes List from the BusinessList Object it works fine it can't seem to handle the list with in a list, is it even possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潦草背影 2024-11-13 01:22:15
public class BusinessList 
{
    [JsonProperty(PropertyName = "Number")]
    public long Number{ get; set; }

    [JsonProperty(PropertyName = "BusinessUnitCode ")]
    public string BusinessUnitCode { get; set; }

    [JsonProperty(PropertyName = "Codes ")]
    public List<Codes> Codes { get; set; }
}

public class Codes
{
    public string Code { get; set; }
}

试试这个

public class BusinessList 
{
    [JsonProperty(PropertyName = "Number")]
    public long Number{ get; set; }

    [JsonProperty(PropertyName = "BusinessUnitCode ")]
    public string BusinessUnitCode { get; set; }

    [JsonProperty(PropertyName = "Codes ")]
    public List<Codes> Codes { get; set; }
}

public class Codes
{
    public string Code { get; set; }
}

try this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文