Accessing properties with a dot in their name

前端 未结 5 1461
春和景丽
春和景丽 2020-12-03 18:57

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 \".\

相关标签:
5条回答
  • 2020-12-03 19:03

    You could serialize not to dynamic but to JObject and then access your property via

    JObject stuff = JsonConvert.DeserializeObject<JObject>(Jsonstring);
    var x = stuff.Value<String>("my.property")
    
    0 讨论(0)
  • 2020-12-03 19:09

    I know you said you were using a dynamic type for your JSON deserialization, but I just wanted to point out that there is a .NET RESTful client out there that supports this with static model definitions too. For you or for anyone else who happens upon this response when searching for an answer to their problems with dots in property names in C# REST calls.

    As of the newly released RestSharp 106.1.0 (and I do mean this version because this support was just added), it can handle renaming properties with a dot in their name via the DeserializeAs attribute. An example being when I call the ElasticSearch API for a _cat call with the following model:

    public class CatResponse
    {
        public string index { get; set; }
        ...
        [DeserializeAs(Name = "docs.count")]
        public string docscount { get; set; }
    }
    

    And actually get back the docs.count property deserialized into docscount now:

     var resource = $"_cat/indices/{indexPattern}?format=json&pretty=true";
    
     var request = new RestRequest(resource, Method.GET);
     var response = client.Execute<List<CatResponse>>(request);
    

    This support is out of the box and doesn't need to use the Newtonsoft.Json.JsonSerializer which I have also heard is a possible solution to this problem but which I couldn't get to work.

    0 讨论(0)
  • 2020-12-03 19:13

    C# doesn't have any way of quoting identifiers. If it's not a valid identifier, your only option is reflection.

    However, it's possible the object returned by your JSON deserializer changed the identifiers to make them useable in C# - you might want to enumerate all the properties to check if that is the case. A dynamic object with indexers might also be a solution (allowing e.g. stuff["en.pickthall"]).

    Another alternative is to change the way the serializer maps properties. For example, Newtonsoft.Jsoft allows you to customize this using a IContractResolver. It's quite easy to replace the . for something more C#-sane in this way.

    0 讨论(0)
  • 2020-12-03 19:15

    With a dynamic object and NewtonSoft.Json:

    dynamic json = JValue.Parse(result);
    
       int Opens = Convert.ToInt32(json.opens);
    
       int Clicks = Convert.ToInt32(json.clicks);
    
       string State = json.state;
    
    0 讨论(0)
  • 2020-12-03 19:18

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

    public class Root
    {
        [JsonProperty(PropertyName = "en.pickthall")]
        public object EnPickthall { get; set; }
    
        public Root() {}
    
        public Root(en_pickthall)
        {
            this.EnPickthall = en_pickthall;
        }
    }
    

    You can then call

    Root stuff = JsonConvert.DeserializeObject<Root>(result);
    
    foreach(var x in stuff.EnPickthall)
    {
    
    }
    
    0 讨论(0)
提交回复
热议问题