2017-02-18 10 views
1

私の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; } 
} 

そして使用:

+2

HTTP

using Newtonsoft.Json; namespace Accounts { class File { [JsonProperty("file")] public string Filename { get; set; } } } 

クラスAccount.csを追加します。

クラスFile.csが追加します。 // json2cshar p.com/ – vipersassassin

答えて

2

がNewtonsoft.JsonからNuGetから取得したデータを照会することができます。

using Newtonsoft.Json; 
using System.Collections.Generic; 

namespace Accounts 
{ 
    class Account 
    { 
     [JsonProperty("accountName")] 
     public string AccountName { get; set; } 

     [JsonProperty("files")] 
     public List<File> Files { get; set; } 
    } 
} 

追加クラスProgram.csの

using Newtonsoft.Json; 
using System; 
using System.Collections.Generic; 

namespace Accounts 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

      const string filename = "accounts.json"; 
      List<Account> accounts = JsonConvert.DeserializeObject<List<Account>>(System.IO.File.ReadAllText(filename)); 

      foreach (Account account in accounts) 
      { 
       Console.WriteLine(account.AccountName); 
       foreach (File file in account.Files) 
       { 
        Console.WriteLine(file.Filename); 
       } 

       Console.WriteLine(); 
      } 
     } 
    } 
} 
1

あなたはこれらのオプションのいずれかを使用することができますカスタムモデルを作成せずに

var result = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Account>>(jsonText); 

var result = Newtonsoft.Json.JsonConvert.DeserializeObject<JArray>(jsonText) 
    .Select(a => new 
    { 
     Account = a.Value<string>("accountName"), 
     Files = a.Value<JArray>("files") 
        .Select(f => new { File = f.Value<string>("file") }).ToList() 
    }).ToList(); 
+0

また 'Newtonsoft.Json'を使用しないと、' System.Web.Script.Serialization.JavaScriptSerializer'を使用してjson値を逆シリアル化することができます。 'var result = new System.Web.Script.Serialization.JavaScriptSerializer()。Deserialize <リスト>(txt);' –

1

オブジェクトグラフを逆シリアル化するように郵便番号をお試しください。あなたは簡単に

public class AccessControlFile 
{ 
    public string file { get; set; } 
} 

public class RootObject 
{ 
    public string accountName { get; set; } 
    public List<AccessControlFile> files { get; set; } 
} 
class Program 
{ 
    static void Main(string[] args) 
    { 

     var jsonString = ""; // load from remote service or from a local file 
     var obj = JsonConvert.DeserializeObject<List<RootObject>>(jsonString); 

    } 
} 
関連する問題