c# - Change Value in JSON -


i have following json:

{ "sonos": [     {       "192.168.10.214": [         {           "volume": "5",           "extension": null,           "name": "wohnzimmer"         }       ]     },     {       "192.168.10.204": [         {           "volume": "5",           "extension": null,           "name": "büro"         }       ]     }   ] } 

on click want change volume, try snippet:

string[] address = ip.text.split(new string[] {" | "},      stringsplitoptions.removeemptyentries); //ip listviewitem ... dynamic jsonobj = jsonconvert.deserializeobject(config); jsonobj["sonos"][address[0]]["volume"] = "10"; 

now last line throws

accessed jarray values invalid key value: "192.168.10.214". int32 array index expected.

how update volume specified ip? using newtonsoft.json;

any hint appreciated!

edit

full function per request:

private void incomingcall()         {             int = 0;             foreach (listviewitem ip in sonoslistext1.items)             {                 string[] address = ip.text.split(new string[] {" | "}, stringsplitoptions.removeemptyentries);                  /* current volume , save */                 string getvol = sendxml("/mediarenderer/renderingcontrol/control", "getvolume",                     "urn:schemas-upnp-org:service:renderingcontrol:1",                     "<instanceid>0</instanceid><channel>master</channel>", address[0]);                  xmldocument xmlresponse = new xmldocument();                 xmlresponse.loadxml(getvol);                 var curvol = ((xmlelement) xmlresponse.firstchild.firstchild.firstchild).innertext;                  string config = file.readalltext(appdomain.currentdomain.basedirectory + "cfg\\config.json");                 dynamic jsonobj = jsonconvert.deserializeobject(config);                 jsonobj["sonos"][i][address[0]][i]["volume"] = curvol;                 string output = jsonconvert.serializeobject(jsonobj, newtonsoft.json.formatting.indented);                 file.writealltext(appdomain.currentdomain.basedirectory + "cfg\\config.json", output);                  /* lower volume */                 sendxml("/mediarenderer/renderingcontrol/control", "setvolume",                     "urn:schemas-upnp-org:service:renderingcontrol:1",                     "<instanceid>0</instanceid><channel>master</channel><desiredvolume>2</desiredvolume>", address[0]);                  /* var */                 i++;             }         } 

"sonos" array subitems , values.

so access follows:

jsonobj["sonos"][0][address[0]][0]["volume"] = "10"; 

edit:

for foreach iteration, consider similar (hacky if structure guaranteed should work shrug):

foreach (listviewitem ip in sonoslistext1.items) {     var index = sonoslistext1.items.tolist().indexof(ip);     string[] address = ip.text.split(new string[] {" | "}, stringsplitoptions.removeemptyentries);     /* --- excluded xml manipulation code --- */     jsonobj["sonos"][index][address[0]][0]["volume"] = "10";   } 

ideally should change sonos json dictionary address values key.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -