我正在尝试将对象序列化为 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"
}
}
我该怎么做 ?
江户川乱折腾
qq_笑_17
扬帆大鱼
随时随地看视频慕课网APP
相关分类