2

(Edited) My asp.net core version is 2.0

I am new to programming. I am currently trying to call Microsoft Graph to list all of the application objects in my Azure AD tenant and save them JSON files.

I am using the Microsoft Graph SDK. When I make the call, the results comes back as a collection of Microsoft.Graph.Application objects but I can't convert them to JSON.

I've tried to convert the result using .ToString(), but each object just becomes "Microsoft.Graph.Application".

Is there a good way to do this?

IConfidentialClientApplication daemonClient = ConfidentialClientApplicationBuilder
    .Create(clientId)
    .WithClientSecret(clientSecret)
    .WithTenantId(tenantId)
    .Build();

ClientCredentialProvider authProvider = new ClientCredentialProvider(daemonClient);
GraphServiceClient graphServiceClient = new GraphServiceClient(authProvider);
GetApplicationList(graphServiceClient, log).GetAwaiter().GetResult();

var applicationList = await graphServiceClient.Applications.Request().GetAsync();
3
  • 3
    This is called Serialization. See stackoverflow.com/search?q=%5Bc%23%5D+serialize+to+json for loads of information on this. Commented Feb 16, 2020 at 22:32
  • Look in to using the package Newtonsoft.Json from NuGet. It makes things very easy to turn json to objects and objects to json. Commented Feb 16, 2020 at 22:32
  • If you are using ASP.NET Core 3.0, there is now a built-in System.Text.Json set of classes which provides out-of-the-box JSON (de)serialization without a dependency on Newtonsoft.Json. It's a bit simpler, and doesn't support some more sophisticated scenarios, but it's also supposed to be faster for most cases. Microsoft's documentation for this is quite good. Commented Feb 16, 2020 at 22:46

1 Answer 1

6

You didn't write which version of asp .net core you use.

In version 2.0 Json Newtonsoft library is used to handle json serialization/deserialization.

Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Sizes = new string[] { "Small" };

string json = JsonConvert.SerializeObject(product);
// {
//   "Name": "Apple",
//   "Expiry": "2008-12-28T00:00:00",
//   "Sizes": [
//     "Small"
//   ]
// }

In version 3.0 has been introduced new default library for handling json operations System.Text.Json, but you can change configuration to still use Newtonsoft. Example usage of the System.Text.Json.

string jsonString;
jsonString = JsonSerializer.Serialize(weatherForecast);
// {
//  "Date": "2019-08-01T00:00:00-07:00",
//  "TemperatureCelsius": 25,
//  "Summary": "Hot",
//  "DatesAvailable": ["2019-08-01T00:00:00-07:00",
//  "2019-08-02T00:00:00-07:00"],
//  "TemperatureRanges": {
//      "Cold": {
//          "High": 20,
//          "Low": -10
//      },
//      "Hot": {
//          "High": 60,
//          "Low": 20
//      }
//  },
//  "SummaryWords": ["Cool",
//  "Windy",
//  "Humid"]
// }
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you all for your help. I've successfully serialized the C# object to JSON using Newtonsoft.Json.
I will attach my code for people who ran into the same question. string json = JsonConvert.SerializeObject(application);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.