.Net Linq to JSON with Newtonsoft JSON library -
i have json sent webservice looks this.
{ root: [ { "key": "foo1", "val": "150" }, { "key": "foo2", "val": "220" }, { "key": "foo3", "val": "300" }, { "key": "dataid", "val": "2289" } ] }
say wanted return value of val
key
equal "dataid"
. how using json.net library?
i know can loop through values find object far bigger example here.
thanks in advance
well something going have loop through @ point. if need fetch lots of values key same json, should build dictionary<string, string>
- means looping on once (either explicitly or using linq todictionary
method) having fast access afterwards.
here's sample code:
using system; using system.io; using system.linq; using newtonsoft.json.linq; class test { static void main() { string text = file.readalltext("test.json"); jobject obj = jobject.parse(text); jarray root = (jarray) obj["root"]; var dictionary = root.todictionary(x => (string) x["key"], x => (string) x["val"]); console.writeline(dictionary["dataid"]); } }
Comments
Post a Comment