-1

I am trying to get the List from a JSON string from a 3rd party API. I am not able to understand how should I parse it,as the key name starts with numeric. I do not need the key but I do require value, but I require the value part in my code to be stored in DB.

JSON

{
  "5MIN": [
    {
      "SETTLEMENTDATE": "2021-08-16T00:30:00",
      "REGIONID": "NSW1",
      "REGION": "NSW1",
      "RRP": 39.27,
      "TOTALDEMAND": 7416.02,
      "PERIODTYPE": "ACTUAL",
      "NETINTERCHANGE": -788.69,
      "SCHEDULEDGENERATION": 5518.17,
      "SEMISCHEDULEDGENERATION": 1076.47
    },
    {
      "SETTLEMENTDATE": "2021-08-16T01:00:00",
      "REGIONID": "NSW1",
      "REGION": "NSW1",
      "RRP": 36.51,
      "TOTALDEMAND": 7288.89,
      "PERIODTYPE": "ACTUAL",
      "NETINTERCHANGE": -828.1,
      "SCHEDULEDGENERATION": 5362.3,
      "SEMISCHEDULEDGENERATION": 1064.35
    }
  ]

}

I think I am over complicating the issue, but I am confused

Abhishek
  • 517
  • 3
  • 18
  • How come a duplicate question can be a downvote question? I have already walked through that question, I wasnt able to get the logic working. – Abhishek Aug 16 '21 at 15:06

1 Answers1

2

As mentioned in the comments above, you can paste the json as code. Next you add a reference to Newtonsoft.Json:

dotnet add package newtonsoft.json

You then call JsonConvert.Deserialize<T>() as in the example below:

using System;
using System.Collections.Generic;

using System.IO;
using Newtonsoft.Json;
using StackOverflow;
using System.Linq;

//In this example I load the JSON from disk
var json = File.ReadAllText("/home/timothy/data.json");
var record = JsonConvert.DeserializeObject<ServiceResponse>(json);

//No need to convert to List<T> if you're not going to filter it
var results = record.The5Min.ToList();

foreach(var item in results)
{
    Console.WriteLine($"{item.Settlementdate}, {item.Regionid}");
}

namespace  StackOverflow
{
    using System;
    using System.Collections.Generic;

    using System.Globalization;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;

    public partial class ServiceResponse
    {
        [JsonProperty("5MIN")]
        public The5Min[] The5Min { get; set; }
    }

    public partial class The5Min
    {
        [JsonProperty("SETTLEMENTDATE")]
        public DateTimeOffset Settlementdate { get; set; }

        [JsonProperty("REGIONID")]
        public string Regionid { get; set; }

        [JsonProperty("REGION")]
        public string Region { get; set; }

        [JsonProperty("RRP")]
        public double Rrp { get; set; }

        [JsonProperty("TOTALDEMAND")]
        public double Totaldemand { get; set; }

        [JsonProperty("PERIODTYPE")]
        public string Periodtype { get; set; }

        [JsonProperty("NETINTERCHANGE")]
        public double Netinterchange { get; set; }

        [JsonProperty("SCHEDULEDGENERATION")]
        public double Scheduledgeneration { get; set; }

        [JsonProperty("SEMISCHEDULEDGENERATION")]
        public double Semischeduledgeneration { get; set; }
    }
}
TimothyP
  • 21,178
  • 26
  • 94
  • 142