【问题标题】:How to remove $ for json result?如何为 json 结果删除 $?
【发布时间】:2018-09-25 20:47:08
【问题描述】:

我正在尝试从简单的谷歌表格中读取单元格值

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

然后我发布了它,我得到一个返回 JSONAPI 链接,请检查以下内容 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; }
}

我想序列化和反序列化这个类,我也想删除$,我需要做什么?

【问题讨论】:

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


    【解决方案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; }
    

    【讨论】:

    • 它工作正常,我删除了 $ 并使用了 JsonProperty。很好的答案谢谢
    猜你喜欢
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 2014-11-03
    • 2018-04-14
    • 2020-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多