asp.net-corejsonresult

how can convert string into json result


I am using function to get the data as a tree format. the data return from GetDepotWithDepartment as a string datatype. how can I convert the string into JsonResult

public JsonResult GetDepotDepartemntsForMap()
    {
         string lson = _unitOfWork.Department.GetDepotWithDepartment();
        return lson // error is coming cannot convert string into mvc.jsonREsult
    }

The data coming from the repository linq as given

[
  {
    "id": 1,
    "title": "1-Depot1",
    "subs": [
      {
        "id": "1.1",
        "title": "1.Advt"
      },
      {
        "id": "1.2",
        "title": "1.Admin"
      }
    
    ]
  },
  {
    "id": 2,
    "title": "2-Depot2",
    "subs": [
      {
        "id": "2.1",
        "title": "2.Sales"
      },
      {
        "id": "2.2",
        "title": "2.Admin"
      }
      
    ]
}
]


Solution

  • According to your description, I suggest you could try below codes to return the json result:

    public JsonResult GetDepotDepartemntsForMap()
        {
             string lson = _unitOfWork.Department.GetDepotWithDepartment();
            return new JsonResult(lson) // error is coming cannot convert string into mvc.jsonREsult
        }
    

    Result:

    enter image description here