繁体   English   中英

序列化和反序列化 json c#

[英]Serialize and deserialize json c#

我正在尝试构建 json(成功),但现在我需要反序列化 json,这是我第一次使用 json,所以我真的不知道该怎么做。 当我尝试反序列化时,我得到了例外:

Cannot deserialize the current JSON array (eg [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,Boosty.AltsInfos[]]' because the type requires a JSON object (eg {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (eg {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (eg ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '', line 1, position 1.

  • 序列化器:

     var json = new List<Alts>(); List<AltsInfos> p1 = new List<AltsInfos> { new AltsInfos { alts_name = "JeSaisPas", alts_email = "truc2fou@g.com", alts_pass = "azerty", alts_type = AltsInfos.AltType.Microsoft } }; json.Add(new Alts { account = p1 }); List<AltsInfos> p2 = new List<AltsInfos> { new AltsInfos { alts_name = "TrucBidule", alts_email = "trucmachun@g.com", alts_pass = "azerty2.0", alts_type = AltsInfos.AltType.Microsoft } }; json.Add(new Alts { account = p2 }); string jsonString = JsonConvert.SerializeObject(json, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.Indented }); File.WriteAllText(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"/BoostyLauncher" + @"/alts.json", jsonString);
  • 解串器

     using (StreamReader sr = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"/BoostyLauncher" + @"/alts.json")) { string json = sr.ReadToEnd(); List<AltsInfos> alts = JsonConvert.DeserializeObject<List<AltsInfos>>(json); foreach (var item in alts) Console.WriteLine(item.alts_email); }
  • 类型容器

    class Alts { public List<AltsInfos> account { get; set; } } class AltsInfos { public string alts_name { get; set; } public string alts_email { get; set; } public string alts_pass { get; set; } public AltType alts_type { get; set; } public enum AltType { [EnumMember(Value = "Microsoft")] Microsoft, [EnumMember(Value = "Altening")] Altening } }

我想要的只是为每个帐户类别提取alts_name 另外我不知道如何在 json 中设置枚举,它设置为 0。

[
  {
    "account": [
      {
        "alts_name": "JeSaisPas",
        "alts_email": "truc2fou@g.com",
        "alts_pass": "azerty",
        "alts_type": 0
      }
    ]
  },
  {
    "account": [
      {
        "alts_name": "TrucBidule",
        "alts_email": "trucmachun@g.com",
        "alts_pass": "azerty2.0",
        "alts_type": 0
      }
    ]
  }
]

我已经对反序列化的行进行了更改。 应始终反序列化为相同的 class (序列化为 json)。

var json = new List<Alts>();
   List<AltsInfos> p1 = new List<AltsInfos> { new AltsInfos { alts_name = "JeSaisPas", alts_email = "truc2fou@g.com", alts_pass = "azerty", alts_type = AltType.Microsoft } };
   json.Add(new Alts { account = p1 });
   List<AltsInfos> p2 = new List<AltsInfos> { new AltsInfos { alts_name = "TrucBidule", alts_email = "trucmachun@g.com", alts_pass = "azerty2.0", alts_type = AltType.Microsoft } };
   json.Add(new Alts { account = p2 });

   string jsonString = JsonConvert.SerializeObject(json, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.Indented });
   **List<Alts>** alts = JsonConvert.DeserializeObject<**List<Alts>**>(jsonString);

请让我知道它是否解决了您的问题,否则将尝试解释更多。

您需要修复反序列化代码中的错误。 而不是 List< AltsInfos > 使用 List< Alts >

List<Alts> alts = JsonConvert.DeserializeObject<List<Alts>>(jsonString);

foreach (var alt in alts)
           foreach (var item in alt.account)
              Console.WriteLine(item.alts_email);

或者你可以使用 LINQ

List<string> emails = alts.SelectMany(a => a.account.Select(s => s.alts_email)).ToList();

顺便说一句,如果您想要缩进 json 字符串,请修复序列化程序选项

string jsonString = JsonConvert.SerializeObject(json, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM