Home > OS >  Map Dynamic Json Key Value pair without dynamic or dictionary c#
Map Dynamic Json Key Value pair without dynamic or dictionary c#

Time:12-05

I have a json like below

{
    "date": "2021-12-04",
    "SMIFUND": {
        "ACC": 5.7299,
        "TATA": 5.155546,
        "RELIANCE": 108.779225
    }
}

now

SMIFUND ->  (input parameter to get this json result)
ACC, TATA , RELAINCE  ->  dynamic 

Now as part of assignment I have to deserialize using Newtonsoft.json and cant use Dictionary<string,float> or dynamic but directly deserialize to either the whole json or the SMIFUND section .

My class structure below

public class Broker
    {
        public string Date { get; set; }
        public List<Fund> funds{ get; set; }
    }

public class Fund
   {
       public string StockName{ get; set; }
       public float Price{ get; set; }
   }

I am able to break the result in two parts

        JObject jsonObject = JObject.Parse(result);

        brokerObject.Date = (string)jsonObject["date"];

but when I try to do List conversion to Fund , it gives a name value deserialization error

var fundsList = JsonConvert.DeserializeObject<List<Fund>>(jsonObject[$"{input}"].ToString());

{input} is SMIFUND

CodePudding user response:

In general not using a Dictionary here would be a bad idea and not practical (IMO), but I assume this is for a school assignment, so this would be the way I would try to approach this.

In you example you are already using JObject which does work similar to a Dictionary so you could use it further like this:

public class Broker
{
    public string Date { get; set; }
    public List<Fund> funds{ get; set; }
}

public class Fund
{
   public string StockName{ get; set; }
   public decimal Price{ get; set; }
}

var input = "SMIFUND"; 
var json = "{\"date\": \"2021-12-04\", \"SMIFUND\": { \"ACC\": 5.7299, \"TATA\": 5.155546, \"RELIANCE\": 108.779225    }}";
var jsonObject = JObject.Parse(json);
    
var brokerObject = new Broker() {funds = new List<Fund>()};     
brokerObject.Date = (string)jsonObject["date"];
    
var fundsList = JsonConvert.DeserializeObject<JObject>(jsonObject[$"{input}"].ToString());
foreach (var x in fundsList)
{
    var fund = new Fund();
    fund.StockName = x.Key;
    fund.Price = Decimal.Parse(x.Value.ToString()); 
    brokerObject.funds.Add(fund); 
}

So basically instead of deserializing into a list directly, you deserialize into a JObject and iterate through that in the same way you would a Dictionary.

Assuming it is allowed to use JObject.

CodePudding user response:

try this

    var jsonDeserialized=JsonConvert.DeserializeObject<BrokerD>(json);

    var result = new Broker
    {
        Date = jsonDeserialized.date,
        funds = jsonDeserialized.SMIFUND.Select(s => new Fund { StockName = s.Key, Price = (float)Convert.ToDouble(s.Value) }).ToList()
    };

public class BrokerD
{
    public string date { get; set; }
    public Dictionary<string, string> SMIFUND { get; set; }
}

but if you want it to do a weird way, use this code

var jsonObject = JObject.Parse(json);
    
    var result = new Broker
    {
        Date = jsonObject["date"].ToString(),
        funds = ((JObject)jsonObject["SMIFUND"]).Properties()
        .Select(s => new Fund { 
        StockName = s.Name, Price = (float)Convert.ToDouble(s.Value)
        }).ToList()
    };

result

{
  "Date": "2021-12-04",
  "funds": [
    {
      "StockName": "ACC",
      "Price": 5.7299
    },
    {
      "StockName": "TATA",
      "Price": 5.155546
    },
    {
      "StockName": "RELIANCE",
      "Price": 108.77923
    }
  ]
}
  • Related