C#创建用于JSON序列化和反序列化的.NET对象 [英] C# Create a .NET object for JSON serialization and deserialization

查看:162
本文介绍了C#创建用于JSON序列化和反序列化的.NET对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为C#中的抽搐聊天机器人开发一个积分系统.我需要将用户的积分和小时数列表保存到本地.txt文件中,如下所示:

I'm working on a point system for my twitch chat bot in C#. I need to save a list of users with their amount of points and hours to a local .txt file like this:

  • 用户:???
    • 用户名:???
      • nick:字符串
      • 要点:int
      • 小时:int
      • users : ???
        • username : ???
          • nick : string
          • points : int
          • hours : int
          • nick:字符串
          • 要点:int
          • 小时:int
          • nick:字符串
          • 要点:int
          • 小时:int

          我正在尝试创建一个.NET对象以序列化为JSON字符串并将其写入我的.txt文件,但是我被卡在这里.

          I'm trying to create a .NET Object to serialize to a JSON string and write this to my .txt file but I'm stuck here.

          我认为用户必须是某种数组,但是如何使用用户名呢?我不认为JSON支持自定义类型?

          I figure users needs to be an Array of some sort but what do I do with username? I don't think JSON supports custom types?

          感谢您的宝贵时间, X3ntr

          Thanks for your time, X3ntr

          推荐答案

          首先,我们需要创建一个模型.该模型需要匹配JSON的结构.有两种创建此模型的方法.

          First we need to create a model. This model needs to match the structure of the JSON. There are a couple of ways to create this model.

          最好和最安全的方法是手动编写.如果您使用的是vs2015,还可以复制json并将其粘贴为类.

          Probably the best and safest way is to write it manualy. If you are using vs2015 you can also copy the json and paste it as class.

          您可以在这里找到此选项:

          You can find this option here:

          另一种选择是使用 http://json2csharp.com/这样的网站,您可以在其中粘贴JSON和这还将为您的JSON生成类.请记住,这些生成器并不是100%准确的,因此您可能必须自行更改一些属性.

          Another option is to use a website like http://json2csharp.com/ here you can paste the JSON and this will also generate classes for your JSON. Keep in mind that these generator are not 100% accurate so you might have to change some properties your self.

          我们得到了一个模型,我们可以使用类似 JSON.net 的库来反序列化JSON.网站上还有一些有关如何使用JSON.net的额外文档

          Ones we got the model we can use a lib like JSON.net to Deserialize our JSON. On the website is also some extra documentation on how to use JSON.net

          var userList = JsonConvert.DeserializeObject<List<UserModel>>(users); //Users being the json as text.
          

          请记住,如果您在模型中错过了一个属性,这将不会总是引发错误.因此,请确保所有属性均已正确序列化.

          Remember that if you miss a property in you model this will not always throw an error. So make sure that all properties got serialize correctly.

          序列化与反序列化一样简单.

          Serializing is as simple as Deserializing.

          string json = JsonConvert.SerializeObject(model);
          File.WriteAllText("C:\json.txt",json); 
          

          可选的是在编写json时添加一种编码.

          Optional is adding an encoding when writing the json.

          File.WriteAllText("C:\json.txt",json,Encoding.UTF8);
          

          这篇关于C#创建用于JSON序列化和反序列化的.NET对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆