简体   繁体   中英

Is there a better approach?

Using C#; I am attempting to create a web service that will read a excel file located on a OneDrive. Based on where the email is from, the excel files will be placed in a folder on a OneDrive (using flow). Using Microsoft graph I would like to read the different excel files, based on the name of the excel file.

After the excel file is read an REST API call is made.

Problem I am having is. How do I tell Microsoft.Graph read excels files even if they are in different folders. In MS-Graph the folder ID changes, from folder to folder.

I am trying to get the shipping information for the excel files and POST them.

I have tried reading one file. Having a hard time parsing JSON into something I can work with. Seperating the Key and Value pairs into Objects.

I did all the prerequisites to be able to use the Microsoft Graph including, getting the access token and registering the App on Azure directory.

One thought I had was to make a class for the different kind of excel files there can be.

//GRAPH CALL after all authenication - application permissions granted

 await apiCaller.CallWebApiAndProcessResultASync("https://graph.microsoft.com/v1.0/users/['user  ID']/drive/items/['Excel ID']/workbook/worksheets/Order%20List/usedRange/", result.AccessToken, Display);
            }

/// Parsing the Json

private static void Display(JObject result)
        {
 foreach (JProperty child in result.Properties().Where(p => !p.Name.StartsWith("@")))
            {
Console.WriteLine($"{child.Name} = {child.Value}";
}

}

It suppose to parse the Json in Key, value Pairs. I'm a junior developer. I feel like this is a bit over my head

I believe you're close to having the right answer. In programming, if it works, then it can be considered the right answer however this is typically how I recommend looking at JSON.

Instead of key / value pairs consider the key the Field Name and the Value the Value of that field. If you use a parser (I recommend Newtonsoft) then you can parse the JSON to any object that fits the descriptions. Here's a simple example:

Example: JSON may look like this

{ 
    "Person" : 
    {
        "firstName" : "John",
        "lastName" : "Doe",
        "DOB" : "04/29/1980"
    }
}

Then if you wanted to use that JSON in C# for example you could construct a class that matches it:

class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Date DOB { get; set; }
}

Now you could use a tool like Newtonsoft to parse it.

string json = ...//however you get the JSON
Person person = JsonConvert.DeserializeObject<Person>(json);

Hope this helps.

EDITED to help questions below:

Here's a console app that shows you how to convert an instantiated type to a JSON string and then take the JSON string and convert it back to an instantiated type.

App:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;

namespace JsonToObjectTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var children = new List<Person>()
            {
                new Person("Michael", "Puckett III", new DateTime(2000, 07, 25), null),
                new Person("Samuel", "Puckett", new DateTime(2003, 07, 23), null),
                new Person("Haylee", "Sanders", new DateTime(2004, 01, 05), null)
            };

            var dad = new Person("Michael", "Puckett II", new DateTime(1980, 01, 29), children);

            var json = JsonConvert.SerializeObject(dad); //dad with array of children is converted to a json string here

            Console.WriteLine(json); 

            var jsonDad = JsonConvert.DeserializeObject<Person>(json); //json string is converted to Person object here

            Console.WriteLine($"Name: {jsonDad.FirstName} {jsonDad.LastName}\nDOB: {jsonDad.DOB.ToString()}");

            foreach (var child in jsonDad.Children)
            {
                Console.WriteLine($"Name: {child.FirstName} {child.LastName}\nDOB: {child.DOB.ToString()}");
            }

            Console.Read();
        }

        public class Person
        {
            public Person(string firstName, string lastName, DateTime dob, List<Person> children)
            {
                FirstName = firstName;
                LastName = lastName;
                DOB = dob;
                Children = children;
            }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public DateTime DOB { get; set; }
            public List<Person> Children { get; set; }
        }
    }
}

Outputs:

{"FirstName":"Michael","LastName":"Puckett II","DOB":"1980-01-29T00:00:00","Children":[{"FirstName":"Michael","LastName":"Puckett III","DOB":"2000-07-25T00:00:00","Children":null},{"FirstName":"Samuel","LastName":"Puckett","DOB":"2003-07-23T00:00:00","Children":null},{"FirstName":"Haylee","LastName":"Sanders","DOB":"2004-01-05T00:00:00","Children":null}]}
Name: Michael Puckett II
DOB: 1/29/1980 12:00:00 AM
Name: Michael Puckett III
DOB: 7/25/2000 12:00:00 AM
Name: Samuel Puckett
DOB: 7/23/2003 12:00:00 AM
Name: Haylee Sanders
DOB: 1/5/2004 12:00:00 AM

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM