【问题标题】:how can convert string into json result如何将字符串转换为 json 结果
【发布时间】:2020-07-28 06:45:58
【问题描述】:

我正在使用函数以树格式获取数据。从 GetDepotWithDepartment 返回的数据作为字符串数据类型。如何将字符串转换为 JsonResult

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

来自给定存储库 linq 的数据

[
  {
    "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"
      }
      
    ]
}
]

【问题讨论】:

标签: asp.net-core jsonresult


【解决方案1】:

根据您的描述,我建议您可以尝试以下代码返回 json 结果:

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

结果:

【讨论】:

    猜你喜欢
    • 2021-09-15
    • 2012-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多