0

I am receiving a GET from all users and in some users I receive $ref with a number, and I want to get all the information like the first object, if I do a get of one user I receive the information correctly

Here is the GET from the API

 // GET: api/usuaris
    public IQueryable<usuari> Getusuaris()
    {
        return db.usuaris;
    }

And here what returns me back

JSON

  • There is not enough information to tell. Please show the code client and server side... Apparently instead of the intended JSON, some reference is being parsed and turned into JSON (either server side or badly parsed client-side) – Attersson May 13 '18 at 14:30
  • As far as JavaScript is concerned, it is a property name like any other. There is nothing special about the $ character. – Quentin May 13 '18 at 14:31
  • @Quentin But then how I can take the name of second object, if only shows me the $ref? –  May 13 '18 at 14:39
  • @Attersson Did it but there isn't much to show –  May 13 '18 at 14:42
  • It could be client sided or server sided. Or perhaps the db records are invalid except the first user.... there are too many possibilities. – Attersson May 13 '18 at 14:48

1 Answers1

0

This looks like .Net objects serialized using Json.NET with PreserveReferencesHandling.Objects enabled.

Based on the screenshot I would say the real objects behind those $refs are defined in the realitzas array or deeper in the hierarchy. Assuming you're employing C#, you shouldn't care much about these internals -- just invoke JsonConvert.DeserializeObject and get the whole hierarchy back.

Dmitry Egorov
  • 9,542
  • 3
  • 22
  • 40
  • Hi @DmitryEgorov, thanks for answer but I want a JSON return and how I can invoke JsonConvert.DeserializeObject, in the controller? –  May 13 '18 at 18:59
  • Or how I can disable PreserveReferencesHandling.Objects? –  May 13 '18 at 19:13
  • @Lluís, I suggested to invoke `JsonConvert.DeserializeObject` on the client side actually. On the controller you need to reset `PreserveReferencesHandling` serializer setting to `PreserveReferencesHandling.None`. If it's an ASP.Net Core project, tweak it with `AddJsonOptions` in `ConfigureServices`: `services.AddMvc().AddJsonOptions(opt => opt.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None);`. – Dmitry Egorov May 14 '18 at 02:22
  • For older WebAPI version you me do something similar changes in [`Application_Start`](https://stackoverflow.com/questions/13274625/how-to-set-custom-jsonserializersettings-for-json-net-in-mvc-4-web-api) – Dmitry Egorov May 14 '18 at 02:22
  • Thanks but how I can Desearialize the object in the Postman or Android, sorry if sound stupid but idk how to do –  May 14 '18 at 10:07
  • I'm afraid there is no easy way to deserialize such `$id`/`$ref` featured JSON on non-.NET clients. You'll have to resort to `PreserveReferencesHandling.None` on the server to make your JSON platform agnostic. – Dmitry Egorov May 14 '18 at 10:54