Ich verwende diese Klasse:C# konvertieren Klasse JSON-Datei
class message
{
public content Content { get; set; }
public from From { get; set; }
public personalizations Personalizations { get; set; }
}
public class content
{
public string type = "text/html";
public string value = "html";
}
public class from
{
public string email = "[email protected]";
public string name = "example";
}
public class personalizations
{
public List<to> tos { get; set; }
}
public class to
{
public string subject { get; set; }
public string email { get; set; }
}
ich bin Serialisierung der Klasse Nachricht an:
var msg = new message() { Content = new content() { type = "text/html", value = "html" },
From = new from() { email = "[email protected]", name = "example" },
Personalizations = new personalizations() { tos = new List<to>() { new to(), new to() } } };
var data = JsonConvert.SerializeObject(msg);
Ich versuche, eine Reihe zu bekommen von alle Eltern die json Ausgabeformat ist
{
"Content": {
"type": "text/html",
"value": "html"
},
"From": {
"email": "[email protected]",
"name": "example"
},
"Personalizations":
{
"tos": [
{
"subject": null,
"email": null
},
{
"subject": null,
"email": null
}
]
}
}
aber ich tun, um dieses Format wollen stattdessen:
{
"content": [
{
"type": "text/html",
"value": "Html"
}
],
"from": {
"email": "",
"name": ""
},
"personalizations": [
{
"subject": "",
"to": [ { "email": "" }]
},
{
"subject": "",
"to": [{ "email": "" }]
},
{
"subject": "",
"to": [{ "email": "" }]
}
]
}
wie kann ich es schaffen, das format auf das letzte zu ändern?
Dank im Voraus
EDIT:
Ich mag Beispiel nicht die Werte um das Format ändern:
im letzten json Beispiel i mehrere json der Personalisierung weicht ein Objekt habe halten, aber in die erste ich habe nur ein Objekt
'null = ""' .... – Gusman
Siehe http://stackoverflow.com/questions/23830206/json-convert-empty -string-statt-von-null – shurik
@shurik du hast Recht ... Offensichtlich weiß OP, wie man Klassen von JSON konstruiert, und wie man Objekt als einzelnes Elementfeld darstellt - wiedereröffnet und auf Klärung wartend auf, was sie Problem mit genau haben. –