【问题标题】:Convert json string to list object (Index number of json object affects the convertion)将json字符串转换为列表对象(json对象的索引号影响转换)
【发布时间】:2019-02-27 19:54:21
【问题描述】:

请看照片。

我在尝试将 JSON 字符串转换为列表对象时遇到了困难。 排除故障后,我发现问题是由于 Json 索引(数字是混乱的,而不是顺序 1,2,4,5,6,5601,5611 ..)。任何人都知道如何将 json 转换为升序列表中的对象 12345678910.. (model[0], model1, model[2], model[3] ....)

        using (StreamReader reader = new StreamReader(stream))
        {
            jsonstr = reader.ReadToEnd();
        }
        var settings = new JsonSerializerSettings
        {
            NullValueHandling = NullValueHandling.Ignore,
            MissingMemberHandling = MissingMemberHandling.Ignore
        };

        List<Notification> model = JsonConvert.DeserializeObject<List<Notification>>(jsonstr, settings).Where(m => m != null).ToList();

错误信息:

Newtonsoft.Json.JsonSerializationException: '无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List`1',因为该类型需要 JSON 数组(例如[1,2,3]) 正确反序列化。 要修复此错误,要么将 JSON 更改为 JSON 数组(例如 [1,2,3]),要么将反序列化类型更改为普通的 .NET 类型(例如,不是像整数这样的原始类型,而不是像这样的集合类型可以从 JSON 对象反序列化的数组或列表。 JsonObjectAttribute 也可以添加到类型中以强制它从 JSON 对象反序列化。 路径 '1',第 1 行,位置 5。'

JSON 结果

{"1":{"confirmation_status":"Rejected","createDateTime":"2018-08-31 16:58:58.087","logID":1,"notification_message":"\"There is a change request for \"Vital\" to be approved.\"","read_status":"true","recipient":"68d9dffc-b3be-4527-a89a-829b76abf754","sender":"01a94cf4-5df4-4097-b6ea-d929170de1e7","senderDetails":"Adeline Tan "},"6":{"confirmation_status":"Rejected","createDateTime":"2018-08-31 16:58:58.087","logID":6,"notification_message":"\"There is a change request for \"Vital\" to be approved.\"","read_status":"true","recipient":"68d9dffc-b3be-4527-a89a-829b76abf754","sender":"01a94cf4-5df4-4097-b6ea-d929170de1e7","senderDetails":"Adeline Tan "},"5601":{"confirmation_status":"Pending","createDateTime":"2018-08-31 16:58:58.087","logID":5601,"notification_message":"\"There is a change request for \"Vital\" to be approved.\"","read_status":"true","recipient":"68d9dffc-b3be-4527-a89a-829b76abf754","sender":"01a94cf4-5df4-4097-b6ea-d929170de1e7","senderDetails":"Adeline Tan "},"5611":{"confirmation_status":"Pending","createDateTime":"2018-08-31 16:58:58.087","logID":5611,"notification_message":"\"There is a change request for \"Vital\" to be approved.\"","read_status":"true","recipient":"68d9dffc-b3be-4527-a89a-829b76abf754","sender":"01a94cf4-5df4-4097-b6ea-d929170de1e7","senderDetails":"Adeline Tan "}}

通知类:

 public class Notification
{
    public string confirmation_status { get; set; }
    public string createDateTime { get; set; }
    public int logID { get; set; }
    public string notification_message { get; set; }
    public string read_status { get; set; }
    public string recipient { get; set; }
    public string sender {get; set; }
    public string senderDetails { get; set; }
}

【问题讨论】:

  • 你能给我们看看你的json数据吗?
  • 向我们展示您的 JSON。错误消息说您试图将单个对象转换为列表
  • 也显示通知类
  • 显示的 JSON 是一个对象而不是一个数组
  • 谢谢你们的帮助。我按要求添加了 json 数据和通知。

标签: c# json


【解决方案1】:

显示的 JSON 是一个对象,而不是一个数组

你很可能想要这个

List<Notification> model = JsonConvert.DeserializeObject<Dictionay<string,Notification>>(jsonstr, settings)
.Select(kvp => kvp.Value)    
.Where(m => m != null)
.OrderBy(m => m.logID)
.ToList();

将 JSON 转换为字典并将值提取为所需类型的位置。

【讨论】:

    猜你喜欢
    • 2018-05-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-27
    • 2020-05-27
    • 1970-01-01
    • 2014-04-07
    • 2018-05-03
    相关资源
    最近更新 更多