简体   繁体   English

从C#中的文件反序列化Json

[英]Deserialize Json from file in C#

I've managed to find a solution without removing the paths from the keys. 我设法找到了一个解决方案,而没有从键中删除路径。
Thanks for the help guys, and also pointing out problems, I really appreciate it! 感谢帮助人员,并指出问题,我真的很感激! :) :)

Loaded the Json to a string, deserialized it into a dynamic, ran a foreach through it, and added to a List with ResFiles in it. 将Json加载到一个字符串中,将其反序列化为动态,通过它运行foreach,并将其添加到包含ResFiles的List中。

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'm trying to deserialize some Json file in C# with Newtonsoft's Json library. 我正在尝试使用Newtonsoft的Json库在C#中反序列化一些Json文件。
The files are named after it's hash, not the real file name and I want to rename them back to the proper names, so like this: 这些文件是以它的哈希值命名的,而不是真正的文件名,我想将它们重命名为正确的名称,所以像这样:
10a54fc66c8f479bb65c8d39c3b62265ac82e742 >> file_1.ext 10a54fc66c8f479bb65c8d39c3b62265ac82e742 >> file_1.ext

The Json file: Json文件:

{
  "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've tried deserialize with this: 我试过反序列化:

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

The deserialization itself working, but I don't know how to loop through them. 反序列化本身有效,但我不知道如何循环它们。

I've also tried to do this: 我也试过这样做:

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

And somehow force the deserialization to use this, but it didn't work of course. 并以某种方式迫使反序列化使用它,但它当然不起作用。

According to your sample json, your classes would be: 根据您的样本json,您的课程将是:

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);
}

Please follow the C# conventions and do not expose member variables as public or start property names with lower case. 请遵循C#约定,不要将成员变量公开为public或以小写形式启动属性名称。 In order to make your conventional objects deserializable, you could use the System.Runtime.Serialization DataContract and DataMember attributes. 为了使您的常规对象可反序列化,您可以使用System.Runtime.Serialization DataContract和DataMember属性。 DataContract indicates that an object of this type is serializable and DataMember is used to specify a property's serialization name. DataContract指示此类型的对象是可序列化的,DataMember用于指定属性的序列化名称。

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. 序列化的属性名称也应该更短,以获得更好的性能。 An example: 一个例子:

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

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

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