C # Create a .NET object to serialize and deserialize JSON

I am working on a point system for my botton in C #. I need to save a list of users with their number of points and hours in a local .txt file, for example:

  • users: ???
    • Username:???
      • nickname: string
      • points: int
      • hours: int
    • Username:???
      • nickname: string
      • points: int
      • hours: int
    • Username:???
      • nickname: string
      • points: int
      • hours: int

I am trying to create a .NET object to serialize into a JSON string and write it to my .txt file, but I'm stuck here.

I suppose users need some kind of Array, but what should I do with the username? I don't think JSON supports custom types?

Thanks for your time, X3ntr

+6
source share
2 answers

First we need to create a model. This model must conform to the JSON structure. There are several ways to create this model.

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

You can find this option here: enter image description here

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

We got a model in which we can use lib, for example JSON.net to deserialize our JSON. The website also has additional 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 your model, this will not always result in an error. Therefore, make sure that all properties have been correctly serialized.

Serialization is as simple as deserialization.

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

Optional encoding is added when writing json.

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

I will take a picture.

First we need a model for our user:

 public class User { public Guid Id { get; set; } public string Username { get; set; } public string NickName { get; set; } public int Points { get ;set; } public int Hours { get; set; } } 

Then in your main method or wherever you are with your users:

 List<User> users = new List<User>(); 

Note that I added the Id property. Even though your usernames may be unique, it is always good practice to have some kind of property whose work is just an identifier. This property should never change during the entire life of the user. If the username can change on the same day, the identifier will always remain the same.

Anyway, now to turn your list into Json:

 string myJsonString = JsonConvert.SerializeObject(users, Formatting.Indented); 

Et voila! Your user list is now in a neat Json line. The above line of code is from JSON.NET, you can get it as a Nuget package . Full qualifications - Newtonsoft.Json.JsonConvert. You can pretty much trust JSON.NET to handle almost everything you throw at it, especially for the simple User class, how you are going to throw it.

Hope this helps you get started.

+6
source

All Articles