c# - How to get object json when deserializing array -
i have incoming json, consists array of objects, say, foo
. deserialize them with
result = jsonconvert.deserializeobject<list<foo>>(message);
now want add string property foo, store it's json (which received), foo'll like:
public class foo { public int myint { get; set; } public bool mybool { get; set; } public string json { get; set; } }
but don't know how can json.net way can populate such field..
upd i'll clearify want. receive json:
[{"myint":1,"mybool":0},{"myint":2,"mybool":0},{"myint":3,"mybool":1}]
here array of 3 objects , want, when deserializing, add corresponding part of json object, that:
first object contain {"myint":1,"mybool":0}
second object contain {"myint":2,"mybool":0}
third object contain {"myint":3,"mybool":1}
in json
property
i'll gratefull help!
this 1 way it, doesn't maintain exact original json - provide static record of original json (but without exact format of original values - i.e. bool maybe 0/1 or true/false):
var message = @"[{""myint"":1,""mybool"":0},{""myint"":2,""mybool"":0},{""myint"":3,""mybool"":1}]"; var foos = jsonconvert.deserializeobject<list<foo>>(message); var t = jsonconvert.serializeobject(foos[0]); foos = foos.select(s => new foo() { mybool = s.mybool, myint = s.myint, json = jsonconvert.serializeobject(s) }).tolist();
if dealing lot of foos, might want find more efficient way. there might way 'update' using linq, rather creating new list.
Comments
Post a Comment