0

I return data from the following:

  var result = await client.GetAsync("https://management.azure.com/subscriptions/c43vbhu- 
  XXXX-XXXX--2b83a074c23f/providers/Microsoft.Consumption/usageDetails?startDate=2020-08- 
   01&endDate=2020-08-05&$top=1000&api-version=2019-10-01");                      

  string data = result.Content.ReadAsStringAsync().Result;

The data is returned but how can I deserialize this into an object so I can traverse the data and do some calculations?

What is the class that I need to serialize to?

Skin
  • 9,085
  • 2
  • 13
  • 29
Mel
  • 319
  • 2
  • 16
  • You can generate classes based on existing json https://www.codeproject.com/Tips/5358402/Convert-JSON-to-Csharp-Classes-using-Paste-JSON-as or get the data dynamically from JObject https://stackoverflow.com/questions/16097768/how-to-get-property-from-dynamic-jobject-programmatically – Yevhen Cherkes Jul 23 '23 at 22:19
  • I like https://json2csharp.com/ because it has the option of setting up the `JsonProperty` attributes. I'd also suggest using `string data = await result.Content.ReadAsStringAsync()` - never use `.Result()` unless you absolutely have to. – stuartd Jul 23 '23 at 22:32
  • ok i get the data .. but it does not have values that are the same as what is in Azure Portal ? How can i work out what its costing for a resource / Do i need to get some values and apply some formulae ? – Mel Jul 24 '23 at 00:56
  • @Mel, you are asking "...but it does not have values that are the same as what is in Azure Portal". The original question is asking about the Serialization whereas the comment is asking for more data. What is the actual issue? Serialization or Data? – Prem Jul 24 '23 at 06:21

1 Answers1

0

How can i serialize a Json object returned from Azure consumption API?

I have reproduced in my environment and below are my results:

To get object from API and serialize use below code:

Code:

using Newtonsoft.Json.Linq;
using System.Text.Json;

using var httpClient = new HttpClient();
var r1 = await httpClient.GetAsync("https://www.xx.yy/ols/api/ontologies");
var c = await r1.Content.ReadAsStringAsync();
JObject json1 = JObject.Parse(c);
Console.WriteLine(json1["_embedded"]["ontologies"][0]["config"]);
Console.ReadLine();

Output:

enter image description here

RithwikBojja
  • 5,069
  • 2
  • 3
  • 7
  • yes i can get the data .. but when i look at the data in relation to Azure portal , they are not the same – Mel Jul 24 '23 at 07:05
  • You should open a new thread or question for that, I answered for above mentioned description in question. – RithwikBojja Jul 24 '23 at 07:25