2014-04-17 97 views
2

在C#中使用Json.NET時,序列化類時出現問題,其中需要定製屬性名稱。序列化時在Json.NET中使用特定的屬性名稱

這是我現在有:

{ 
    "baseName": { 
    "subName1": { 
     "name": null, 
     "longName": null, 
     "asd1": null, 
     "asd2": null 
    }, 
    "subName2": [ 
     { 
     "id": "ID_NUMBER", 
     "info": { 
      "someInfo1": "asd", 
      "someInfo2": "asd2" 
     } 
     } 
    ] 
    } 
} 

這就是我想要

{ 
    "baseName": { 
    "subName1": { 
     "name": null, 
     "longName": null, 
     "asd1": null, 
     "asd2": null 
    }, 
    "subName2": [ 
     { 
     "ID_NUMBER": { 
      "someInfo1": "asd", 
      "someInfo2": "asd2" 
     } 
     } 
    ] 
    } 
} 

也就是說,是不是具有id和價值ID_NUMBER的關鍵,我想ID_NUMBER是包含密鑰someInfo1someInfo2的對象的關鍵。

class JSONTestClass 
{ 
    public JSONBaseTestClass baseName; 
} 

class JSONBaseTestClass 
{ 
    public JSONSubTestClass1 subName1; 
    public List<JSONSubTestClass2> subName2; 
} 

class JSONSubTestClass1 
{ 
    public string name; 
    public string longName; 
    public string asd1; 
    public string asd2; 
} 

class JSONSubTestClass2 
{ 
    public string id; 
    public JSONInfoTestClass info; 
} 

class JSONInfoTestClass 
{ 
    public string someInfo1; 
    public string someInfo2; 
} 

這:

使用這些類(遺憾的壞話)中最嚴重的JSON代碼

private void MakeJSON() 
{ 
    // This value can be changed at runtime 
    string specificId = "ID_NUMBER"; 

    JSONInfoTestClass jitc = new JSONInfoTestClass(); 
    jitc.someInfo1 = "asd"; 
    jitc.someInfo2 = "asd2";    

    JSONTestClass jtc = new JSONTestClass(); 

    JSONBaseTestClass jbtc = new JSONBaseTestClass(); 

    JSONSubTestClass1 jstc1 = new JSONSubTestClass1(); 

    JSONSubTestClass2 jstc2 = new JSONSubTestClass2(); 
    jstc2.id = specificId; 
    jstc2.info = jitc; 

    List<JSONSubTestClass2> list = new List<JSONSubTestClass2>(); 
    list.Add(jstc2); 

    jbtc.subName1 = jstc1; 
    jbtc.subName2 = list; 

    jtc.baseName = jbtc; 

    // Convert to JSON 
    string json = JsonConvert.SerializeObject(jtc, Formatting.Indented); 
    tbxJSONOutput.Text = json; 
} 

哪些是需要改變這樣我就可以得到相應的一個JSON輸出到上面提到的第二個JSON響應?

回答

1

你可以爲你的JSONSubTestClass2類這樣創建自定義JsonConverter得到你想要的輸出:

class JSONSubTestClass2Converter : JsonConverter 
{ 
    public override bool CanConvert(Type objectType) 
    { 
     return (objectType == typeof(JSONSubTestClass2)); 
    } 

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 
    { 
     JSONSubTestClass2 jstc2 = (JSONSubTestClass2)value; 
     JObject jo = new JObject(); 
     jo.Add(jstc2.id, JObject.FromObject(jstc2.info)); 
     jo.WriteTo(writer); 
    } 

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) 
    { 
     throw new NotImplementedException(); 
    } 
} 

然後,序列化你這樣的類:

JsonSerializerSettings settings = new JsonSerializerSettings(); 
settings.Converters.Add(new JSONSubTestClass2Converter()); 
settings.Formatting = Formatting.Indented; 

// Convert to JSON 
string json = JsonConvert.SerializeObject(jtc, settings); 
+0

非常感謝!精彩的工作:) – eightx2

+0

很高興我能幫上忙。 –

相關問題