Deserialize json from file in c #

I managed to find a solution without removing the paths from the keys.
Thanks for helping the guys and also pointing out the problems, I really appreciate it! :)

I loaded Json into a string, deserialized it into a dynamic one, passed a profile through it, and added it to the list with ResFiles in it.

static void loadJson() { List<ResFile> fileList = new List<ResFile>(); string jsonString = File.ReadAllText(jsonPath); dynamic files = JsonConvert.DeserializeObject(jsonString); foreach (var f in files.objects) fileList.Add(new ResFile(f.Name, f.Value.hash.ToString(), (int)f.Value.size.Value)); } 




I am trying to deserialize some Json file in C # using the Newtonsoft Json library.
The files are named after the hash, not the name of the real file, and I want to rename them to names that should be specified as follows: 10a54fc66c8f479bb65c8d39c3b62265ac82e742 → file_1.ext

Json File:

 { "files": { "file_1.ext": { "hash": "10a54fc66c8f479bb65c8d39c3b62265ac82e742", "size": 8112 }, "file_2.ext": { "hash": "14cfb2f24e7d91dbc22a2a0e3b880d9829320243", "size": 7347 }, "file_3.ext": { "hash": "bf7fadaf64945f6b31c803d086ac6a652aabef9b", "size": 3838 }, "file_4.ext": { "hash": "48f7e1bb098abd36b9760cca27b9d4391a23de26", "size": 6905 } } } 

I tried to deserialize with this:

 static void loadJson() { using (StreamReader reader = new StreamReader(jsonPath)) { string json = reader.ReadToEnd(); dynamic files = JsonConvert.DeserializeObject(json); } } 

Deserialization itself works, but I don't know how to skip it.

I also tried to do this:

 class ResFile { public string name; public string hash; public int size; } 

And somehow make deserialization use this, but that didn't work, of course.

+5
source share
2 answers

According to your json sample, your classes will be:

 public class ResFile { public string hash { set; get; } public int size { set; get; } } public class ResRoot { public Dictionary<string, ResFile> Files { set; get; } } 

You can deserialize as

 var res = JsonConvert.DeserializeObject<ResRoot>(File.ReadAllText(filename)); 

 foreach(var f in res.Files) { Console.WriteLine("Name={0} Size={1}", f.Key, f.Value.size); } 
+15
source

Please follow C # conventions and do not expose member variables as public or start property names in lower case. To make your regular objects deserializable, you can use the DataContract System.Runtime.Serialization DataContract and DataMember attributes. A DataContract indicates that an object of this type is being serialized, and a DataMember is used to specify the serialization name of the property.

 class ResFile { [DataMember(Name = "name")] public string Name { get; set; } [DataMember(Name = "hash")] public string Hash { get; set; } [DataMember(Name = "size")] public int Size { get; set; } public ResFile () { } } [DataContract] class ResFileCollection { [DataMember(Name ="files")] public Dictionary<string, ResFile> Files { get; set; } } 

And here is the deserialization:

 string json = File.ReadAllText("data.json"); var files = JsonConvert.DeserializeObject<ResFileCollection>(json); foreach(KeyValuePair<string, ResFile> f in files.Files) { Console.WriteLine("{0} {1} {2}", f.Key, f.Value.Name, f.Value.Hash); } 

Serialized property names should also be shorter for better performance. Example:

 [DataMember(Name="src")] public string SourcePath { get; set; } 
0
source

All Articles