我正在使用Newtonsoft.dll,这就是场景,
List<int> listNumbers = new List<int>() { 1, 2, 3, 4, 5};
var result = JsonConvert.SerializeObject(listNumbers);
结果是,
[1,2,3,4,5]
但我想要以下类型的结果,
[{"cardvalue":1},{"cardvalue":2},{"cardvalue":3},{"cardvalue":4},{"cardvalue":5}]
我该怎么做?
答案 0 :(得分:1)
尝试 -
var result = listNumbers.Select(a => new
{
cardvalue = a
});
var jsonResult = JsonConvert.SerializeObject(result);
或直接序列化结果 -
var result = JsonConvert.SerializeObject(listNumbers.Select(a => new
{
cardvalue = a
}));