Home > database >  C# Json Convert any dynamic object to key value pairs not class
C# Json Convert any dynamic object to key value pairs not class

Time:12-16

i have a Json

{
   "test1.png": "123",
   "image.png": "456",
   "pdffile.pdf": "789"
}

how can i convert to C# dictionary or table

CodePudding user response:

How about this?

string serializedDic = @"{
   ""test1.png"": ""123"",
   ""image.png"": ""456"",
   ""pdffile.pdf"": ""789""
}";

Dictionary<string, string> dict = 
  JsonSerializer
  .Deserialize<Dictionary<string, string>>(serializedDic);

CodePudding user response:

Use Newtonsoft Json library.

First, create your own class, with 3 properties name it as you want. Add JsonPropertyAttribute with name exactly same as 3 json property.

Then, just Deserialize your json file to get your object.

CodePudding user response:

Use Newtonsoft Json library

string json = @"{
    "test1.png": "123",
    "image.png": "456",
    "pdffile.pdf": "789"
}";

var dic = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
  • Related