简体   繁体   English

有更好的方法吗?

[英]Is there a better approach?

Using C#; 使用C#; I am attempting to create a web service that will read a excel file located on a OneDrive. 我正在尝试创建一个Web服务,该服务将读取位于OneDrive上的Excel文件。 Based on where the email is from, the excel files will be placed in a folder on a OneDrive (using flow). 根据电子邮件的来源,excel文件将放在OneDrive上的文件夹中(使用流程)。 Using Microsoft graph I would like to read the different excel files, based on the name of the excel file. 使用Microsoft图表我想根据excel文件的名称读取不同的excel文件。

After the excel file is read an REST API call is made. 读取excel文件后,将进行REST API调用。

Problem I am having is. 我遇到的问题是。 How do I tell Microsoft.Graph read excels files even if they are in different folders. 如何告诉Microsoft.Graph读取excels文件,即使它们位于不同的文件夹中。 In MS-Graph the folder ID changes, from folder to folder. 在MS-Graph中,文件夹ID从文件夹更改为文件夹。

I am trying to get the shipping information for the excel files and POST them. 我正在尝试获取excel文件的发货信息并发布它们。

I have tried reading one file. 我试过读一个文件。 Having a hard time parsing JSON into something I can work with. 很难将JSON解析成我可以使用的东西。 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. 我已经完成了使用Microsoft Graph的所有先决条件,包括获取访问令牌和在Azure目录上注册应用程序。

One thought I had was to make a class for the different kind of excel files there can be. 我有一个想法是为不同类型的excel文件创建一个类。

//GRAPH CALL after all authenication - application permissions granted //所有身份验证后的GRAPH CALL - 授予应用程序权限

 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 ///解析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. 它假设在Key,值Pairs中解析Json。 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. 在编程中,如果它工作,那么它可以被认为是正确的答案,但这通常是我建议看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. 如果您使用解析器(我推荐使用Newtonsoft),那么您可以将JSON解析为适合描述的任何对象。 Here's a simple example: 这是一个简单的例子:

Example: JSON may look like this 示例:JSON可能如下所示

{ 
    "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: 然后,如果你想在C#中使用那个JSON,你可以构造一个匹配它的类:

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. 现在你可以使用像Newtonsoft这样的工具来解析它。

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. 这是一个控制台应用程序,它向您展示如何将实例化类型转换为JSON字符串,然后获取JSON字符串并将其转换回实例化类型。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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