0

I need to Parse and build dynamic key value json model . in every time key value is different

{"product1" :"batch1,batch2","xyzproduct" : "batc265",....,  "productn" : "batch12"}

here productn not fixed its any thing value . can someone help how to do in c# or using newtonsoft lib

1 Answers1

1
  1. use dictionary
var prodDict = JsonConvert.DeserializeObject<Dictionary<string,string>>(json);

string product1 = prodDict["product1"];
  1. use dynamic
var dict = JsonConvert.DeserializeObject<Dictionary<string,object>>(json);

dynamic products = dict.Aggregate(new ExpandoObject() as IDictionary<string, object>,
                                (a, p) => { a.Add(p); return a; });
string product1 = products.product1;
Serge
  • 40,935
  • 4
  • 18
  • 45