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

前端 未结 3 1783
轻奢々
轻奢々 2020-12-03 17:32

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# clas

3条回答
  •  温柔的废话
    2020-12-03 18:01

    Those items with the dollar sign ($) are usually meant to be metadata and NOT fields. When JSON.NET serializes an object and you tell it to handle the object types, it will insert $ items that denotes metadata for correct deserialization later on.

    If you want to treat the $ items as meta data, use JsonSerializerSettings. For example:

    Dim jsonSettings As New Newtonsoft.Json.JsonSerializerSettings With {.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.All}
    Dim jsonOut As String = Newtonsoft.Json.JsonConvert.SerializeObject(objects, jsonSettings)
    

    The TypeNameHandling = Newtonsoft.Json.TypeNameHandling.All tells JSON to handle the datatypes while relying on the $ for information.

    Hope that helps..

提交回复
热议问题