如何从Newtonsoft.Json(C#)访问嵌套的JSON对象?

时间:2017-02-18 13:30:16

标签: c# json winforms

这是我的C#WinForm应用程序需要访问的JSON文件。根据您的帐户名称(Windows AD登录),您可以访问应用程序中的特定文件。

根据其他开发者的建议,我被鼓励使用 Newtonsoft.Json

我的问题是如何获取密钥“file”的值

[
  {
    "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#代码,到目前为止我尝试了但是我被困了

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

正如我之前所说,我是新手使用Newtonsoft的图书馆,所以如果有更好的方法,请告诉我。

3 个答案:

答案 0 :(得分:2)

NuGet 获取 Newtonsoft.Json

添加课程 File.cs

using Newtonsoft.Json;

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

添加课程 Account.cs

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 :(得分:1)

您可以使用以下任一选项:

创建自定义模型

public class Account
{
    public string AccountName { get; set; }
    public List<FileItem> Files { get; set; }
}
public class FileItem
{
    public string File { get; set; }
}

然后使用:

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();

答案 2 :(得分: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);

    }
}