如何为json结果删除$?

时间:2018-09-25 20:47:09

标签: c# asp.net json google-sheets google-sheets-api

我正在尝试从一个简单的Google工作表中读取一个单元格值

https://docs.google.com/spreadsheets/d/1opP1t_E9xfuLXBkhuyzo5j9k_xBNDx0XKb31JwLP1MM/edit?usp=sharing

然后我发布了它,并获得了一个返回 JSON API 链接,请检查以下内容 https://spreadsheets.google.com/feeds/list/1opP1t_E9xfuLXBkhuyzo5j9k_xBNDx0XKb31JwLP1MM/1/public/values?alt=json

当我尝试使用http://json2csharp.com/ JSON 生成 C#类时,我得到

  

invalid_name和$(不适用于csharp编译器)

public class Id
{
    public string __invalid_name__$t { get; set; }
}

public class Updated
{
    public DateTime __invalid_name__$t { get; set; }
}

public class Category
{
    public string scheme { get; set; }
    public string term { get; set; }
}

public class Title
{
    public string type { get; set; }
    public string __invalid_name__$t { get; set; }
}

public class Link
{
    public string rel { get; set; }
    public string type { get; set; }
    public string href { get; set; }
}

public class Name
{
    public string __invalid_name__$t { get; set; }
}

public class Email
{
    public string __invalid_name__$t { get; set; }
}

public class Author
{
    public Name name { get; set; }
    public Email email { get; set; }
}

public class OpenSearchTotalResults
{
    public string __invalid_name__$t { get; set; }
}

public class OpenSearchStartIndex
{
    public string __invalid_name__$t { get; set; }
}

public class Id2
{
    public string __invalid_name__$t { get; set; }
}

public class Updated2
{
    public DateTime __invalid_name__$t { get; set; }
}

public class Category2
{
    public string scheme { get; set; }
    public string term { get; set; }
}

public class Title2
{
    public string type { get; set; }
    public string __invalid_name__$t { get; set; }
}

public class Content
{
    public string type { get; set; }
    public string __invalid_name__$t { get; set; }
}

public class Link2
{
    public string rel { get; set; }
    public string type { get; set; }
    public string href { get; set; }
}

public class GsxName
{
    public string __invalid_name__$t { get; set; }
}

public class GsxPhonenumber
{
    public string __invalid_name__$t { get; set; }
}

public class Entry
{
    public Id2 id { get; set; }
    public Updated2 updated { get; set; }
    public List<Category2> category { get; set; }
    public Title2 title { get; set; }
    public Content content { get; set; }
    public List<Link2> link { get; set; }
    public GsxName __invalid_name__gsx$name { get; set; }
    public GsxPhonenumber __invalid_name__gsx$phonenumber { get; set; }
}

public class Feed
{
    public string xmlns { get; set; }
    public string __invalid_name__xmlns$openSearch { get; set; }
    public string __invalid_name__xmlns$gsx { get; set; }
    public Id id { get; set; }
    public Updated updated { get; set; }
    public List<Category> category { get; set; }
    public Title title { get; set; }
    public List<Link> link { get; set; }
    public List<Author> author { get; set; }
    public OpenSearchTotalResults __invalid_name__openSearch$totalResults { get; set; }
    public OpenSearchStartIndex __invalid_name__openSearch$startIndex { get; set; }
    public List<Entry> entry { get; set; }
}

public class RootObject
{
    public string version { get; set; }
    public string encoding { get; set; }
    public Feed feed { get; set; }
}

我想序列化和反序列化此类,我也想删除$,我该怎么做?

1 个答案:

答案 0 :(得分:1)

很显然,json2csharp将每个json对象转换为一个类,并将键名按字面意义转换为变量名。因此,无论何时找到以$开头的键名,都无法使用此字符创建c#变量,并且在变量名之前添加_invalid_name_。这里没有错。

您应该告诉您为什么要从变量名中删除此invalid_name短语?您想序列化和反序列化此类吗?如果是这样,您可以使用NewtonSoft Json库,并使用$符号定义这些字段,如下所示:

[JsonProperty(PropertyName = "$t")]
public string t { get; set; }

这将允许您序列化/反序列化json文档

与gsx $ name相同:

[JsonProperty(PropertyName = "gsx$name")]
public string gsxname { get; set; }