根据类数据生成json

时间:2018-09-12 08:54:27

标签: c# json asp.net-mvc

让我所有的课和我想要的Json消失。

如何基于这些类生成Json?

{
    "encabezado": {
        "iemp": "1",
        "inumoper": "11",
        "tdetalle": "Pago nómina Enero",
        "itdsop": "990",
        "inumsop": "35",
        "snumsop": "CIS-200635",
        "fsoport": "03/15/2015",
        "iclasifop": "2",
        "imoneda": "10",
        "iprocess": "2",
        "banulada": "F"
    },
    "datosprincipales": {
        "bcomandosimpresion": "F"
    },
    "listamovimientos": [{
            "icc": "",
            "icuenta": "31050510",
            "tdetalle": "",
            "mvrbase": "0.0000",
            "ibanco": "",
            "icheque": "",
            "init": "1053814757",
            "fsoport": "03/15/2015",
            "initcxx": "",
            "fpagocxx": "04/15/2015",
            "fvencimcxx": "06/15/2015",
            "mdebito": "0.0000",
            "mcredito": "25555838.6100",
            "iactivo": "",
            "inumsopcxx": "",
            "iflujoefec": "",
            "mvrotramoneda": "0.0000",
            "scomandos": "",
            "ilineamov": "1",
            "valor1": "0.0000",
            "valor2": "0.0000",
            "clase1": "",
            "clase2": "",
            "initvendedor": ""
        }, {
            "icc": "",
            "icuenta": "31050555",
            "tdetalle": "",
            "mvrbase": "0.0000",
            "ibanco": "",
            "icheque": "",
            "init": "10542887",
            "fsoport": "04/15/2015",

            "initcxx": "",
            "fpagocxx": "05/15/2015",
            "fvencimcxx": "06/15/2015",
            "mdebito": "0.0000",
            "mcredito": "25555838.6200",
            "iactivo": "",
            "inumsopcxx": "",
            "iflujoefec": "",
            "mvrotramoneda": "0.0000",
            "scomandos": "",
            "ilineamov": "2",
            "valor1": "0.0000",
            "valor2": "0.0000",
            "clase1": "",
            "clase2": "",
            "initvendedor": ""
        }, {
            "icc": "",
            "icuenta": "31050555",
            "tdetalle": "",
            "mvrbase": "0.0000",
            "ibanco": "",
            "icheque": "",
            "init": "30355123",
            "fsoport": "04/15/2015",
            "initcxx": "",
            "fpagocxx": "05/15/2015",
            "fvencimcxx": "06/15/2015",
            "mdebito": "0.0000",
            "mcredito": "25555838.6200",
            "iactivo": "",
            "inumsopcxx": "",
            "iflujoefec": "",
            "mvrotramoneda": "0.0000",
            "scomandos": "",
            "ilineamov": "3",
            "valor1": "0.0000",
            "valor2": "0.0000",
            "clase1": "",
            "clase2": "",
            "initvendedor": ""
        }, {
            "icc": "",
            "icuenta": "31050555",
            "tdetalle": "",
            "mvrbase": "0.0000",
            "ibanco": "",
            "icheque": "",
            "init": "14205632",
            "fsoport": "04/15/2015",
            "initcxx": "",
            "fpagocxx": "05/15/2015",
            "fvencimcxx": "06/15/2015",
            "mdebito": "0.0000",
            "mcredito": "25555838.6200",
            "iactivo": "",
            "inumsopcxx": "",

            "iflujoefec": "",
            "mvrotramoneda": "0.0000",
            "scomandos": "",
            "ilineamov": "4",
            "valor1": "0.0000",
            "valor2": "0.0000",
            "clase1": "",
            "clase2": "",
            "initvendedor": ""
        }
    ]
}

使用c#mvc传递并生成这种json

public class encabezado
{
    public int iemp { get; set; }
    public int inumoper { get; set; }
    public string tdetalle { get; set; }
    public int itdsop { get; set; }
    public int inumsop { get; set; }
    public string snumsop { get; set; }
    public string fsoport { get; set; }
    public int iclasifop { get; set; }
    public int imoneda { get; set; }
    public int iprocess { get; set; }
    public string banulada { get; set; }
}
public class datosprincipales
{
    public string bcomandosimpresion { get; set; }
}
public class listamovimientos
{
    public string icc { get; set; }
    public string icuenta { get; set; }
    public string tdetalle { get; set; }
    public decimal mvrbase { get; set; }
    public string ibanco { get; set; }
    public string icheque { get; set; }
    public string init { get; set; }
    public string fsoport { get; set; }
    public string initcxx { get; set; }
    public string fpagocxx { get; set; }

    public string fvencimcxx { get; set; }
    public decimal mdebito { get; set; }
    public decimal mcredito { get; set; }
    public string iactivo { get; set; }
    public string inumsopcxx { get; set; }
    public string iflujoefec { get; set; }
    public decimal mvrotramoneda { get; set; }
    public string scomandos { get; set; }
    public int ilineamov { get; set; }
    public decimal valor1 { get; set; }
    public decimal valor2 { get; set; }
    public string clase1 { get; set; }
    public string clase2 { get; set; }
    public string initvendedor { get; set; }
}

2 个答案:

答案 0 :(得分:1)

如果您使用的是Asp.Net MVC,则Visual Studio可能已经将NewtonSoft包含到您的应用程序中,但如果不是简单地将其作为Nuget软件包安装。

然后您可以执行以下操作:

public class ObjectToSerialize
{
    public encabezado encabezado { get; set; }
    public datosprincipales datosprincipales { get; set; }
    public listamovimientos listamovimientos { get; set; }
}

string json = JsonConvert.SerializeObject(new ObjectToSerialize());

答案 1 :(得分:0)

也许这是您的Model类。获取填充到其中的数据。然后,您可以使用NewtonsoftJson。您可以从Nuget商店购买。然后要搜索这些(我的意思是将其转换为Json格式),可以使用Library的SeralizeObject方法。