相关疑难解决方法(0)

.Net Core 3.0中IMvcBuilder AddJsonOptions放在哪里?

我刚刚将我的ASP Web API项目从.Net core 2.0升级到3.0。我在用

     services.AddMvc()
             .AddJsonOptions(options =>options.SerializerSettings.ContractResolver 
                                       = new DefaultContractResolver());
Run Code Online (Sandbox Code Playgroud)

以前是为了确保序列化JSON的小写字母。

升级到3.0后,出现此错误...

错误CS1061'IMvcBuilder'不包含'AddJsonOptions'的定义,找不到可以接受的扩展方法'AddJsonOptions'接受类型为'IMvcBuilder'的第一个参数(是否缺少using指令或程序集引用?)

根据Asp.Net Core 2.2中MvcJsonOptions的AddJsonOptions,Microsoft.AspNetCore.Mvc.Formatters.Json nuget包提供了AddJsonOptions扩展方法。我尝试安装/重新安装此程序,但仍然无法解决该方法。有趣的是,智能感知仅显示Microsoft.AspNetCore.Mvc.Formatters。即使我添加了Json nuget包,当我尝试添加using语句时也使用Xml

有什么想法吗?该文档AddJsonOptions只上升到.NET 2.2所以也许是方法已经在3.0赞成一些其他配置机制的被弃用?

asp.net json.net asp.net-core asp.net-core-3.0

35
推荐指数
5
解决办法
1万
查看次数

如何使用JSONConvert将变量Result转换为对象?

我正在使用.NET Core for Linux进行控制台程序.使用Http功能我可以从Web服务获得一些信息.然后我试图将结果转换为对象,但我无法使用JSON.

我读过这篇文章,但是我没有找到任何示例,也没有访问JavaScriptSerializer的权限

    public async void CallApi(Object stateInfo)
    {
        var client = new HttpClient();
        var requestContent = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("pair", "XETHZEUR"), });
        HttpResponseMessage response = await client.PostAsync("https://api.kraken.com/0/public/Trades", requestContent);
        HttpContent responseContent = response.Content;
        using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync()))
        {
            String result = await reader.ReadToEndAsync();
            //Here I would like to do a deserialized of my variable result using JSON (JObject obj = (JObject)JsonConvert.DeserializeObject(result);) But I don't find any JSON object
        }
    }
Run Code Online (Sandbox Code Playgroud)

编辑 …

c# console json .net-core

6
推荐指数
1
解决办法
2万
查看次数

如何在ASP.NET Core WebAPI中使用Newtonsoft:Json.NET?

我找到了一些关于nuget包的信息--Newtonsoft:Json.NET

据我所知,它使从JSON到C#对象的序列化更快.

我的问题:将这个软件包安装到我的ASP.NET CORE WebAPI项目是否已经足够了,或者我已经以某种方式绑定它,不管中间件还是其他什么东西?它会使序列化更快吗?

json json.net asp.net-core

5
推荐指数
1
解决办法
4万
查看次数

在 .Net Core 3.1 中使用 NewtonSoft 将 DataTable 转换为 JSON 字符串时如何获得不含 '\u0022' 或 '\' 等字符的 JSON 字符串

我正在 .net core 3.1 中编写一个简单的 API。要将我的数据表转换为 JSON 字符串,我使用 NewtonSoft 库和以下代码:

string JSONresult = JsonConvert.SerializeObject(dt, Formatting.Indented);
return Json (new { JSONresult });
Run Code Online (Sandbox Code Playgroud)

我得到的输出是 JSON 字符串,但它有很多像 '\u0022' 这样的字符,我知道它是双引号。

{"jsoNresult":"[\r\n  {\r\n    \u0022ID\u0022: 2,\r\n    \u0022FunctionalityName\u0022: \u0022User Upload\u0022,\r\n    \u0022FunctionalityDescription\u0022: \u0022For Bulk Uploading User At Once\u0022,\r\n    \u0022TableName\u0022: \u0022tablename\u0022,\r\n    \u0022ValidationSP\u0022: \u0022user_Validate\u0022,\r\n    \u0022InsertSP\u0022: \u0022Insert_User\u0022\r\n  }\r\n]"}
Run Code Online (Sandbox Code Playgroud)

我想要的是:

{"jsoNresult":"[{"ID": "2","FunctionalityName": "User Upload","FunctionalityDescription": "For Bulk Uploading User At Once","TableName": "tablename","ValidationSP": "user_Validate","InsertSP": "Insert_User"}]"}
Run Code Online (Sandbox Code Playgroud)

我是 c# 新手,但之前曾在 Flask 甚至 Spring Boot 上工作过,他们返回清晰的 json 字符串。

那么我如何在 .net core 3.1 中实现我想要的。PS:我知道使用Formatting.Indented,我可以有或没有它的序列化字符串

c# json json.net asp.net-core-webapi asp.net-core-3.1

5
推荐指数
1
解决办法
6505
查看次数