Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
503 views
in Technique[技术] by (71.8m points)

c# - 将JSON反序列化为字典 <string, List<string> >(Deserialize JSON to Dictionary<string, List<string>>)

I am very new to JSON, so I may have missed something.

(我是JSON的新手,所以我可能错过了一些东西。)

But here's what I am attempting.

(但是,这就是我正在尝试的。)

I want to Deserialize the following type of JSON

(我想反序列化以下类型的JSON)

{
  "Size": 
  {
    "Creature": 
    {
      "Dragon": 
      [
        "Huge",
        "Colossal",
        "Mountain-sized"
      ],

      "Wolf": 
      [
        "Pup",
        "Fully grown",
        "Giant"
      ]
    },

    "Building": 
    [
      "Small",
      "Medium",
      "Large"
    ]
  }
}

The core function of the JSON is intended so that I am not sure how nested it may become over time.

(JSON的核心功能旨在防止我随时间推移嵌套的方式。)

With creature having subtypes depending on what kind of creature it is, and same for building and so on.

(对于具有根据其生物种类而定的亚型的生物,对于建筑物等也具有相同的亚型。)

I've attempted with this code

(我尝试使用此代码)

using StreamReader r = new StreamReader("Storage.json");
string json = r.ReadToEnd();
CoreStorageDict = JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(json);

I would like to Deserialize it into a dictionary as directly as possible, but I've yet to find a good way to do it and I think I am missing something fundamental about the whole system.

(我想尽可能直接将其反序列化为字典,但是我还没有找到一种好的方法来完成它,而且我想我缺少整个系统的基本知识。)

Is the JSON wrong or is my code wrong?

(JSON错误还是我的代码错误?)

Or both perhaps?

(还是两者都有?)

  ask by Tor Nordmark translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you define the following classes:

(如果定义以下类:)

public class Creature
{
    public IList<string> Dragon { get; set; }
    public IList<string> Wolf { get; set; }
}

public class Size
{
    public Creature Creature { get; set; }
    public IList<string> Building { get; set; }
}

public class Example
{
    public Size Size { get; set; }
}

and then try to deserialize your json you will make it.

(然后尝试反序列化您的json即可。)

You can alter the name of classes as you want.

(您可以根据需要更改类的名称。)

For the names above you have just to do this:

(对于上述名称,您只需执行以下操作:)

var result = JsonConvert.DeserializeObject<Example>(json);

What is the problem with the approach you followed ?

(您采用的方法有什么问题?)

The problem is that you have nested types.

(问题是您有嵌套的类型。)

So you have to declare each and any type properly in order the deserialization work.

(因此,您必须正确声明每个类型,才能进行反序列化工作。)

How you can find which classes are needed to be declared ?

(如何找到需要声明的类?)

There are may tools out there that do this job.

(可能有一些工具可以完成这项工作。)

The one I used is the following one JSON Utils .

(我使用的是以下一个JSON Utils 。)

Provided that you have a valid json, these tools can auto generate the required classes.

(只要您具有有效的json,这些工具就可以自动生成所需的类。)

If I am correct, also Visual Studio, provides you with such a functionality.

(如果我是正确的话,Visual Studio也会为您提供这种功能。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...