Created
March 28, 2024 05:02
-
-
Save normanlmfung/329c06ffad0bd12baf34c79d17da47b3 to your computer and use it in GitHub Desktop.
csharp_json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using Newtonsoft.Json; // dotnet add package Newtonsoft.Json | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// Read JSON order book from file | |
string jsonOrderBook = File.ReadAllText("order_book.json"); | |
// Deserialize JSON to C# object | |
OrderBook orderBook = JsonConvert.DeserializeObject<OrderBook>(jsonOrderBook); | |
// Convert to Python-like dictionary structure | |
Dictionary<string, Dictionary<string, List<decimal>>> pythonDict = new Dictionary<string, Dictionary<string, List<decimal>>>(); | |
foreach (var pair in orderBook.Pairs) | |
{ | |
Dictionary<string, List<decimal>> bidsAndAsks = new Dictionary<string, List<decimal>>(); | |
bidsAndAsks.Add("bids", pair.Value.Bids); | |
bidsAndAsks.Add("asks", pair.Value.Asks); | |
pythonDict.Add(pair.Key, bidsAndAsks); | |
} | |
// Output Python-like dictionary structure | |
foreach (var pair in pythonDict) | |
{ | |
Console.WriteLine(pair.Key + ":"); | |
foreach (var item in pair.Value) | |
{ | |
Console.WriteLine(" " + item.Key + ":"); | |
foreach (var price in item.Value) | |
{ | |
Console.WriteLine(" " + price); | |
} | |
} | |
} | |
} | |
} | |
class OrderBook | |
{ | |
public Dictionary<string, OrderBookPair> Pairs { get; set; } | |
} | |
class OrderBookPair | |
{ | |
public List<decimal> Bids { get; set; } | |
public List<decimal> Asks { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment