我有以下代码
class MyJSONSerializableClass
{
[JsonProperty("index")]
public int Index { get; set; };
[JsonIgnore]
public long Id { get; set; };
}
var collection = new List<MyJSONSerializableClass>()
{
new MyJSONSerializableClass()
{
Index = 10,
Id = 1000
}
};
string jsonOutput = JsonConvert.SerializeObject(collection);
jsonOutput将是
[{ index: 10 }]
我希望jsonOutput成为
[ 10 ]
我是否可以将任何类属性应用于MyJSONSerializableClass,它会告诉JSON.NET只发出属性Index而不是整个对象?
的内容
[JsonOutput(f => f.Index)]
class MyJSONSerializableClass
{
...
}
由于
答案 0 :(得分:1)
不确定属性,但也可以尝试
JsonConvert.SerializeObject(collection.Select(o => o.Index)).
根据documentation,所有IEnumerable都是数组。