简体   繁体   中英

Converting C# Objects to Json format

(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();

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"]
// }

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