How to parse json data with dynamic keys and values

john zyd 421 Reputation points
2023-01-18T22:39:11.07+00:00
Hello:
I have a json string, I want to parse with C#.

{
	"code": "0",
	"msg": "Success",
	"data": {
		"lastUpdatedAt": 1673852258000,
		"symbols": {
			"AGLD": {
				"withdrawal_decimals": 8,
				"fullName": "Adventure Gold",
				"networks": [{
					"minWithdrawalAmount": "10",
					"network": "ETH"
				}, {
					"minWithdrawalAmount": "0.4",
					"network": "CRONOS"
				}]
			},
			"MATIC": {
				"withdrawal_decimals": 8,
				"fullName": "MATIC",
				"networks": [{
					"minWithdrawalAmount": "50",
					"network": "ETH"
				}, {
					"minWithdrawalAmount": "1",
					"network": "CRONOS"
				}, {
					"minWithdrawalAmount": "0.02",
					"network": "MATIC"
				}]
			},
			"APT": {
				"withdrawal_decimals": 8,
				"fullName": "Aptos",
				"networks": [{
					"minWithdrawalAmount": "0.018",
					"network": "APT"
				}]
			}
		}
	}
}



I want to parse the above string and put the results into a dictionary, the keys are the symbol names, the values of dictionary are a list of minWithdrawalAmount and networks.
Like: Key1 = "AGLD", List<string> Value1 = ("network" = "ETH", "minWithdrawalAmount" = "10"; 
"network" = "CRONOS", "minWithdrawalAmount" = "0.4")
Key2 = "MATIC", List<string> Value1 = ("network" = "ETH", "minWithdrawalAmount" = "50"; 
"network" = "CRONOS", "minWithdrawalAmount" = "1"; "network" = "MATIC", "minWithdrawalAmount" = "0.02")
etc.

Please advise,
Thanks,

Developer technologies C#
0 comments No comments
{count} vote

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2023-01-19T03:21:48.56+00:00

    @john zyd , Welcome to Microsoft Q&A, you could try to install nuget-package Newtonsoft.Json to copy your data to Dictionary.

    Here is a code example you could refer to.

     static void Main(string[] args)
            {
                var jsonstr = File.ReadAllText("1.json");
                Dictionary<string, List<NetWork>> dic = new Dictionary<string, List<NetWork>>();
                JObject stuff = JObject.Parse(jsonstr);
                var result = stuff.SelectTokens("data.symbols");
                foreach (JProperty item in result.Children())
                {
                    Console.WriteLine(item.Name);
                    List<NetWork> works = new List<NetWork>();
                    foreach (var re in item.Value["networks"])
                    {
                        NetWork net = new NetWork();
                        net.network = re["network"].ToString();
                        net.minWithdrawalAmount = Convert.ToDouble(re["minWithdrawalAmount"]);
                        works.Add(net);
                    }
                    dic.Add(item.Name, works);
                    
                }
    
        
               
            }
    

    Please don't forget we need to define a class called NetWork:

        public class NetWork
        {
            public double minWithdrawalAmount { get; set; }
            public string network { get; set; }
        }
    
    

    Best Regards,

    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.