有沒有辦法取一些C#類並將每個屬性轉換爲[JsonPropertyName,value]的字符串數組而不通過字符串創建JSON。我一直在嘗試使用newtonsoft.Json將對象序列化爲JSON,但我無法使屬性以與所需輸出相同的方式顯示。將類屬性轉換爲[JsonPropertyName,Value]的數組
public class UserCredentials
{
[JsonProperty("InternetGatewayDevice.WANDevice.1.WANConnectionDevice.1.WANPPPConnection.1.Username")]
public string Username;
[JsonProperty("InternetGatewayDevice.WANDevice.1.WANConnectionDevice.1.WANPPPConnection.1.Password")]
public string Password;
}
期望JSON輸出:
{"name":"setParameterValues",
"parameterValues": [
["InternetGatewayDevice.WANDevice.1.WANConnectionDevice.1.WANPPPConnection.1.Username", "[email protected]"],
["InternetGatewayDevice.WANDevice.1.WANConnectionDevice.1.WANPPPConnection.1.Password", "hello"]
]
}