0
我是个初学者在发展,所以请不要判断。 我需要阅读许多(如74)JSON文件,并且它们都具有其他值。 如何动态读取JSON文件并选择特定值?如何读取JSON文件,并选择特定值C#[完成]
目前我使用Newtonsoft JSON,但我不知道我怎样才能得到具体数值。 因此,作为示例,这是JSON文件之一。
{
"value": [
{
"resourceRef": "/accessControlLists/abc123",
"resourceId": "abc123",
"resourceMetadata": {
"resourceName": "SCVMM Default ACL"
},
"etag": "W/\"ea02b098-bcd1-406a-abff-ab380011d510\"",
"instanceId": "e123f536-ffc1-429e-b67e-50627b1613c8",
"properties": {
"provisioningState": "Succeeded",
"aclRules": [
{
"resourceRef": "/accessControlLists/abc123/aclRules/def456",
"resourceId": "def456",
"etag": "W/\"ea02b098-bcd1-406a-abff-ab380011d510\"",
"instanceId": "e3467412-e9c7-48a0-9d45-bdaedbe72d2d",
"properties": {
"provisioningState": "Succeeded",
"protocol": "All",
"sourcePortRange": "*",
"destinationPortRange": "*",
"action": "Allow",
"sourceAddressPrefix": "*",
"destinationAddressPrefix": "*",
"priority": "65000",
"description": "SCVMM Default Rule - Allow All",
"type": "Inbound",
"logging": "Enabled"
}
}
],
"ipConfigurations": [],
"subnets": []
}
}
],
"nextLink": ""
}
第二个文件是这样的。
{
"resourceRef": "/virtualNetworkManager/",
"instanceId": "00000000-0000-0000-0000-000000000000",
"properties": {
"provisioningState": "Succeeded",
"distributedRouterState": "Enabled",
"networkVirtualizationProtocol": "VXLAN"
}
}
那么我怎样才能读取第一个文件的描述样本? 我该如何动态地做到这一点?
感谢您的帮助!
编辑: dynamic
与JsonConvert
为我工作。
string path = string.Empty;
Console.Write("Please enter json file path: ");
path = Console.ReadLine();
dynamic obj = JObject.Parse(File.ReadAllText(path));
for (int i = 0; i < obj.value.Count; i++)
{
for (int j = 0; j < obj.value[i].properties.aclRules.Count; j++)
{
Console.WriteLine(obj.value[i].properties.aclRules[j].properties.description);
}
}
如果你是初学者不要紧。规则适用于所有人。关键字:newtonsoft –
您可以使用['SelectToken'](http://www.newtonsoft.com/json/help/html/QueryJsonSelectToken.htm)。见[这里](https://stackoverflow.com/questions/1698175/what-is-the-json-net-equivalent-of-xmls-xpath-selectnodes-selectsinglenode/1711407#1711407)或[这里](https: //stackoverflow.com/questions/19645501/searching-for-a-specific-jtoken-by-name-in-a-jobject-hierarchy/19646950#19646950)。从[如何使用C#解析JSON?]开始(https://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c)。 – dbc
谢谢。但是这一切都没有帮助。当我使用课程时,我有30多个课程。没有其他办法可以做到吗? – BlackMorokei