创建此json对象的等效类的最简单方法

本文关键字:最简单 方法 json 对象 创建 | 更新日期: 2023-09-27 18:09:01

我得到了这个json对象:

{
    foo: {
        someVar: 100,
        anotherVar: "foobar"
    },
    bar: {
        nextVar: "193",
        isThisAVar: true
    },
    foobar: "some text",
    foobool: false,
    barfoo: [
        {
            first: "text",
            second: 391,
            third: "DateTime string"
        },
        {
            first: "text",
            second: 391,
            third: "DateTime string"
        },
        {
            first: "text",
            second: 391,
            third: "DateTime string"
        },
    ]
}

我想在c#中创建一个与这个json对象等价的类,这样我就可以用JsonConvert.Deserialize<T>() (Json.Net)反序列化json对象。
但是我不想为foo, barbarfoo创建一个类/结构。
我也不能使用Json的动态反序列化。因为我的c#类包含一些帮助函数来操作数据。

那么,实现这一目标最简单/最快的方法是什么?

创建此json对象的等效类的最简单方法

您可以使用http://jsonutils.com来实现这一点。

您输入的JSON将生成以下类结构:

public class Foo
{
    public int someVar { get; set; }
    public string anotherVar { get; set; }
}
public class Bar
{
    public string nextVar { get; set; }
    public bool isThisAVar { get; set; }
}
public class Barfoo
{
    public string first { get; set; }
    public int second { get; set; }
    public string third { get; set; }
}
public class Example
{
    public Foo foo { get; set; }
    public Bar bar { get; set; }
    public string foobar { get; set; }
    public bool foobool { get; set; }
    public IList<Barfoo> barfoo { get; set; }
}