【问题标题】:Json Naming issueJson 命名问题
【发布时间】:2016-02-17 08:01:37
【问题描述】:

我是 JSON 和 API 的新手 我正在尝试从我拥有 Json 并想为其创建一个类的 API 中获取一些数据。当我使用 josn2csharp 转换它时,我得到以下代码。请让我知道我的课程的正确格式应该是什么,以便它转换为 json。

public class DataSet {
    public string __invalid_name__-i:nil { get; set; } }

public class GenericObject {
    public string __invalid_name__-xmlns:d2p1 { get; set; }
    public string __invalid_name__-i:type { get; set; }
    public string __invalid_name__#text { get; set; } }

public class Message {
    public string __invalid_name__-i:nil { get; set; } }

public class FunctionResponse {
    public string __invalid_name__-xmlns:i { get; set; }
    public string __invalid_name__-xmlns { get; set; }
    public DataSet DataSet { get; set; }
    public GenericObject GenericObject { get; set; }
    public Message Message { get; set; }
    public string Success { get; set; } }

public class RootObject {
    public FunctionResponse FunctionResponse { get; set; } }

使用this问题作为参考,我开始知道如何写作

公共字符串 invalid_name#text { get;放; }

但是其他人呢。 请帮忙

编辑 1:

{
   "DataSet": {
      "@nil": "true"
   },
   "GenericObject": {
      "@type": "d2p1:boolean",
      "#text": "true"
   },
   "Message": {
      "@nil": "true"
   },
   "Success": "true"
}

【问题讨论】:

  • 如您提到的问题的答案中所述,您可以将属性命名为任何名称,确保 JsonProperty 参数与您要解析的 json 的键匹配。

标签: c# json windows-phone-8.1 json.net


【解决方案1】:

例子:

{
 "json": 
   {
   "#thekeyinsidejsonproperty": "value"
   }
}

你应该有

public class RootObject
{
    [JsonProperty("json")]
    public Json jsonobj;

}

 public class Json
{
    [JsonProperty("#thekeyinsidejsonproperty")]
    public string somepropertyname{ get; set; }

}

希望对您有所帮助! :)

【讨论】:

  • 这意味着对于 "DataSet": { "-i:nil": "true" },我可以写 public class DataSet { [JsonProperty("-i:nil")] public string Idata {得到;放; } }
  • 这是我必须要做的事情
  • public class DataSet { [JsonProperty("-i:nil")] public string Idata { get;放; } } 你的 json 应该是 { "-i:nil": "true" } 而不是 "DataSet": { "-i:nil": "true" }
  • 否则将你的类设为 public class RootObject { [JsonProperty("DataSet")] public DataSet dataSet; } 如果你对你的 json 非常严格 -- {"DataSet": { "-i:nil": "true" }}
  • 继续我给它应该工作的类结构(Y)。
猜你喜欢
  • 2014-03-26
  • 2018-07-26
  • 2020-05-16
  • 2011-02-05
  • 2011-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多