JSON反序列化到C#类 [英] Deserialize JSON to C# Classes
问题描述
下面是一个(略)剥离下来的反应,我从在成功创建一个新的工作规范项的REST API获取。我需要反序列化响应为若干类,但我难倒。
Below is a (slightly) stripped down response I get from a REST API upon successful creation of a new "job code" entry. I need to deserialize the response into some classes, but I'm stumped.
有关参考,我在.NET 3.5使用JSON.NET(在SSIS运行在SQL Server 2008 R2)脚本来尝试我的反序列化。这里的JSON - 我明明没有控制权,因为它是从别人的API来:
For reference, I'm using JSON.NET in .NET 3.5 (running in a SSIS script in SQL Server 2008 R2) to attempt my deserialization. Here's the JSON - which I obviously have no control over as it's coming from someone else's API:
{
"results":{
"jobcodes":{
"1":{
"_status_code":200,
"_status_message":"Created",
"id":444444444,
"assigned_to_all":false,
"billable":true,
"active":true,
"type":"regular",
"name":"1234 Main Street - Jackson"
},
"2":{
"_status_code":200,
"_status_message":"Created",
"id":1234567890,
"assigned_to_all":false,
"billable":true,
"active":true,
"type":"regular",
"name":"4321 Some Other Street - Jackson"
}
}
}
}
在我的C#代码,我有一个JOBCODE类中定义该JSON值仅部分地映射到的属性 - 我没有兴趣在所有的返回的数据我说:
In my C# code, I do have a "JobCode" class defined which only partially maps the JSON values to properties - I'm not interested in all of the data that's returned to me:
[JsonObject]
class JobCode
{
[JsonProperty("_status_code")]
public string StatusCode { get; set; }
[JsonProperty("_status_message")]
public string StatusMessage { get; set; }
[JsonProperty("id")]
public string Id {get; set;}
[JsonProperty("name")]
public string Name { get; set; }
//-------------------------------------------------------------------------------
// Empty constructor for JSON serialization support
//-------------------------------------------------------------------------------
public JobCode() { }
}
我试图通过此调用反序列化数据:
I'm attempting to deserialize the data via this call:
newResource = JsonConvert.DeserializeObject<JobCode>(jsonResponse);
在哪里jsonResponse高于输出。结果
当我执行代码的代码, newResource总是回来为空值 - 因为我知道,其实有数据多jobcodes这不是意外,这代码试图将它反序列化到一个单一的JOBCODE对象。我试图创建一个名为JobCodes新的类,看起来像这样:
Where jsonResponse is the code outputted above.
When I execute the code, "newResource" always comes back as null - which is not unexpected because I know that there are actually multiple jobcodes in the data and this code is trying to deserialize it into a single JobCode object. I tried creating a new class called "JobCodes" that looks like this:
class JobCodes
{
[JsonProperty("jobcodes")]
public List<JobCode>_JobCodes { get; set; }
}
然后我打过电话是:
And then I tried calling this:
newResource = JsonConvert.DeserializeObject<JobCodes>(jsonResponse);
但问题依然存在 - 我回来对象为null。
什么扔我了,我想,是的1和2标识的存在。我不知道如何在我的对象设计和/或JSON.NET类的使用占了他们的存在/财产属性,如[的JSONObject],[JsonProperty]等。
But the issue persists - my return object is null. What's throwing me off, I think, is the presence of the "1" and "2" identifiers. I don't know how to account for their presence in my object design and/or usage of the JSON.NET class / property attributes like [JsonObject],[JsonProperty], etc.
当我运行通过 JSON2CSharp JSON数据,构建了一些看起来怪怪的课,所以这并没有证明太大的效果。我已经验证了几个不同的验证器和它所有检查出来的JSON - 我只是不知道我在这里失踪。
When I run the JSON data through JSON2CSharp, it constructs some weird-looking classes, so that hasn't proven too effective. I've validated the JSON with several different validators and it all checks out - I just don't know what I'm missing here.
最后,我想从JSON数据返回一个列表,但我什么,我需要做要做到这一点难倒了。
Ultimately, I'd like to return a List from the JSON data, but I'm stumped on what I need to do to make that happen.
推荐答案
您的问题是双重的:
- 您别'吨有在根级别定义的类。该类结构需要匹配的全部的JSON,你不能只是从中间反序列化。
- 当你有一个对象,它的键可以改变,你需要使用
词典<字符串,T>
。普通班将不适用于工作; ,也不会在列表< T>
- You don't have a class defined at the root level. The class structure needs to match the entire JSON, you can't just deserialize from the middle.
- Whenever you have an object whose keys can change, you need to use a
Dictionary<string, T>
. A regular class won't work for that; neither will aList<T>
.
请你的类是这样的:
class RootObject
{
[JsonProperty("results")]
public Results Results { get; set; }
}
class Results
{
[JsonProperty("jobcodes")]
public Dictionary<string, JobCode> JobCodes { get; set; }
}
class JobCode
{
[JsonProperty("_status_code")]
public string StatusCode { get; set; }
[JsonProperty("_status_message")]
public string StatusMessage { get; set; }
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
}
然后,反序列化是这样的:
Then, deserialize like this:
RootObject obj = JsonConvert.DeserializeObject<RootObject>(json);
这篇关于JSON反序列化到C#类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!