私のC#WinFormアプリケーションがアクセスする必要があるJSONファイルは次のとおりです。アカウント名(Windows ADログイン)に基づいて、アプリケーションから特定のファイルにアクセスできます。Newtonsoft.Json(C#)からネストされたJSONオブジェクトにアクセスする方法を教えてください。
他の開発者の提案に基づいて、私はNewtonsoft.Json
を使用することをお勧めして私の質問はここにどのように私は、キー「ファイル」の値を取得するのですか
[
{
"accountName": "admin.lastname",
"files": [
{
"file": "department1.assistant.division.manager.js"
},
{
"file": "department1.division.manager.js"
},
{
"file": "department2.assistant.division.manager.1.js"
},
{
"file": "department2.assistant.division.manager.2.js"
},
{
"file": "department2.division.manager.js"
},
{
"file": "department3.js"
},
{
"file": "department4.assistant.division.manager.1.js"
},
{
"file": "department4.assistant.division.manager.2.js"
},
{
"file": "department4.division.manager.js"
},
{
"file": "department5.assistant.division.manager.1.js"
},
{
"file": "department5.division.manager.js"
},
{
"file": "department6.division.manager.js"
},
{
"file": "department7.division.manager.js"
},
{
"file": "department8.division.manager.js"
},
{
"file": "department9.division.manager.js"
},
{
"file": "department10.js"
},
{
"file": "department11.division.manager.js"
},
{
"file": "department12.assistant.division.manager.1.js"
},
{
"file": "department12.assistant.division.manager.2.js"
},
{
"file": "department12.assistant.division.manager.3.js"
},
{
"file": "department12.assistant.division.manager.4.js"
},
{
"file": "department12.division.manager.js"
},
{
"file": "department13.js"
}
]
},
{
"accountName": "firstname.lastname",
"files": [
{
"file": "department1.assistant.division.manager.js"
},
{
"file": "department1.division.manager.js"
},
{
"file": "department2.assistant.division.manager.1.js"
},
{
"file": "department2.assistant.division.manager.2.js"
},
{
"file": "department2.division.manager.js"
},
{
"file": "department3.js"
},
{
"file": "department4.assistant.division.manager.1.js"
},
{
"file": "department4.assistant.division.manager.2.js"
},
{
"file": "department4.division.manager.js"
},
{
"file": "department5.assistant.division.manager.1.js"
},
{
"file": "department5.division.manager.js"
},
{
"file": "department6.division.manager.js"
},
{
"file": "department7.division.manager.js"
},
{
"file": "department8.division.manager.js"
},
{
"file": "department9.division.manager.js"
},
{
"file": "department10.js"
},
{
"file": "department11.division.manager.js"
},
{
"file": "department12.assistant.division.manager.1.js"
},
{
"file": "department12.assistant.division.manager.2.js"
},
{
"file": "department12.assistant.division.manager.3.js"
},
{
"file": "department12.assistant.division.manager.4.js"
},
{
"file": "department12.division.manager.js"
},
{
"file": "department13.js"
}
]
},
{
"accountName": "jason.bourne",
"files": [
{
"file": "department1.assistant.division.manager.js"
},
{
"file": "department1.division.manager.js"
},
{
"file": "department2.assistant.division.manager.1.js"
},
{
"file": "department2.assistant.division.manager.2.js"
},
{
"file": "department2.division.manager.js"
},
{
"file": "department3.js"
},
{
"file": "department4.assistant.division.manager.1.js"
},
{
"file": "department4.assistant.division.manager.2.js"
},
{
"file": "department4.division.manager.js"
},
{
"file": "department5.assistant.division.manager.1.js"
},
{
"file": "department5.division.manager.js"
},
{
"file": "department6.division.manager.js"
},
{
"file": "department7.division.manager.js"
},
{
"file": "department8.division.manager.js"
},
{
"file": "department9.division.manager.js"
},
{
"file": "department10.js"
},
{
"file": "department11.division.manager.js"
},
{
"file": "department12.assistant.division.manager.1.js"
},
{
"file": "department12.assistant.division.manager.2.js"
},
{
"file": "department12.assistant.division.manager.3.js"
},
{
"file": "department12.assistant.division.manager.4.js"
},
{
"file": "department12.division.manager.js"
},
{
"file": "department13.js"
}
]
},
{
"accountName": "admin.bourne",
"files": [
{
"file": "department1.assistant.division.manager.js"
},
{
"file": "department1.division.manager.js"
},
{
"file": "department2.assistant.division.manager.1.js"
},
{
"file": "department2.assistant.division.manager.2.js"
},
{
"file": "department2.division.manager.js"
},
{
"file": "department3.js"
},
{
"file": "department4.assistant.division.manager.1.js"
},
{
"file": "department4.assistant.division.manager.2.js"
},
{
"file": "department4.division.manager.js"
},
{
"file": "department5.assistant.division.manager.1.js"
},
{
"file": "department5.division.manager.js"
},
{
"file": "department6.division.manager.js"
},
{
"file": "department7.division.manager.js"
},
{
"file": "department8.division.manager.js"
},
{
"file": "department9.division.manager.js"
},
{
"file": "department10.js"
},
{
"file": "department11.division.manager.js"
},
{
"file": "department12.assistant.division.manager.1.js"
},
{
"file": "department12.assistant.division.manager.2.js"
},
{
"file": "department12.assistant.division.manager.3.js"
},
{
"file": "department12.assistant.division.manager.4.js"
},
{
"file": "department12.division.manager.js"
},
{
"file": "department13.js"
}
]
}
]
されています私のC#のコードと何が私がこれまで試みたが、私が前に言ったように私は、私はトンので、もしNewtonsoftのライブラリを使用する新しいです
public void loadAccessControl(String fileName)
{
var locationInformation = System.Environment.CurrentDirectory + Path.DirectorySeparatorChar + fileName;
using (StreamReader file = File.OpenText(locationInformation))
using (JsonTextReader reader = new JsonTextReader(file))
{
JArray o = (JArray)JToken.ReadFrom(reader);
items = o;
MessageBox.Show(items.ToString());
}
foreach (var item in items.Children())
{
var itemProperties = item.Children<JProperty>();
var myElement = itemProperties.FirstOrDefault(x => x.Name == "accountName");
var myElementValue = myElement.Value; ////This is a JValue type
if (myElementValue.ToString().Contains(Environment.UserName))
{
MessageBox.Show("Authorized");
authorizedAccess = true;
myElement = itemProperties.FirstOrDefault(x => x.Name == "files");
myElementValue = myElement.Value; ////This is a JValue type
break;
}
}
}
立ち往生していますここでは良い方法です、私に教えてください。
の作成カスタムモデル
public class Account
{
public string AccountName { get; set; }
public List<FileItem> Files { get; set; }
}
public class FileItem
{
public string File { get; set; }
}
そして使用:
HTTP
クラスAccount.csを追加します。
クラスFile.csが追加します。 // json2cshar p.com/ – vipersassassin