c# - 缩短 JSON 阅读类 [已解决]
问题描述
底部的解决方案。
我有一个简单的 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;
}
JSON对象的相关部分:
{
"rooms": [
{
"id": 1,
"type": "room",
"width": 5,
"height": 5
},
{
"id": 2,
"type": "room",
"width": 3,
"height": 3
},
{
"id": 3,
"type": "room",
"width": 5,
"height": 5,
"items": [
{
"type": "disappearing boobietrap",
"damage": 1,
"x": 2,
"y": 1
},
{
"type": "sankara stone",
"x": 2,
"y": 2
}
]
},
{
"id": 4,
"type": "room",
"width": 11,
"height": 7,
"items": [
{
"type": "key",
"color": "green",
"x": 1,
"y": 1
},
{
"type": "sankara stone",
"x": 5,
"y": 3
},
{
"type": "boobietrap",
"damage": 1,
"x": 4,
"y": 2
},
{
"type": "boobietrap",
"damage": 1,
"x": 5,
"y": 2
},
{
"type": "boobietrap",
"damage": 1,
"x": 6,
"y": 2
},
{
"type": "boobietrap",
"damage": 1,
"x": 4,
"y": 4
},
{
"type": "boobietrap",
"damage": 1,
"x": 5,
"y": 4
},
{
"type": "boobietrap",
"damage": 1,
"x": 6,
"y": 4
}
]
},
{
"id": 5,
"type": "room",
"width": 5,
"height": 5,
"items": [
{
"type": "key",
"color": "red",
"x": 2,
"y": 3
},
{
"type": "sankara stone",
"x": 2,
"y": 2
}
]
},
{
"id": 6,
"type": "room",
"width": 3,
"height": 3,
"items": [
{
"type": "sankara stone",
"x": 1,
"y": 1
}
]
},
{
"id": 7,
"type": "room",
"width": 5,
"height": 3,
"items": [
{
"type": "pressure plate",
"x": 2,
"y": 1
}
]
},
{
"id": 8,
"type": "room",
"width": 3,
"height": 3
},
{
"id": 9,
"type": "room",
"width": 5,
"height": 5,
"items": [
{
"type": "sankara stone",
"x": 2,
"y": 2
},
{
"type": "boobietrap",
"damage": 1,
"x": 1,
"y": 3
},
{
"type": "boobietrap",
"damage": 1,
"x": 2,
"y": 3
},
{
"type": "boobietrap",
"damage": 1,
"x": 3,
"y": 3
}
]
}
],
}
如您所见,每个房间都有一个ID、宽度和高度(类型可以忽略),有些房间有物品,它们都有类型、x 和y 坐标,有些还有颜色或损坏编号。有没有更好的方法将所有这些值放入 C# 类中?一个游戏有一个房间列表,每个房间都可能有一个项目列表。
编辑:谢谢大家!这些代码行完全符合我的要求(以及每个对象游戏/玩家/项目等的一些额外类)。
public Game Read(string fileName)
{
JObject json = JObject.Parse(File.ReadAllText(fileName));
return JsonConvert.DeserializeObject<Game>(json.ToString());
}
解决方案
我的建议是使用一个名为https://json2csharp.com/的网页,它将为您的 json 创建模型类。有时它需要一点点调整,然后就像调用一样简单
var json = JsonConverter.DeserializeObject<YourJsonClass>(File.ReadAllText(fileName))
而且您将有一个代表您的 json 文件的类,如果您仍然需要将此信息移动到您的自定义类中,则根本不需要 if 只需执行类似的操作
foreach (var jsonRoom in json.Rooms)
{
room.id = jsonRoom.id;
//and so on
}
推荐阅读
- google-apps-script - Google 表格 On Edit 触发器需要一些调整
- javascript - 使用 CryptoJS 解密 AES 密文
- python - 使用 python 线程为 Maya 创建 GIF
- firebase - Firebase 存储项目级别权限
- kubernetes - Istioctl 安装错误无法识别“STDIN”:版本“networking.istio.io/v1alpha3”中的种类不匹配
- r - r raster 如何将日期信息保存在 z 变量中
- javascript - 手动运行一个 husky git 钩子(不使用 git 命令触发它)
- javascript - 用于网页前端的 Java Scripting
- java - 将 Highcharts 图表添加到使用 Java API 生成的 JasperReport 模板
- angular - 在 Angular 8 中制作响应式侧导航