Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do C# classes deal with dollar signs in JSON?

I'm getting a JSON feed from Google's data API and a lot of the property names start with a $ character (dollar sign).

My problem is that I can't create a C# class with a variable name starting with a dollar sign, it's not allowed by the language. I'm using JSON.NET from Newtonsoft to convert JSON to C# objects. How can I get around this problem?

like image 491
Johnathan Sewell Avatar asked Jan 09 '11 09:01

Johnathan Sewell


People also ask

How do while loop works in C?

Syntax. do { statement(s); } while( condition ); Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop executes once before the condition is tested. If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop executes again.


2 Answers

You could try using the [JsonProperty] attribute to specify the name:

[JsonProperty(PropertyName = "$someName")]
public string SomeName { get; set; }
like image 79
Darin Dimitrov Avatar answered Sep 21 '22 19:09

Darin Dimitrov


firas489 was on the right track that $ indicates metadata, not an actual data field. However the fix is actually to do this:

JsonSerializerSettings settings = new JsonSerializerSettings();
settings.MetadataPropertyHandling = MetadataPropertyHandling.Ignore;            

Set the metadata handling to ignore, and then you can serialize/deserialize the property using the PropertyName attribute:

[JsonProperty("$id")]
public string Id { get; set; }
like image 32
Greg Ennis Avatar answered Sep 23 '22 19:09

Greg Ennis