1

鉴于 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# 建议指出的其他方法)。虽然,我认为我可能无法为这些方法中的某些方法找到正确的 using 指令,但是 WebMatrix 中的细微差异再次让我无法找到该信息,如果它确实存在的话。

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

我在 WebMatrix 中注意到的一些与 json 相关的非静态方法包括“DynamicJsonObject”和“DynamicJsonArray”,但是,无论是[使用 Server.MapPath 与否] 指向文件的路径还是只是将完整的 json 文件存储为单个字符串变量,我尝试使用这些方法都没有成功。

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

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

4

1 回答 1

1

您可以使用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>
于 2013-02-13T21:56:39.183 回答