我一直在使用NewtonSoft JSON Convert库来解析JSON字符串并将其转换为C#对象.但是现在我遇到了一个非常笨拙的JSON字符串,我无法将其转换为C#对象,因为我无法从这个JSON字符串中创建一个C#类.
这是JSON字符串
{
"1": {
"fajr": "04:15",
"sunrise": "05:42",
"zuhr": "12:30",
"asr": "15:53",
"maghrib": "19:18",
"isha": "20:40"
},
"2": {
"fajr": "04:15",
"sunrise": "05:42",
"zuhr": "12:30",
"asr": "15:53",
"maghrib": "19:18",
"isha": "20:41"
}
}
Run Code Online (Sandbox Code Playgroud)
解析此JSON字符串所需的C#类应如下所示:
public class 1 {
public string fajr { get; set; }
public string sunrise { get; set; }
public string zuhr { get; set; }
public string asr { get; set; }
public string maghrib { get; set; }
public string …Run Code Online (Sandbox Code Playgroud) 下面是我在成功创建新的"作业代码"条目后从REST API获得的(稍微)精简的响应.我需要将响应反序列化为某些类,但我很难过.
作为参考,我在.NET 3.5中使用JSON.NET(在SQL Server 2008 R2中的SSIS脚本中运行)来尝试反序列化.这是JSON - 我显然无法控制它,因为它来自其他人的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"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在我的C#代码中,我确实定义了一个"JobCode"类,它只将JSON值部分映射到属性 - 我对返回给我的所有数据都不感兴趣:
[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; …Run Code Online (Sandbox Code Playgroud) 我从一个看起来像这样的API获得JSON:
{
"Items": {
"Item322A": [{
"prop1": "string",
"prop2": "string",
"prop3": 1,
"prop4": false
},{
"prop1": "string",
"prop2": "string",
"prop3": 0,
"prop4": false
}],
"Item2B": [{
"prop1": "string",
"prop2": "string",
"prop3": 14,
"prop4": true
}]
},
"Errors": ["String"]
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了一些方法来在c#对象中表示这个JSON(这里列出的太多了).我已尝试过列表和词典,这是我最近尝试如何表示它的一个例子:
private class Response
{
public Item Items { get; set; }
public string[] Errors { get; set; }
}
private class Item
{
public List<SubItem> SubItems { get; set; }
}
private class SubItem
{
public List<Info> Infos { get; …Run Code Online (Sandbox Code Playgroud)