Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing properties with a dot in their name

Tags:

json

c#

json.net

I am trying to deserialize JSON. My root object has a single property "en.pickthall". I am using dynamic type for reading my JSON. I thought I could just do away with "." in the property since its a local JSON file but then there must be some way to access such a property

var result = App_Code.FileIOHelper.ReadFromDefaultFile("ms-appx:///Assets/en.pickthall.json");

dynamic stuff = JsonConvert.DeserializeObject(result);

foreach(var x in stuff.(en.pickthall)) //Tried this intellisense didn't like it
{

}
like image 581
Rahul Jha Avatar asked Apr 02 '16 17:04

Rahul Jha


1 Answers

You could create a root class to deserialize into and use JsonProperty

public class Root
{
    // Use the proper type instead of object
    [JsonProperty(PropertyName = "en.pickthall")]
    public IEnumerable<object> EnPickthall { get; set; } 

    public Root() { }
}

Used as follows

Root stuff = JsonConvert.DeserializeObject<Root>(result);

foreach(var x in stuff.EnPickthall)
{

}
like image 51
Arthur Rey Avatar answered Oct 22 '22 08:10

Arthur Rey