c# - How can I make method dynamically load key/value pairs? -


how can take code , make more dynamic?

right have post json as

{ values: [{key: 'mykey', value: 'myvalue'}] } 

ideally post json like:

    { values: [{make: 'car', color: 'red'}] } or     { values: [{firstname: 'george', lastname: 'constanza'}] } 

so tied using key/value in json.
how can dynamically load values without having use key/value?

public class myperformercontroller : apicontroller {     [httpput("update/{mypath}")]     public task<iactionresult> dosomething(string mypath, [frombody]mycollection values)     {         foreach (var val in value)         {             var keyvalue = val.key;             var somevalue = val.value;         }     } }  public class mycollection : ienumerable<keyvaluepair<string,string>> {      public ienumerable<keyvaluepair<string, string>> values { get; set; }      public ienumerator<keyvaluepair<string, string>> getenumerator()     {         return values.getenumerator();     }      ienumerator ienumerable.getenumerator()     {         return getenumerator();     } } 

if had structure:

[{firstname: 'george', lastname: 'constanza'}] 

you can convert jarray using jsonconvert.deserializeobject<jarray>(json);

for instance, having json:

[     {         firstname: "george",         lastname: "constanza"     }, {         firstname: "mike",         lastname: "passant"     }, {         firstname: "john",         lastname: "connor"     } ] 

you can iterate , key , value:

jarray jsonarray = jsonconvert.deserializeobject<jarray>(json);  foreach (jobject item in jsonarray.children<jobject>()) {     foreach (jproperty element in item.properties())     {         console.writeline(string.format("key: {0}", element.name));         console.writeline(string.format("value: {0}", element.value.tostring()));     } } 

here can see demo.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -