C# 正在处理JObject[]筛选和匹配
我正在使用JsonClassGenerator项目将Json转换为C#类 这是我正在构建的一些中间件,用于连接到我的公司已经构建了很多的TrackVia API项目 我现在正在我的.Net应用程序中构建数据层 本质上,我有一个Json结构,我需要解析它,以确定是否应该根据“结构”中的“canUpdate”值序列化“data”中的一个属性 让我们从Json结构开始:C# 正在处理JObject[]筛选和匹配,c#,.net,json,modeling,C#,.net,Json,Modeling,我正在使用JsonClassGenerator项目将Json转换为C#类 这是我正在构建的一些中间件,用于连接到我的公司已经构建了很多的TrackVia API项目 我现在正在我的.Net应用程序中构建数据层 本质上,我有一个Json结构,我需要解析它,以确定是否应该根据“结构”中的“canUpdate”值序列化“data”中的一个属性 让我们从Json结构开始: { "structure": [ { "name": "Updated", "type": "d
{
"structure": [
{
"name": "Updated",
"type": "datetime",
"required": false,
"unique": false,
"canRead": true,
"canUpdate": false,
"canCreate": false
},
{
"name": "Amount",
"type": "currency",
"required": false,
"unique": false,
"canRead": true,
"canUpdate": false,
"canCreate": false
},
{
"name": "Description",
"type": "paragraph",
"required": false,
"unique": false,
"canRead": true,
"canUpdate": false,
"canCreate": false
},
{
"name": "Created",
"type": "datetime",
"required": false,
"unique": false,
"canRead": true,
"canUpdate": false,
"canCreate": false
},
{
"name": "Quantity",
"type": "number",
"required": false,
"unique": false,
"canRead": true,
"canUpdate": true,
"canCreate": true
},
{
"name": "Created By User",
"type": "user",
"required": false,
"unique": false,
"canRead": true,
"canUpdate": false,
"canCreate": false
},
{
"name": "ID",
"type": "number",
"required": false,
"unique": true,
"canRead": true,
"canUpdate": false,
"canCreate": false
},
{
"name": "Last User",
"type": "user",
"required": false,
"unique": false,
"canRead": true,
"canUpdate": false,
"canCreate": false
},
{
"name": "Unit_Tracker",
"type": "relationship",
"required": false,
"unique": false,
"canRead": true,
"canUpdate": true,
"canCreate": true
},
{
"name": "Project_Materials",
"type": "relationship",
"required": false,
"unique": false,
"canRead": true,
"canUpdate": true,
"canCreate": true
},
{
"name": "Record ID",
"type": "identifier",
"required": false,
"unique": false,
"canRead": true,
"canUpdate": false,
"canCreate": false
}
],
"data": [
{
"Updated": "2017-04-20T10:12:00.000-06:00",
"Project_Materials(id)": 404,
"Unit_Tracker(id)": 209,
"Amount": "78.18",
"Description": "1/2 in",
"Quantity": "1",
"Created By User": "By",
"Unit_Tracker": "R04-5050",
"Project_Materials": "1001755734",
"id": 772,
"Created": "2017-04-20T10:12:00.000-06:00",
"Last User": "By",
"Last User(id)": 58443,
"Created By User(id)": 58443,
"Record ID": "1/2 in"
}
],
"totalCount": 763
}
这是我需要匹配的。我需要将“data”(即“Update”)中的一个键与“structure”(即结构->名称)中的一个键值匹配:“Update”,然后确定“canRead”、“canUpdate”、“canCreate”的值以设置某种标志
然后,该标志将告诉代码编写器添加一行“[JsonIgnoreSerialization]”,并在我输出的文本中生成一个格式化属性,如下所示:
[JsonIgnoreSerialization]
[JsonProperty("Updated")]
public string Updated { get; set; }
我甚至没有使用过JObjects,所以我的代码现在是空的,因为我不知道从哪里开始
这是一件大事,因为我必须在我的应用程序中,基于API的Json导出,将Trackvia中的数百个表转换为C#类——这将花费我数周的时间,我希望有一个定制的转换工具,为我构建C#类——所以拥有它将是一件了不起的事情
我不想找讲义,我愿意为自己的成功而努力,需要一些指导。试试这种定制的课堂结构
public class Root
{
public List<Structure> structure;
}
public class Structure
{
public string name { get; set; }
public string type { get; set; }
public string required { get; set; }
public string unique { get; set; }
public string canRead { get; set; }
public string canUpdate { get; set; }
public string canCreate { get; set; }
}
注意:sample.json包含您的json响应。您觉得答案有用吗?还是没有解决你的问题?
using (var stream = new StreamReader("sample.json"))
{
var rootObject = JsonConvert.DeserializeObject<Root>(stream.ReadToEnd());
int index = 0;
foreach (var structures in rootObject.structure)
{
Console.WriteLine(structures.canUpdate);
}
}