命名具有可变内容的对象以在 json 文件中序列化

我正在尝试将对象序列化为 JSON 文件,但问题是我想为 JSON 对象提供一个可变内容名称。


我以为我可以做这样的事情:


string codfis = "Example1";


var jsonCF = new {

    codfis = new { // codfis is the name of the variable as you can see

        Cognome = vcgm,

        Nome = vnm,

        Sesso = ss,

        LuogoDiNascita = ldn,

        Provincia = pr,

        DataDiNascita = ddn

    }

};

using (StreamWriter file = File.CreateText("CodFisCalcolati.json")) {

    JsonSerializer serializer = new JsonSerializer();

    serializer.Serialize(file, jsonCF);

}

但显然这没有做我想要的:


{

    "codfis": { // it named the json object like this, and not "Example1" like above

        "Cognome": "Yoyo",

        "Nome": "OK!",

        "Sesso": "Nice",

        "LuogoDiNascita": "Good",

        "Provincia": "Perfect",

        "DataDiNascita": "Fine"

    }

}

我也试过这个:


string codfis = "Example1";


var jsonCF = new {

    [codfis] = new { // putting brackets on the variable

        Cognome = vcgm,

        Nome = vnm,

        Sesso = ss,

        LuogoDiNascita = ldn,

        Provincia = pr,

        DataDiNascita = ddn

    }

};

但它给了我一个语法错误。


所以我只想做这个...


{

    "Example1": {

        "Cognome": "Yoyo",

        "Nome": "OK!",

        "Sesso": "Nice",

        "LuogoDiNascita": "Good",

        "Provincia": "Perfect",

        "DataDiNascita": "Fine"

    }

}

我该怎么做 ?


江户川乱折腾
浏览 174回答 2
2回答

qq_笑_17

正如评论中提到的其他 OPS,创建字典并序列化它string codfis = "Example1";var codfisValue = new{ // codfis is the name of the variable as you can see&nbsp; &nbsp; Cognome = "vcgm",&nbsp; &nbsp; Nome = "vnm",&nbsp; &nbsp; Sesso = "ss",&nbsp; &nbsp; LuogoDiNascita = "ldn",&nbsp; &nbsp; Provincia = "pr",&nbsp; &nbsp; DataDiNascita = "ddn"};var jsonCF = new Dictionary<string, object>();jsonCF.Add(codfis, codfisValue);using (StreamWriter file = File.CreateText("CodFisCalcolati.json")){&nbsp; &nbsp; JsonSerializer serializer = new JsonSerializer();&nbsp; &nbsp; serializer.Serialize(file, jsonCF);}

扬帆大鱼

除了这是匿名对象这一事实之外,这实际上是正确序列化的。从不使用在 jsonCF 对象之外初始化的 codfis 对象。您实际上是在创建一个全新的对象来表示对象内部的属性。“解决方案”取决于您要对此序列化项目执行的操作。如果您想要的话,您只需要引用现有变量而不是创建一个新变量。或者,如果您只想将该属性的名称设为Example1,只需将其设置为如下所示:&nbsp; &nbsp; var jsonCF = new {&nbsp; &nbsp; Example1 = new { //Note the property name&nbsp; &nbsp; &nbsp; &nbsp; Cognome = vcgm,&nbsp; &nbsp; &nbsp; &nbsp; Nome = vnm,&nbsp; &nbsp; &nbsp; &nbsp; Sesso = ss,&nbsp; &nbsp; &nbsp; &nbsp; LuogoDiNascita = ldn,&nbsp; &nbsp; &nbsp; &nbsp; Provincia = pr,&nbsp; &nbsp; &nbsp; &nbsp; DataDiNascita = ddn&nbsp; &nbsp; }};或者,var codfis = new {&nbsp; &nbsp; &nbsp; &nbsp; Cognome = vcgm,&nbsp; &nbsp; &nbsp; &nbsp; Nome = vnm,&nbsp; &nbsp; &nbsp; &nbsp; Sesso = ss,&nbsp; &nbsp; &nbsp; &nbsp; LuogoDiNascita = ldn,&nbsp; &nbsp; &nbsp; &nbsp; Provincia = pr,&nbsp; &nbsp; &nbsp; &nbsp; DataDiNascita = ddn&nbsp; &nbsp; };var jsonConf = new {&nbsp; &nbsp; &nbsp;Example1 = codfis}如果您希望属性名称和值都不同,您可能会使用字典而不是那样做var codfisName = "Example1";var jsonConf = new Dictionary<string, object>{&nbsp; &nbsp; &nbsp;{codfisName, codfis}};
打开App,查看更多内容
随时随地看视频慕课网APP