英文:
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:
{
"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?
答案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("result")]
public ICollection<ResultItem> Result { get; set; }
}
public class ResultItem // change name as necessary
{
[JsonProperty("sys_id")]
public string SysId { get; set; }
}
You would then use it as:
var x = Newtonsoft.Json.JsonConvert.DeserializeObject<Root>(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<result> result { get; set; }
}
And then you can deserialize it:
var x = JsonConvert.DeserializeObject<Document>(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)["result"][0]["sys_id"];
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论