Dynamic json object with numeric keys

I have a json object that I converted to a dynamic C # object using this answer. It works fine, but the problem is that this object has numeric keys. For instance,

var jsStr = "{address:{"100": {...}}}"; 

So i cant wirte

 dynObj.address.100 

And, as I know, I cannot use indexers to get this object, like this

 dynObj.address["100"] 

Please explain to me how I can do this.

+3
source share
3 answers

As far as I can see from the source code, it resolves the properties through a private dictionary, so you need to use reflection to access the dictionary key or slightly change its code so that TryGetMember in DynamicJSONObject is as follows (and use __numeric__ to get the key, for example data.address. __ numeric__100, and then do not use __numeric__ as a key):

 public override bool TryGetMember(GetMemberBinder binder, out object result) { var name = binder.Name; //Code to check if key is of form __numeric__<number> so that numeric keys can be accessed if (binder != null && binder.Name != null && binder.Name.StartsWith("__numeric__")) { name = binder.Name.Substring(11); } if (!_dictionary.TryGetValue(name, out result)) { // return null to avoid exception. caller can check for null this way... result = null; return true; } var dictionary = result as IDictionary<string, object>; if (dictionary != null) { result = new DynamicJsonObject(dictionary); return true; } var arrayList = result as ArrayList; if (arrayList != null && arrayList.Count > 0) { if (arrayList[0] is IDictionary<string, object>) result = new List<object>(arrayList.Cast<IDictionary<string, object>>().Select(x => new DynamicJsonObject(x))); else result = new List<object>(arrayList.Cast<object>()); } return true; } 
+2
source

My view of openource ImpromptuInterface has methods for invoking dynamic members using the string name of any dynamic C # 4 object.

 object tOut =Impromptu.InvokeGet(dynObj.address,"100"); 

I tested it with ExpandoObject , it seemed to work fine.

+1
source

The identifier must begin with a letter, underscore (_) or dollar sign ($); subsequent characters may also be digits (0-9). Since JavaScript is case-sensitive, letters include the characters ā€œAā€ to ā€œZā€ (upper case) and the characters ā€œaā€ through ā€œzā€, (Lowercase). Starting with JavaScript 1.5, ISO 8859-1, or Unicode letters (or Unixode \ uXXXX escape sequences) can be used in identifiers.

Quote from: http://en.wikipedia.org/wiki/JavaScript_syntax#Variables

Oh, I'm sorry I misunderstood the question, well, here you go with a working example that you can adapt to your needs:

 <script> var jsStr = {address:{'100': 'test'}}; var test = jsStr.address; console.log(test); alert(test[100]); </script> 

btw key MAY be numeric (as you see in the example in the answer), only identifiers cannot. so you need to access, just like you. you just need to leave quotes for the number keys! and your json string will not be an object without evaluation, so in this example its strictly speaking a javascript object and not json, but it does not matter to the subject

0
source

All Articles