json - Deserializing links collection to a dictionary -
i'm trying deserialize output rest api call. fragment of json here:
"_links": { "invoices": { "href": "url", "type": "application/json" }, "members": { "href": "otherurl", "type": "application/json" }, "paymentcard": { "href": "yetanotherurl", "type": "application/json" }, "self": { "href": "evenmoreurl", "type": "application/json" }, "session": { "href": "andyesevenmoreurl", "type": "application/json" }, "subscription": { "href": "tiredofurls", "type": "application/json" } }
i deserialize dictionary of following class:
public class link { public string name {get; set;} public string href {get; set;} public string type {get; set;} }
the dictionary
dictionary<string, link> = new dictionary<string, link>();
where name property key
i'm trying use newtonsoft's json serializer.
is possible?
yes, can dictionary want this:
dictionary<string, link> dict = jobject.parse(json) .selecttoken(".._links") .children<jproperty>() .todictionary(jp => jp.name, jp => new link() { name = jp.name, href = (string)jp.value["href"], type = (string)jp.value["type"] });
here, using json.net's linq-to-json api parse json jobject
. use selecttoken
method jsonpath expression recursively find first node called "_links" anywhere in json. there, child properties , convert dictionary of links described.
fiddle: https://dotnetfiddle.net/hbjm51
Comments
Post a Comment