C# 缩短JSON阅读类[已解决]

C# 缩短JSON阅读类[已解决],c#,json,optimization,json.net,C#,Json,Optimization,Json.net,底部的解决方案。 我有一个简单的JSON读取类,它应该从JSON对象中获取值并将其放入c#变量中。现在它使用8个if语句,但我想知道是否可以以更平滑的方式完成 当前代码: public Game Read(string filePath) { string fileName = "./Levels/TempleOfDoom.json"; JObject json = JObject.Parse(File.ReadAllText(fil

底部的解决方案。

我有一个简单的JSON读取类,它应该从JSON对象中获取值并将其放入c#变量中。现在它使用8个if语句,但我想知道是否可以以更平滑的方式完成

当前代码:

public Game Read(string filePath)
    {
        string fileName = "./Levels/TempleOfDoom.json";

        JObject json = JObject.Parse(File.ReadAllText(fileName));

        Game game = new Game();

        foreach (JObject jconnection in json["rooms"])
        {
            Room room = new Room();
            foreach (JProperty jProperty in jconnection.Children().OfType<JProperty>())
            {
                if (jProperty.Name == "id")
                    room.id = jProperty.Value.ToObject<int>();

                if (jProperty.Name == "width")
                    room.width = jProperty.Value.ToObject<int>();

                if (jProperty.Name == "height")
                    room.height = jProperty.Value.ToObject<int>();

                foreach (JObject jconnection2 in jconnection["items"])
                {
                    Item item = new Item();
                    foreach (JProperty jProperty2 in jconnection.Children().OfType<JProperty>())
                    {
                        if (jProperty.Name == "type")
                            item.type = jProperty2.Value.ToObject<string>();

                        if (jProperty.Name == "x")
                            item.x = jProperty2.Value.ToObject<int>();

                        if (jProperty.Name == "y")
                            item.y = jProperty2.Value.ToObject<int>();

                        if (jProperty.Name == "damage")
                            item.damage = jProperty2.Value.ToObject<int>();

                        if (jProperty.Name == "color")
                            item.color = jProperty2.Value.ToObject<string>();
                    }
                }
            }
            game.Rooms.Add(room);
        }

        return game;
    }
}

如您所见,每个房间都有一个ID、宽度和高度(类型可以忽略),有些房间有项目,它们都有类型、x和y坐标,有些房间有颜色或损坏编号。有没有更好的方法将所有这些值放入C#类?一个游戏有一个房间列表,每个房间都可能有一个物品列表

编辑:谢谢大家!这些代码行完全符合我的要求(以及每个游戏/玩家/物品等的额外类)

public Game Read(string fileName)
    {
        JObject json = JObject.Parse(File.ReadAllText(fileName));
        return JsonConvert.DeserializeObject<Game>(json.ToString());
    }
公共游戏读取(字符串文件名)
{
JObject json=JObject.Parse(File.ReadAllText(文件名));
返回JsonConvert.DeserializeObject(json.ToString());
}

我的建议是使用一个名为的网页,它将为您的json创建模型类。有时它需要一些调整,然后就像调用

var json = JsonConverter.DeserializeObject<YourJsonClass>(File.ReadAllText(fileName)) 

您可以创建一个类,例如:

public class Item
{
    public string type { get; set; }
    public int damage { get; set; }
    public int x { get; set; }
    public int y { get; set; }
    public string color { get; set; }
}

public class Room
{
    public int id { get; set; }
    public string type { get; set; }
    public int width { get; set; }
    public int height { get; set; }
    public List<Item> items { get; set; }
}

public class MyClass
{
    public List<Room> rooms { get; set; }
}
公共类项目
{
公共字符串类型{get;set;}
公共整数损坏{get;set;}
公共整数x{get;set;}
公共整数y{get;set;}
公共字符串颜色{get;set;}
}
公共教室
{
公共int id{get;set;}
公共字符串类型{get;set;}
公共整数宽度{get;set;}
公共整数高度{get;set;}
公共列表项{get;set;}
}
公共类MyClass
{
公共列表室{get;set;}
}
然后将对象反序列化为:

MyClass myDeserializedClass = JsonConvert.DeserializeObject<MyClass>(json.ToString());
MyClass myDeserializedClass=JsonConvert.DeserializeObject(json.ToString());

然后您可以访问
MyClass
的所有内部,并使用
foreach

List
上进行迭代。更传统的方法是创建
房间
类型和
项目
类型,以及带有
列表房间{get;set;}的
游戏
property,如有必要,添加适当的属性,然后调用
JsonConverter.DeserializeObject
。它将为您排序。请参阅或其他Json.NET教程。@Jon Skeet似乎不错,另一个建议是使用System.Reflection和dynamicly按名称设置值。请告诉我是否需要我制作一个example@Alen.Toma: 为什么要重新发明轮子?
JsonConvert
已经在使用反射……是的,我知道为什么我说@Jon Skeet的建议似乎很好,如果他愿意,我只是想给出另一种方法。数据模型是什么样子的?OP已经在使用Newtonsoft.Json了-问题被标记为
Json.net
e OP的现有代码使用了
JProperty
等。我建议删除你答案的第一部分。谢谢@JonSkeet,我按照顺序编辑了答案。我将保留JSON2Charp链接,因为它可能会帮助他,而且OP没有共享这些类,我不确定他们是否在使用json模型
public class Item
{
    public string type { get; set; }
    public int damage { get; set; }
    public int x { get; set; }
    public int y { get; set; }
    public string color { get; set; }
}

public class Room
{
    public int id { get; set; }
    public string type { get; set; }
    public int width { get; set; }
    public int height { get; set; }
    public List<Item> items { get; set; }
}

public class MyClass
{
    public List<Room> rooms { get; set; }
}
MyClass myDeserializedClass = JsonConvert.DeserializeObject<MyClass>(json.ToString());