【问题标题】:Json(JToken.Parse(response)) returns empty arrayJson(JToken.Parse(response)) 返回空数组
【发布时间】:2021-11-26 16:52:01
【问题描述】:

我使用 HttpClient 来使用 API,然后我从 response.Content.ReadAsStringAsync().Result 中获取作为序列化字符串的响应,如下所述,

"{\r\n  \"responseCode\": 400,\r\n  \"errors\": [\r\n    {\r\n      \"errorCode\": \"RPM400\",\r\n      \"errorMessage\": \"A program already exists.\"\r\n    }\r\n  ]\r\n}"

我想将其反序列化为

{
  "responseCode": 400,
  "errors": [
    {
      "errorCode": "RPM400",
      "errorMessage": "A program already exists."
    }
  ]
}

然后我必须将它发送到 UI,ASP.Net MVC 5 ActionMethod 中的返回类型是 ActionResult。

我尝试过使用 Json(JObject.Parse(response)) 和 Json(JToken.Parse(response)),但我在邮递员中得到如下的空响应

[
    [
        []
    ],
    [
        [
            [
                [
                    []
                ],
                [
                    []
                ]
            ]
        ]
    ]
]

同样的 Json(JObject.Parse(response)) 正在另一个应用程序 ASP.Net Core 2 中工作。您能提供一些帮助吗?

【问题讨论】:

标签: json asp.net-mvc-5


【解决方案1】:

我使用了stackOverflow's 标记的答案,我必须使用 using System.Web.Script.Serialization; 进行反序列化然后它起作用了,因为我的 .Net Framework 是 4.7.2 p>

【讨论】:

    猜你喜欢
    • 2018-05-31
    • 2018-10-11
    • 2014-03-09
    • 2015-08-08
    • 1970-01-01
    • 2014-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多