次の変数を持つオブジェクトクラスがあります。
class empleado
{
public string nombre = "Nicolas";
public string apellido = "Flores";
public int edad= 22;
}
そして、次のコード行でJsonを生成できます。
string jsonResult = JsonConvert.SerializeObject(empleado);
しかし、生成するjsonに、オブジェクトとは別に他のデータを追加したいのですが、どうすればよいですか?
別のアプローチとして、このような別のモデルを保持できます。したがって、empleadoオブジェクトとその他の必要なプロパティを割り当ててから、Jsonオブジェクトに変換できます。
class MyJsonObjectModel
{
public string otherProperty1 = "My string 1";
public string otherProperty2 = "My string 2";
public int otherProperty3 = "1";
public empleado empleadoModel;
}
string jsonResult = JsonConvert.SerializeObject(MyJsonObjectModel);
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加