How to deserialize Newtonsoft Json.NET references to separate, individual instances

前端 未结 2 1012
借酒劲吻你
借酒劲吻你 2021-01-13 05:59

I have a piece of JSON that looks like this:

[
  {
    \"$id\": \"1\",
    \"Name\": \"James\",
    \"BirthDate\": \"1983-03-08T00:00Z\",
    \"LastModified\         


        
2条回答
  •  北海茫月
    2021-01-13 06:40

    I had problem with $ref deserialization. PreserveReferencesHandling helps only if you have metadata located at the beginning of the request/response. Otherwise I recommend using those:

    var settings = new JsonSerializerSettings();
    settings.PreserveReferencesHandling = PreserveReferencesHandling.Objects; 
    settings.MetadataPropertyHandling = MetadataPropertyHandling.Ignore; //ign
    var deserializedObjects = Newtonsoft.Json.JsonConvert.DeserializeObject(requestResult.Content, settings);
    

    Regarding to https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_MetadataPropertyHandling.htm

    Default 0   Read metadata properties located at the start of a JSON object.
    ReadAhead   1   Read metadata properties located anywhere in a JSON object. Note that this setting will impact performance.
    Ignore  2   Do not try to read metadata properties.
    

提交回复
热议问题