C# 如何将json反序列化为具有@&引用;在属性名称的开头?

C# 如何将json反序列化为具有@&引用;在属性名称的开头?,c#,json.net,deserialization,C#,Json.net,Deserialization,我在反序列化json时遇到了问题,因为json在许多属性名称的开头有一个@符号。它包含大量json,因此我不知道从json中删除所有@是否安全,或者是否会丢失与属性相关的值中的一些有价值的信息 例如,我尝试使用[JsonProperty(@fontName”)],但没有成功(C#对象没有采用我在JSON中看到的值;而是null) 下面是我想要反序列化到的内容的示例: public class Column { [JsonProperty("@fontName")] public

我在反序列化json时遇到了问题,因为json在许多属性名称的开头有一个
@
符号。它包含大量json,因此我不知道从json中删除所有
@
是否安全,或者是否会丢失与属性相关的值中的一些有价值的信息

例如,我尝试使用
[JsonProperty(@fontName”)]
,但没有成功(C#对象没有采用我在JSON中看到的值;而是
null

下面是我想要反序列化到的内容的示例:

public class Column
{
    [JsonProperty("@fontName")]
    public string fontName { get; set; }
    public object text { get; set; }
}

public class Row
{
    public List<Column> column { get; set; }
    public string text { get; set; }
}

public class Page
{
    public string index { get; set; }
    public List<Row> row { get; set; }
    public string text { get; set; }
}

public class Document
{
    public List<Page> page { get; set; }
}

public class RootObject
{
    public Document document { get; set; }
}
公共类列
{
[JsonProperty(“@fontName”)]
公共字符串fontName{get;set;}
公共对象文本{get;set;}
}
公共类行
{
公共列表列{get;set;}
公共字符串文本{get;set;}
}
公共类页面
{
公共字符串索引{get;set;}
公共列表行{get;set;}
公共字符串文本{get;set;}
}
公共类文档
{
公共列表页{get;set;}
}
公共类根对象
{
公共文档文档{get;set;}
}

据我所知,您缺少了
页面
对象的一个属性
索引
属性

这样写比较好理解。另外,我们在单独的
文本
类中没有看到
fontName
fontSize
x
y
等的定义。出于某种原因,你把它写成了对象

public class Text
{
    [JsonProperty(PropertyName ="@fontName")]
    public string FontName {get; set;}

    [JsonProperty(PropertyName ="@fontSize")]
    public string FontSize {get; set;}

    [JsonProperty(PropertyName ="#text")]
    public string TextResult{get; set;}

    //other objects
}

public class Column
{
    public List<Text> text { get; set; }
}
公共类文本
{
[JsonProperty(PropertyName=“@fontName”)]
公共字符串FontName{get;set;}
[JsonProperty(PropertyName=“@fontSize”)]
公共字符串FontSize{get;set;}
[JsonProperty(PropertyName=“#text”)]
公共字符串TextResult{get;set;}
//其他物体
}
公共类专栏
{
公共列表文本{get;set;}
}

我的回答对你有帮助吗?那不起作用意味着什么?到底发生了什么事?正如@mybirthname在下面的答案中所暗示的,您需要想出一种方法,使
文本
包含一个复杂的类型或字符串。@mybirthname,我将在今晚或明天尝试,并会让您知道。这类似于我在上面尝试的方法,但不起作用,但我会尝试看看我是否遗漏了什么@AndrewHitaker,它不起作用意味着当我取回C#对象时,我希望在该属性中保存的信息是
null
。反序列化(作为一个黑匣子)在我的输入没有产生预期输出的情况下不起作用。我很快会再试一次。我只想提一下,这正是我已经尝试过的
fontName
属性。我没有将
JsonProperty
decorator/属性放在其余属性上的原因纯粹是懒惰。在我向自己证明这是我需要做的之前,我不想把这些都穿上。由于它对
fontName
不起作用,我认为这意味着它对其余部分不起作用。
public class Column
{
    [JsonProperty("@fontName")]
    public string fontName { get; set; }
    public object text { get; set; }
}

public class Row
{
    public List<Column> column { get; set; }
    public string text { get; set; }
}

public class Page
{
    public string index { get; set; }
    public List<Row> row { get; set; }
    public string text { get; set; }
}

public class Document
{
    public List<Page> page { get; set; }
}

public class RootObject
{
    public Document document { get; set; }
}
public class Text
{
    [JsonProperty(PropertyName ="@fontName")]
    public string FontName {get; set;}

    [JsonProperty(PropertyName ="@fontSize")]
    public string FontSize {get; set;}

    [JsonProperty(PropertyName ="#text")]
    public string TextResult{get; set;}

    //other objects
}

public class Column
{
    public List<Text> text { get; set; }
}