0

How to Deserialize this Json Data in asp.net C#.

{"USD_PHP":{"2016-12-31":49.560001},"PHP_USD":{"2016-12-31":0.020178}}

Here the USD_PHP and PHP_USD is going to be always unique, the Date 2016-12-31 is also unique for every date value. All i want to get the Currency Conversion Rate (49.560001, and 0.020178) in my application. How can i Build the program which is having no Common Key Field?

Source

Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
Amandeep Kochhar
  • 91
  • 1
  • 1
  • 3
  • 2
    Stack Overflow is not a free code writing service. You are expected to try to write the code yourself. After [doing more research](http://meta.stackoverflow.com/questions/261592) if you have a problem you can post what you've tried with a clear explanation of what isn't working and providing a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). See [How to Ask](http://stackoverflow.com/questions/how-to-ask). Also, be sure to take the [tour](http://stackoverflow.com/tour) – Roman Marusyk Dec 08 '17 at 10:28
  • 1
    Look at [JSON.NET](https://www.newtonsoft.com/json) lib. – BWA Dec 08 '17 at 10:31
  • Copy the Json, then check the Edit menu in Visual Studio, it should have a special paste option for pasting as json classes. If the keys differ then you are usually dealing with a dictionary. – Lasse V. Karlsen Dec 08 '17 at 10:32
  • 1
    *opens google and enters: `foreach property in JObject`*. Would my [first hit](https://stackoverflow.com/questions/10543512/how-do-i-enumerate-through-a-jobject) help you? – Arno Dec 08 '17 at 10:32
  • MegaTron brother, I did a lot of research regarding this, But i wasn't able to get an answer which include no common Key within json String. – Amandeep Kochhar Dec 08 '17 at 12:47

2 Answers2

0
var req = new RestRequest("api url"); // RestSharp.dll
var client = new RestClient("host url");
var response = client.ExecuteTaskAsync(req).Result.Content;
var json =((dynamic)JsonConvert.DeserializeObject(response))
 //Newtonsoft.Json.dll
var dic = new Dictionary<string, List<Tuple<string, string>>>();
foreach (var item in json)
{
  var items = new List<Tuple<string, string>>();
  foreach (var value in item.Value)
  {
       var newItem = new Tuple<string, string>(value.Name, value.Value.ToString());
       items.Add(newItem);
  }
  if (items.Any())
  {
       dic.Add(item.Name, items);
  }
}

I hope this example will help you.

0

Deserialize to a Dictionary<string, Dictionary<string, decimal>>. The outer key will be the currency conversion name, the inner key will be the date, and the value of the inner will be the conversion rate. This will work with either JavaScriptSerializer or Json.Net.

string json = @"{""USD_PHP"":{""2016-12-31"":49.560001},""PHP_USD"":{""2016-12-31"":0.020178}}";

var jss = new JavaScriptSerializer();

var outer = jss.Deserialize<Dictionary<string, Dictionary<string, decimal>>>(json);

foreach (var kvp in outer)
{
    foreach (var kvp2 in kvp.Value)
    {
        Console.WriteLine(kvp.Key + "  " + kvp2.Key + "  " + kvp2.Value);
    }
}

Output:

USD_PHP  2016-12-31  49.560001
PHP_USD  2016-12-31  0.020178

Fiddle (Json.Net): https://dotnetfiddle.net/L7cvMY

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300