4

I'm working with a Json dynamic object.

Here's what i'm using to get data out of the object:

string = obj.item.today.price;

This works fine, the problem is that as soon as I have to start using numbers example :

string = obj.daily.10000;

It gives me an error

Is there any way to fix this?

3
  • can you please put code Commented Apr 23, 2013 at 9:08
  • 1
    can you name a variable like: var 1000 = 'ABC'; ??? Commented Apr 23, 2013 at 9:11
  • 2
    It's not unusual for JSON libraries to support indexing syntax like obj.daily["10000"]. Have you tried that? Commented Apr 23, 2013 at 9:14

2 Answers 2

3

That's not possible to "call 10000 on daily object" just because 10000 is NOT a valid identifier.

Let me explain what is going on here:

JSON parser generates some runtime type, inherited from some base JSON type (e.g JsonObject). So, obj is some generated type, you call property item on it, it returns similar generated type, then you call today property and so on.

The last step is weird, there cannot be 10000 property on any type, generated or not.

But, If library supports key-value access to objects, you can try to write

obj.daily["10000"]

or cast obj to JObject (assume you are using JSON.NET) and call Property method:

var jsonObject = (JObject) obj;
var propertyValue = jsonObject.Property("10000").Value;
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah I tried obj.daily["10000"], still giving me identifier error. But how would it be possible then to Deserialize the json code if all the names are numbers?
@Tom library can maintain internal dictionary inside deserialized object and hold properties there.
1

If you using Json.NET

string json = "{ dayly : { 1000 : 'asd' } }";
dynamic d = JsonConvert.DeserializeObject(json);
Console.WriteLine((d.dayly as JObject).Property("1000").Value);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.