2016-08-15 82 views
0

有沒有辦法取一些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"] 
    ] 
} 

回答

0

可以將對象轉換爲屬性的數組,然後將其序列化。 事情是這樣的:

public class Test 
{ 
    [JsonProperty("TestJsonString")] 
    public string TestFieldString { get; set; } 

    [JsonProperty("TestJsonInt")] 
    public int TestFieldInt { get; set; } 

    public int TestFieldInt2 { get; set; } 
} 
class Program 
{ 
    static void Main(string[] args) 
    { 
     var data = new Test() {TestFieldInt = 1, TestFieldString = "11"}; 
     var testArray = data.GetType().GetProperties() 
      .Where(x => x.GetCustomAttributes(true).Any(attr => attr is JsonPropertyAttribute)) 
      .Select(x => new [] 
      { 
       (x.GetCustomAttributes(typeof(JsonPropertyAttribute), true).Single() as JsonPropertyAttribute).PropertyName, 
       x.GetGetMethod().Invoke(data, null) == null ? "" : x.GetGetMethod().Invoke(data, null).ToString() 
      }); 
     var serialized = JsonConvert.SerializeObject(testArray); 
    } 
} 
0

您可以JSON屬性和價值,你的問題問,據我的瞭解,使用的Newtonsoft.Json像下面,

string x = JsonConvert.SerializeObject([YourObject],Newtonsoft.Json.Formatting.None, new JsonSerializerSettings { MetadataPropertyHandling = MetadataPropertyHandling.ReadAhead}); 
1

你可以創建自定義JSON序列化程序。有關示例,請查看這些鏈接(link1link2)。

這裏是你的代碼可能看起來怎麼樣(這只是草稿,你必須正確地使用反射):

private class PropertyNamesSerializer : JsonConverter 
    { 
     public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 
     { 
      writer.WriteStartObject(); 
      writer.WritePropertyName("name"); 
      writer.WriteValue("setParameterValues"); 
      writer.WritePropertyName("parameterValues"); 
      writer.WriteStartArray(); 
      writer.WriteStartObject(); 
      foreach (var property in value.GetType().GetProperties()) 
      { 
       var attribute = property.GetCustomAttribute<JsonPropertyAttribute>(); 
       writer.WritePropertyName(attribute.PropertyName); 
       writer.WriteValue(property.GetValue(value)); 
      } 
      writer.WriteEndObject(); 
      writer.WriteEndArray(); 
      writer.WriteEndObject(); 
     } 
    // other methods 

用法:

[JsonConverter(typeof(PropertyNamesSerializer))] 
    public class UserCredentials 
    { 
     [JsonProperty("InternetGatewayDevice.WANDevice.1.WANConnectionDevice.1.WANPPPConnection.1.Username")] 
     public string Username { get; set; } 

     [JsonProperty("InternetGatewayDevice.WANDevice.1.WANConnectionDevice.1.WANPPPConnection.1.Password")] 
     public string Password { get; set; } 
    } 

    private static void Main(string[] args) 
    { 

     Console.WriteLine(JsonConvert.SerializeObject(new UserCredentials() {Username = "test", Password = "something"})); 
    }