【问题标题】:How to access Json Attribute which has space in its name如何访问名称中包含空格的 Json 属性
【发布时间】:2013-06-07 11:00:13
【问题描述】:

在 json 响应下方查找...

{
"personalDetails": {
    "Name ": " Taeyeon",
    "Date Of Birth ": " 03/09/1989",
    "Zodiac ": " Pisces"
},
"education": {
    "High School ": " Jeonju Art High school ",
    "University ": " -"
}

}

我的班级在这里

    public class Biography
{
    public personalDetails personalDetails { get; set; }
    public education education { get; set; }
    public work work { get; set; }
    public personal personal { get; set; }
}


public class personalDetails
{
    public string Name { get; set; }
    public string DateBirth { get; set; }
    public string Zodiac { get; set; }
}

public class education
{
    public string HighSchool { get; set; }
    public string University { get; set; }
}

然后我放代码:

Biography dataSet = JsonConvert.DeserializeObject<Biography>(e.Result);

由于Arttribute有空间,它不起作用。 我该怎么办?

【问题讨论】:

    标签: c# json windows-phone


    【解决方案1】:

    尝试添加JsonProperty 属性。这应该对你有用。

    [JsonProperty(PropertyName = "Date Of Birth ")]
    public string DateBirth { get; set; }
    
    [JsonProperty(PropertyName = "High School ")]
    public string HighSchool { get; set; }
    

    编辑

    我看到你也有尾随空格,所以更新了上面的属性。对“名称”等执行相同操作。

    【讨论】:

      【解决方案2】:

      将 Json 作为字符串下载,并使用类似 myString = myString.Replace(@"High School", "HighSchool") 的内容。将此作为反序列化之前的步骤。

      【讨论】:

        【解决方案3】:

        这对某些人可能会有所帮助:

        添加命名空间:using Newtonsoft.Json;

        var jsonString = "{" +
            "'personalDetails': {" +
                "'Name ': 'Taeyeon'," +
                "'Date Of Birth ': ' 03/09/1989'," +
                "'Zodiac ': ' Pisces'," +
            "}," +
            "'education': {" +
                "'High School ': ' Jeonju Art High school '," +
                "'University ': ' -'," +
            "}" +
        "}";
        
        var json = JsonConvert.DeserializeObject(jsonString);            
        return Ok(json);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-08-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-12-27
          • 1970-01-01
          相关资源
          最近更新 更多