c# - Update JSON object using path and value -


i have csv file contains paths , values in following format:

path;value prop1.prop2.1;hello prop1.prop2.2;world prop1.prop2.3;! prop1.prop3.test;hi prop1.prop4;value 

and wand json:

{   "prop1": {     "prop2": {       "1": "hello",       "2": "world",       "3": "!"     }     "prop3": {       "test": "hi"     }     "prop4": "value"   }     } 

i've parsed csv file this:

dictionary<string, string> dict = new dictionary<string, string>(); while (csv.read()) {   string path = csv.getfield<string>(0);   string value = csv.getfield<string>(1);   dict.add(path, value); } 

could me method, create json dictionary using json.net library. of course properties in original file can different.

you can use function json string dictionary

public static string buildjson(dictionary<string, string> dict) {     dynamic expando = new expandoobject();      foreach (keyvaluepair<string, string> pair in dict.where(x => !string.equals(x.key, "path")))     {         string[] patharray = pair.key.split('.');         var currentexpando = expando idictionary<string, object>;          (int = 0; < patharray.count(); i++)         {             if (i == patharray.count() - 1)             {                 currentexpando.add(patharray[i], pair.value);             }             else             {                 if (!currentexpando.keys.contains(patharray[i]))                 {                     currentexpando.add(patharray[i], new expandoobject());                 }                 currentexpando = currentexpando[patharray[i]] idictionary<string, object>;             }         }     }      jobject o = jobject.fromobject(expando);     return o.tostring(); } 

you need add using system.dynamic;


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -