0

I am creating service on ASP .NET Core 2.1 and I cant get Json response with class attribute.

This is ShopItem Class:

 [JsonObject(MemberSerialization.OptIn)]
public class ShopItem
{
    [JsonProperty]
    public int id { get; set; }
    [JsonProperty]
    public String ItemName { get; set; }
    [JsonProperty]
    public string ItemQty { get; set; }
    [JsonProperty]
    public int ListId { get; set; }

    public ShopItem()
    {

    }
    [JsonConstructor]
    public ShopItem(int _id, String _ItemName, string _ItemQty, int _ListId)
    {
        id = _id;
        ItemName = _ItemName;
        ItemQty = _ItemQty;
        ListId = _ListId;
    }
}

This is ShopList class that contains list of shop items

[JsonObject(MemberSerialization.OptIn)]
public class ShopList
{
    [JsonProperty]
    public int id { get; set; }
    [JsonProperty]
    public String ListName { get; set; }
    [JsonProperty]
    public string Store { get; set; }
    [JsonProperty]
    public int UserId { get; set; }
    [JsonProperty]
    public List<ShopItem> list = new List<ShopItem>();

    public ShopList()
    {

    }
    public ShopList(int _id, String _ListName, string _Store, int _UserId)
    {
        id = _id;
        ListName = _ListName;
        Store = _Store;
        UserId = _UserId;
    }
}

This is my controler class

[Route("api/[action]")]
public class ShopListsRetriveControler : Controller
{
    //get all Shoping lists
    // GET: api/<controller>
    [ActionName("GetAllShopLst")]
    [HttpGet]
    public List<ShopList> getAllLists()
    {
        return DAL._GetAllShopLst();
        //return Ok(DAL._GetAllShopLst());
    }
    [HttpGet("getAllListsRecords")]
    public JsonResult getAllListsRecords()
    {
        JsonResult jr = Json(DAL._GetAllShopLst());
        Newtonsoft.Json.JsonSerializerSettings jsonSerializerSettings = new Newtonsoft.Json.JsonSerializerSettings();
        jsonSerializerSettings.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.All;
        return Json(DAL._GetAllShopLst(),jsonSerializerSettings);
    }

}

Result of my code is:

{
"list":    [
        {
     "id": 1,
     "itemName": "Stvar1",
     "itemQty": "1 kom",
     "listId": 1
  },
        {
     "id": 2,
     "itemName": "Stvar2",
     "itemQty": "2 kom",
     "listId": 1
  },
        {
     "id": 3,
     "itemName": "Stvar3",
     "itemQty": "3 kom",
     "listId": 1
  },

]
"id": 1,
"listName": "Lista1",
"store": "          ",
"userId": 0
} 

How can I get type names in response. I google it a lot but i cant find whats missing.

  • 1
    are you talking about including int, string, float vs inside json? – Derviş Kayımbaşıoğlu Feb 03 '19 at 21:36
  • I'm a little unclear about your problem. Is it that, despite passing `Newtonsoft.Json.TypeNameHandling.All` to `return Json(DAL._GetAllShopLst(),jsonSerializerSettings);`, no `"$type"` parameters are appearing in the response? – dbc Feb 04 '19 at 04:29
  • Yes that is a problem. When I will deserialize this on android how would he know witch class to use. Why there is no `"$type"` parameters? – Valentina Dijaković Kukec Feb 04 '19 at 07:14
  • Assuming you get this working, do be aware of the security risks with this design. For details see [TypeNameHandling caution in Newtonsoft Json](https://stackoverflow.com/q/39565954/3744182) and [External json vulnerable because of Json.Net TypeNameHandling auto?](https://stackoverflow.com/q/49038055/3744182) for details. – dbc Feb 04 '19 at 08:22

0 Answers0