Extracting a value from Json object in C# -
i have json document , trying value analoginput each channel 1 4. have tried code:
jobject originalobject = jobject.parse(testjsonobject); var analoginputtruevalues = originalobject.descendants().oftype<jproperty>().where(p => p.name == "digitalinput").select(x => x.value).toarray();
where testjsonobject json file gets loaded method.
debugging code, value analoginputtruevalues is:
{newtonsoft.json.linq.jtoken[4]} [0]: {13} [1]: {13} [2]: {14} [3]: {14}
,which correct. interested have array or list {"13","13","14","14"}. can not move forwards since can not extract exact values , have them in list or array. when do:
digitalinputtruevalues.getvalue(0) {13} base: {13} hasvalues: false type: string value: "13"
i can't extract value, interested in. how can around kind of problem , extract desired values? object working follows:
{ "module": { "serial": "3", "label": "a", "lat": "b", "long": "c", "channels": [ {"channel": "1", "label": "channel 1", "analoginput": "13", "analoginputraw": "13", "analoginputscale": "raw", "digitalinput": "off"}, {"channel": "2", "label": "channel 2", "analoginput": "13", "analoginputraw": "13", "analoginputscale": "raw", "digitalinput": "on"}, {"channel": "3", "label": "channel 3", "analoginput": "14", "analoginputraw": "14", "analoginputscale": "raw", "digitalinput": "on"}, {"channel": "4", "label": "channel 4", "analoginput": "14", "analoginputraw": "14", "analoginputscale": "raw", "digitalinput": "on"} ], "variables": [ {"1": "0"}, {"2": "0"}, {"3": "1"}, {"4": "0"} ] } }
you need include tostring()
in select
look after x.value
:
jobject originalobject = jobject.parse(json); var analoginputtruevalues = originalobject.descendants() .oftype<jproperty>() .where(p => p.name == "analoginput") .select(x => x.value.tostring()) .toarray();
working example: https://dotnetfiddle.net/tu5mc8
alternative method using strongly-typed classes: https://dotnetfiddle.net/us4bs0
c# json json.net
No comments:
Post a Comment