troubleshooting Question

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

Avatar of trevor1940
trevor1940 asked on
.NET ProgrammingC#JSON
4 Comments2 Solutions43 ViewsLast Modified:
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"))
            }

        }
    }
}

BBCMTopics.cs
namespace JsonToArray
{
    public class BBCMTopics
    {

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

    }
}

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"}]
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 2 Answers and 4 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros