Avatar of trevor1940
trevor1940

asked on 

C#: Read JSON file to List<t> and query

Hi

I have the JSON file Below how do I read it into a  LIST<T> so I can use a case insensitive .Where query against label and return id?
Program

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JsonToArray
{
    class Program
    {
        static void Main(string[] args)
        {
            // deserialize JSON directly from a file
            string jsonFile = @"Path\To\BBCM_topics.json";
            List<BBCMTopics> bbcmTopics = new List<BBCMTopics>();
            if (File.Exists(jsonFile))
            {
                var json = File.ReadAllText(jsonFile);
                var rootObject = JsonConvert.DeserializeObject<>(json);

                //         string MyId = rootObject. .bbcm.label.Where(l => l.Equals("Domestic economic"))
            }

        }
    }
}

Open in new window


BBCMTopics.cs
namespace JsonToArray
{
    public class BBCMTopics
    {

        public string id { get; set; }
        public string label { get; set; }

    }
}

Open in new window


BBCM_topics.json

[{"id":"Civil_unrest","label":"Civil unrest"},{"id":"Coronavirus","label":"Coronavirus"},{"id":"Crime","label":"Crime"},{"id":"Cyber_security","label":"Cyber security"},{"id":"Domestic_economic","label":"Domestic economic"},{"id":"Domestic_political","label":"Domestic political"},{"id":"Elections","label":"Elections"},{"id":"Energy","label":"Energy"},{"id":"Environment","label":"Environment"},{"id":"Fake_news","label":"Fake news"},{"id":"Health","label":"Health"},{"id":"Human_rights","label":"Human rights"},{"id":"Humanitarian","label":"Humanitarian"},{"id":"International_economic","label":"International economic"},{"id":"International_political","label":"International political"},{"id":"Jihadism","label":"Jihadism"},{"id":"Leader","label":"Leader"},{"id":"Maritime","label":"Maritime"},{"id":"Media","label":"Media"},{"id":"Migration","label":"Migration"},{"id":"Military","label":"Military"},{"id":"Narcotics","label":"Narcotics"},{"id":"Peacekeeping","label":"Peacekeeping"},{"id":"Proliferation","label":"Proliferation"},{"id":"Society","label":"Society"},{"id":"Technology","label":"Technology"},{"id":"Telecom","label":"Telecom"},{"id":"Terrorism","label":"Terrorism"}]

Open in new window

.NET ProgrammingC#JSON

Avatar of undefined
Last Comment
trevor1940

8/22/2022 - Mon