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
  • 1
    Use a Dictionary<string,string>. Commented Aug 18, 2022 at 9:01

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.