I have a JSON value like this and I need help specifically in putting the Value of D variable in a keyValuepair.
{"A":727657,"B":"72.74:hello","C":"Http","D":"Value:0,Value1:79,Value2:56,Value3:45","E":0,"F":2}
I am parsing this value like this.
MyJSONContructor MyJSONContructorObject = Newtonsoft.Json.JsonConvert.DeserializeObject<MyJSONContructor>(JSONValue);
I have a constructor like this.
public MyJSONContructor(int A, string B, string C, string D, bool E,int F)
{
this.A = A;
this.B = B;
this.C = C;
this.D = D;
this.E = E;
this.F = F;
}
What I need help with is the value in variable D, if you notice it has several values inside it, I want to be able to put them in a keyValuePair, something like this
List<KeyValuePair<string, string>> contentsToPost
Is there an easy way to add them to the List of KeyValuePairs, since we would not know how many key value pairs would be coming in D in Json, I am trying to avoid doing something like this.
var list = new List<KeyValuePair<string, string>>() {
new KeyValuePair<string, string>("Value", 0),
new KeyValuePair<string, string>("Value1", 79),
If you're comfortable with LINQ, you can do it like this:
List<KeyValuePair<string, string>> kvps =
(from pair in MyJSONContructorObject.D.Split(',')
let kv = pair.Split(':')
select new KeyValuePair<string, string>(kv[0], kv[1])
).ToList();
Fiddle: https://dotnetfiddle.net/HHti3s