0

i'm working on .net framework project. I got this weird result when i was trying to convert list object to json string, result was correct at first element enter image description here

But move to second one, result was: enter image description here

I just dont know where $ref: 24 came from. Here is the method that processing list -> json: enter image description here

Pls show me how can i fix this? :((

Mark Score
  • 62
  • 1
  • 6

1 Answers1

1

The PreserveReferencesHandling.Objects setting tells the serializer to serialize objects by reference rather than by value. If you want to see all properties listed for all objects, remove this from your serialization call:

public static string Convert(object data)
{
    return JsonConvert.SerializeObject(
        data, 
        new JsonSerializerSettings { Formatting = Format.Indented });
}
Matthew Jaspers
  • 1,526
  • 1
  • 10
  • 13
  • Seems like this didn't work for me, I got 'Self reference loop detected...' when use this block of code :( – Mark Score Jul 27 '20 at 16:58
  • @HarryStormborn The easiest fix is setting the serializer to ignore reference loops, but it depends on your needs. See https://stackoverflow.com/questions/7397207/json-net-error-self-referencing-loop-detected-for-type – Matthew Jaspers Jul 27 '20 at 17:03