从json对象数组中获取WebMatrix C#对象数组

本文关键字:对象 数组 WebMatrix 获取 json | 更新日期: 2023-09-27 17:59:03

假设json文件(json对象)看起来像这样(一个对象数组):

{
    "job": [
        {
            "title": "Mechanic",
            "department": "Central Repair Department",
            "summary": "Repair and perform preventative maintenance to vehicles and equipment.",
            "qualifications": "High school diploma or equivalent, valid Oklahoma driver's license, general knowledge of operating principals of gasoline and diesel engines, and experience with mechanical repair of vehicles. Basic knowledge of hydraulics and equipment is also desirable.",
            "incentive": "",
            "wage": ""
        },
        {
            "title": "Police Patrol Officer",
            "department": "Police Department",
            "summary": "Repair and perform preventative maintenance to vehicles and equipment.",
            "qualifications": "High school diploma or equivalent, valid Oklahoma driver's license, general knowledge of operating principals of gasoline and diesel engines, and experience with mechanical repair of vehicles. Basic knowledge of hydraulics and equipment is also desirable.",
            "incentive": "",
            "wage": ""
        }
    ]
}

如何获取使用这些值绘制的对象数组?

只有当你熟悉如何在WebMatrix的C#中做到这一点时,请回答(这与常规C#不同,也就是说,没有JsonReader方法,没有JObject方法,也没有其他C#建议指出的其他方法)。尽管,我认为我不可能为其中一些方法找到正确的使用指令,但WebMatrix中的细微差异再次让我无法找到这些信息,如果这些信息确实存在的话。

此外,我不知道什么是LINQ,也不知道如何使用它,我也不在乎,除非这绝对是最好/最简单的方法。

我在WebMatrix中注意到的一些与json有关的非静态方法包括DynamicJsonObject和DynamicJson Array,然而,无论是否使用Server.MapPath作为文件的路径,或者只是将完整的json文件存储为单个字符串变量,我尝试使用这些方法都没有成功。

没有简单的方法可以从json对象数组创建一个C#对象数组吗?

谢谢你的帮助!如果你需要任何进一步的信息,请告诉我。

从json对象数组中获取WebMatrix C#对象数组

您可以使用Json Helper将Json解码为动态对象。假设您在上面发布的JSON位于一个名为JsonFile.txt的文件中,则以下操作将完成:

@{
    var json = File.ReadAllText(Server.MapPath("~/JsonFile.txt"));
    var data = Json.Decode(json);
}
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        @foreach(var j in data.job){
            <h3>@j.title</h3>
            <div>@j.summary</div>
        }
    </body>
</html>