Сохраните Deserialize JSON ответ в DataTable с помощью приложения C# Console - PullRequest
0 голосов
/ 26 мая 2020

Приветствую .. !!!

Мне нужно прочитать JSON ответ из файла («AllProjects. JSON»), а затем сохранить данные JSON респонов в таблице DATA. Когда когда-либо передается ProjectName (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"
      }
    }
   ]
}

C# COnsole application Code:

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




        }
    }
}

1 Ответ

1 голос
/ 26 мая 2020

Вы можете фильтровать свои данные с помощью 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;
...