使用C#控制台应用程序将反序列化JSON响应存储到DataTable中

使用C#控制台应用程序将反序列化JSON响应存储到DataTable中,c#,json,datatable,console-application,C#,Json,Datatable,Console Application,你好 我需要从文件(“AllProjects.JSON”)中读取JSON响应,然后将JSON响应数据存储在数据表中。当有一个passprojectname(pName)作为过滤器时,则需要获取项目详细信息(如:Id、Name、templates)。我是C#新手,尝试了以下代码,请帮助我完成下一部分。 提前谢谢你 AllProjects.JSON: { "name": "projects", "totalRows": 25, "rowData": [ { "id":

你好

我需要从文件(“AllProjects.JSON”)中读取JSON响应,然后将JSON响应数据存储在数据表中。当有一个passprojectname(pName)作为过滤器时,则需要获取项目详细信息(如:Id、Name、templates)。我是C#新手,尝试了以下代码,请帮助我完成下一部分。 提前谢谢你

AllProjects.JSON:

{
  "name": "projects",
  "totalRows": 25,
  "rowData": [
    {
      "id": "100",
      "name": "Project1",
      "data": {
        "creator_id": "336",
        "create_time": "5/1/2020 5:21:24 AM",
        "is_global": "False",
        "last_publication": "5/1/2020 5:21:29 AM",
        "active": "True",
        "lnk_cnt": "0",
        "templates": "5",
        "owner": "Quepal"
      }
    },
    {
      "id": "101",
      "name": "Project2",
      "data": {
        "creator_id": "336",
        "create_time": "4/30/2020 4:01:22 AM",
        "is_global": "False",
        "last_publication": "4/30/2020 4:01:27 AM",
        "active": "True",
        "lnk_cnt": "0",
        "templates": "5",
        "owner": "Quepal"
      }
    }
   ]
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;

namespace DataTable
{
    class Program
    {
        public class Data
        {
            public string creator_id { get; set; }
            public DateTime create_time { get; set; }
            public string is_global { get; set; }
            public DateTime last_publication { get; set; }
            public string active { get; set; }
            public string lnk_cnt { get; set; }
            public string templates { get; set; }
            public string owner { get; set; }

        }
        public class RowData
        {
            public string id { get; set; }
            public string name { get; set; }
            public Data data { get; set; }

        }
        public class Application
        {
            public string name { get; set; }
            public int totalRows { get; set; }
            public IList<RowData> rowData { get; set; }

        }
        static void Main(string[] args)
        {
            string pName = "Project1";

            var responseBody = File.ReadAllText("D:\\EnrichInput\\AllProjects.JSON");
            var allProjs = JsonConvert.DeserializeObject<Application>(responseBody);




        }
    }
}
C#控制台应用程序代码:

{
  "name": "projects",
  "totalRows": 25,
  "rowData": [
    {
      "id": "100",
      "name": "Project1",
      "data": {
        "creator_id": "336",
        "create_time": "5/1/2020 5:21:24 AM",
        "is_global": "False",
        "last_publication": "5/1/2020 5:21:29 AM",
        "active": "True",
        "lnk_cnt": "0",
        "templates": "5",
        "owner": "Quepal"
      }
    },
    {
      "id": "101",
      "name": "Project2",
      "data": {
        "creator_id": "336",
        "create_time": "4/30/2020 4:01:22 AM",
        "is_global": "False",
        "last_publication": "4/30/2020 4:01:27 AM",
        "active": "True",
        "lnk_cnt": "0",
        "templates": "5",
        "owner": "Quepal"
      }
    }
   ]
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;

namespace DataTable
{
    class Program
    {
        public class Data
        {
            public string creator_id { get; set; }
            public DateTime create_time { get; set; }
            public string is_global { get; set; }
            public DateTime last_publication { get; set; }
            public string active { get; set; }
            public string lnk_cnt { get; set; }
            public string templates { get; set; }
            public string owner { get; set; }

        }
        public class RowData
        {
            public string id { get; set; }
            public string name { get; set; }
            public Data data { get; set; }

        }
        public class Application
        {
            public string name { get; set; }
            public int totalRows { get; set; }
            public IList<RowData> rowData { get; set; }

        }
        static void Main(string[] args)
        {
            string pName = "Project1";

            var responseBody = File.ReadAllText("D:\\EnrichInput\\AllProjects.JSON");
            var allProjs = JsonConvert.DeserializeObject<Application>(responseBody);




        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.IO;
使用Newtonsoft.Json;
命名空间数据表
{
班级计划
{
公共类数据
{
公共字符串创建者_id{get;set;}
公共日期时间创建时间{get;set;}
公共字符串是_global{get;set;}
公共日期时间上次发布{get;set;}
公共字符串活动{get;set;}
公共字符串lnk_cnt{get;set;}
公共字符串模板{get;set;}
公共字符串所有者{get;set;}
}
公共类行数据
{
公共字符串id{get;set;}
公共字符串名称{get;set;}
公共数据数据{get;set;}
}
公共类应用程序
{
公共字符串名称{get;set;}
公共整数totalRows{get;set;}
公共IList行数据{get;set;}
}
静态void Main(字符串[]参数)
{
字符串pName=“Project1”;
var responseBody=File.ReadAllText(“D:\\EnrichInput\\AllProjects.JSON”);
var allProjs=JsonConvert.DeserializeObject(ResponseBy);
}
}
}

您可以使用system.linq筛选数据,如下所示

        var proj = allProjs.rowData.FirstOrDefault(x => x.name == "projname");
        if(proj == null) return;

        var id = proj.id;
        var name = proj.name;
        var data_templates = proj.data.templates;

具体来说,您遇到了什么问题?您尚未显示
ProjectName(pName)
方法。Pro提示:您可以将谓词放入
FirstOrDefault(x=>x.name…)
中,而不必同时使用
Where