我有以下类(有点简化版本):
public class DemoList<K, V>
{
private List<SomeItem<K, V>> _listItems;
public DemoList()
{
_listItems = new List<SomeItem<K, V>>();
}
public Int32 Count
{
get { return _listItems.Count; }
}
public void Add(SomeItem<K, V> item)
{
_listItems.Add(item);
}
public SomeItem<K, V> this[int index]
{
get { return _listItems[index]; }
}
}
someitem代码:
public class SomeItem<K, V>
{
private K _key;
private V _value;
public SomeItem(K key, V value)
{
_key = key;
_value = value;
}
public K Key
{
get { return _key; }
set { _key = value; }
}
public V Value
{
get { return _value; }
set { _value = value; }
}
}
当我向演示列表添加一些值时:
DemoList<String, String> dl = new DemoList<string, string>();
dl.Add(new SomeItem<string, string>("bla1", "diebla1"));
dl.Add(new SomeItem<string, string>("bla2", "diebla2"));
dl.Add(new SomeItem<string, string>("bla3", "diebla3"));
dl.Add(new SomeItem<string, string>("bla4", "diebla4"));
现在我想使用newtonsoft.json将其转换为JSON ...像这样:
JsonSerializerSettings serializerSettings = new JsonSerializerSettings();
serializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
var jsonContent = JsonConvert.SerializeObject(dl, serializerSettings);
这将导致以下JSON:
{"Count":4}
我希望的json就像:
{
"items": [
{ "bla1": "diabla1"},
{"bla2": "diabla2"},
{"bla3": "diabla3"},
{"bla4": "diabla4"}
]
}
有什么想法?
获取json.net序列化项目列表,您可以将项目公开为公共属性(正如其他人建议的那样),或用 [JsonProperty]
属性标记列表,:
[JsonProperty("items")]
private List<SomeItem<K, V>> _listItems;
如果要从输出中抑制Count
,请用[JsonIgnore]
标记该属性:
[JsonIgnore]
public Int32 Count
{
get { return _listItems.Count; }
}
随着上述更改,我们将获得以下JSON,它更接近,但仍然不太想要:
{
"items": [
{ "key": "bla1", "value": "diebla1" },
{ "key": "bla2", "value": "diebla2" },
{ "key": "bla3", "value": "diebla3" },
{ "key": "bla4", "value": "diebla4" }
]
}
要将这些项目纳入所需的格式,您需要一个自定义的JsonConverter
类,例如:
class SomeItemConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType.IsGenericType &&
objectType.GetGenericTypeDefinition() == typeof(SomeItem<,>);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
JToken token = JToken.FromObject(value);
JObject obj = new JObject();
obj.Add(token["Key"].ToString(), token["Value"]);
obj.WriteTo(writer);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JToken token = JToken.Load(reader);
JProperty prop = token.Children<JProperty>().First();
JObject obj = new JObject();
obj.Add("Key", prop.Name);
obj.Add("Value", prop.Value);
return obj.ToObject(objectType);
}
}
将转换器添加到您的jsonserializersettings,并像以前一样序列化列表:
JsonSerializerSettings serializerSettings = new JsonSerializerSettings();
serializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
serializerSettings.Converters.Add(new SomeItemConverter());
var jsonContent = JsonConvert.SerializeObject(dl, serializerSettings);
Console.WriteLine(jsonContent);
现在我们有这个JSON:
{
"items": [
{ "bla1":"diebla1" },
{ "bla2":"diebla2" },
{ "bla3":"diebla3" },
{ "bla4":"diebla4" }
]
}
创建返回_listItems的拆除列表中的任何方法。然后在JSON序列化中调用该Getter方法将提供所需的JSON。