Newtonsoft.Json.JsonConvert.DeserializeObject 返回为 NULL。

huangapple go评论51阅读模式
英文:

Newtonsoft.Json.JsonConvert.DeserializeObject coming back as NULL

问题

I'm getting back the following string from an API call:

{
  "result":
  [
    {
     "sys_id":"12d199028752a9108ae38516dabb35d3"
    }
  ]
}

I'm trying to deserialize it into this class:

public class result
{
    public string sys_id { get; set; }
}

I'm using this call: var x = Newtonsoft.Json.JsonConvert.DeserializeObject<result>(jsonString);

But x.sys_id is always coming up as NULL. What am I missing?

英文:

I'm getting back the following string from an API call:

{
  &quot;result&quot;:
  [
    {
     &quot;sys_id&quot;:&quot;12d199028752a9108ae38516dabb35d3&quot;
    }
  ]
}

I'm trying to deserialize it into this class"

  public class result
    {
        public string sys_id { get; set; }
    }

I'm using this call: var x = Newtonsoft.Json.JsonConvert.DeserializeObject&lt;result&gt;(jsonString);

But "x.sys_id" is always coming up as NULL. What am I missing?

答案1

得分: 2

JSON响应的结构包括一个对象,该对象具有一个类型为“array”的属性result,其中包含一个包含单一属性sys_id(类型为string)的对象。

因此,您需要将响应映射到类似以下的结构:

public class Root // 根据需要更改名称
{
    [JsonProperty("result")]
    public ICollection<ResultItem> Result { get; set; }
}

public class ResultItem // 根据需要更改名称
{
    [JsonProperty("sys_id")]
    public string SysId { get; set; }
}

然后,您可以使用它如下所示:

var x = Newtonsoft.Json.JsonConvert.DeserializeObject<Root>(jsonString);
英文:

The structure of the JSON response consists of an object which has a property result of type array, within which you have an object containing a single property sys_id of type string.

As such, you would need the response to be mapped to something like the following:

public class Root // change name as necessary
{
    [JsonProperty(&quot;result&quot;)]
    public ICollection&lt;ResultItem&gt; Result { get; set; }
}

public class ResultItem // change name as necessary
{
    [JsonProperty(&quot;sys_id&quot;)]
    public string SysId { get; set; }
}

You would then use it as:

var x = Newtonsoft.Json.JsonConvert.DeserializeObject&lt;Root&gt;(jsonString);

答案2

得分: 1

Your JSON models an object containing a result property containing an array with an object with a sys_id property, itself a string. However, your result class is for that final object, the one with sys_id. You're therefore missing a level: you need something to contain those results.

所以让我们定义一个新的类:

public class Document // or whatever other name you want
{
    public List<result> result { get; set; }
}

然后你可以反序列化它:

var x = JsonConvert.DeserializeObject<Document>(jsonString);
英文:

Your JSON models an object containing a result property containing an array with an object with a sys_id property, itself a string. However, your result class is for that final object, the one with sys_id. You're therefore missing a level: you need something to contain those results.

So let's define a new class:

public class Document // or whatever other name you want
{
    public List&lt;result&gt; result { get; set; }
}

And then you can deserialize it:

var x = JsonConvert.DeserializeObject&lt;Document&gt;(jsonString);

答案3

得分: 1

sys_id 是一个对象内的字符串属性,该对象位于一个数组内,因此只能通过索引访问该对象。但您不需要任何类来获取它。您只需解析一个 JSON 字符串。

string sys_id = (string)JObject.Parse(json)["result"][0]["sys_id"];
英文:

sys_id is a string property of an object that is inside of an array, and so the object can be accessed by index only. But you don't need any classes to obtain it.You can just parse a json string

string sys_id = (string) JObject.Parse(json)[&quot;result&quot;][0][&quot;sys_id&quot;];

huangapple
  • 本文由 发表于 2023年5月7日 07:01:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191544.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定