相关疑难解决方法(0)

在使用Json.net进行序列化时如何更改属性名称?

我在C#DataSet对象中有一些数据.我现在可以使用像这样的Json.net转换器来序列化它

DataSet data = new DataSet();
// do some work here to populate 'data'
string output = JsonConvert.SerializeObject(data);
Run Code Online (Sandbox Code Playgroud)

但是,这会data在打印到.json文件时使用属性名称.我想将属性名称更改为不同的名称(例如,将'foo'更改为'bar').

Json.net文档中,在"序列化和反序列化JSON"→"序列化属性"下,它表示"JsonPropertyAttribute ...允许自定义名称".但没有例子.有谁知道如何使用JsonPropertyAttribute将属性名称更改为其他名称?

(直接链接到文档)

Json.net的文档似乎很少.如果你有一个很好的例子,我会尝试将它添加到官方文档中.谢谢!

c# serialization json.net

415
推荐指数
3
解决办法
28万
查看次数

使用C#Object创建JSON

我正在尝试创建以下JSON数据:

{
'chart.labels': ['Bob','Lucy','Gary','Hoolio'],
'chart.tooltips': ['Bob did well',
               'Lucy had her best result',
               'Gary - not so good',
               'Hoolio had a good start'
              ]
}
Run Code Online (Sandbox Code Playgroud)

我正在使用C#并尝试创建一个对象以便执行此操作......类似于:

public class chart{
 public string[] chart.labels {get;set;}
 public string[] chart.tooltips {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

但显然我不能拥有包含空格的属性.

我该怎么做呢?

更新:

使用JamieC的答案,以下作品完美无缺

public virtual ActionResult CompanyStatus()
    {
        var labelList = new List<string>() { "Bob", "Lucy", "Gary", "Hoolio" };
        var tooltipsList = new List<string>() { "Bob did well", "Lucy had her best result", "Gary - not so good", "Hoolio had a …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc json

9
推荐指数
1
解决办法
6558
查看次数

标签 统计

c# ×2

asp.net-mvc ×1

json ×1

json.net ×1

serialization ×1