相关疑难解决方法(0)

如何用C#解析JSON?

我有以下代码:

var user = (Dictionary<string, object>)serializer.DeserializeObject(responsecontent);
Run Code Online (Sandbox Code Playgroud)

输入responsecontent是JSON,但未正确解析为对象.我应该如何正确地反序列化它?

c# json json.net deserialization

424
推荐指数
12
解决办法
103万
查看次数

由于对象的当前状态(System.Text.Json),操作无效

我们有一个 API,它只是将传入的 JSON 文档发布到消息总线,并为每个文档分配了一个 GUID。我们正在从 .Net Core 2.2 升级到 3.1,并打算用新System.Text.Json库替换 NewtonSoft 。

我们反序列化传入的文档,将 GUID 分配给字段之一,然后在发送到消息总线之前重新序列化。不幸的是,重新序列化失败了,例外Operation is not valid due to the current state of the object

这是一个显示问题的控制器:-

using System;
using System.Net;
using Project.Models;
using Microsoft.AspNetCore.Mvc;
using System.IO;
using System.Text;
using System.Text.Json;

namespace Project.Controllers
{
    [Route("api/test")]
    public class TestController : Controller
    {
        private const string JSONAPIMIMETYPE = "application/vnd.api+json";

        public TestController()
        {
        }

        [HttpPost("{eventType}")]
        public async System.Threading.Tasks.Task<IActionResult> ProcessEventAsync([FromRoute] string eventType)
        {
            try
            {
                JsonApiMessage payload;

                using (StreamReader reader …
Run Code Online (Sandbox Code Playgroud)

c# json .net-core-3.1 system.text.json

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

System.Text.Json.JsonElement ToObject解决方法

我想知道ToObject<>()Json.NET中System.Text.Json 的等效方法。

使用Json.NET,您可以使用任何JToken一个并将其转换为类。例如:

var str = ""; // some json string
var jObj = JObject.Parse(str);
var myClass = jObj["SomeProperty"].ToObject<SomeClass>();
Run Code Online (Sandbox Code Playgroud)

.NET Core 3的新功能我们将如何做到这一点 System.Text.Json

var str = ""; // some json string
var jDoc = JsonDocument.Parse(str);
var myClass = jDoc.RootElement.GetProperty("SomeProperty"). <-- now what??
Run Code Online (Sandbox Code Playgroud)

最初,我以为只是将JsonElement返回的内容jDoc.RootElement.GetPRoperty("SomeProperty")转换为字符串,然后反序列化该字符串。但是我觉得这可能不是最有效的方法,而且我真的找不到以其他方式进行操作的文档。

c# .net-core-3.0 system.text.json

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

System.Text.Json.JsonDocument.Parse 对象数组

我正在尝试使用以下 json 进行解析System.Text.Json.JsonDocument

[
    {
        "Name" : "Test 2",
        "NumberOfComponents" : 1,
        "IsActive" : true,
        "CreatedBy" : "bsharma"
    },
    {
        "Name" : "Test 2",
        "NumberOfComponents" : 1,
        "IsActive" : true,
        "CreatedBy" : "bsharma"
    }
]
Run Code Online (Sandbox Code Playgroud)

要解析的代码:

 using var jsonDoc = JsonDocument.Parse(inputStream, _jsonDocumentOptions);
Run Code Online (Sandbox Code Playgroud)

解析失败并出现错误:

System.Text.Json.JsonReaderException
  HResult=0x80131500
  Message='[' is an invalid start of a property name. Expected a '"'. LineNumber: 1 | BytePositionInLine: 4.
  Source=System.Text.Json
  StackTrace:
   at System.Text.Json.ThrowHelper.ThrowJsonReaderException(Utf8JsonReader& json, ExceptionResource resource, Byte nextByte, ReadOnlySpan`1 bytes)
   at System.Text.Json.Utf8JsonReader.ReadSingleSegment()
   at System.Text.Json.Utf8JsonReader.Read()
   at System.Text.Json.JsonDocument.Parse(ReadOnlySpan`1 …
Run Code Online (Sandbox Code Playgroud)

c# json .net-core asp.net-core system.text.json

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

在.net core 3中将newtonsoft代码转换为System.Text.Json。JObject.Parse和JsonProperty等效什么

我正在将我的newtonsoft实现转换为.net core 3.0中的新JSON库。我有以下代码

public static bool IsValidJson(string json)
{
    try
    {                
        JObject.Parse(json);
        return true;
    }
    catch (Exception ex)
    {
        Logger.ErrorFormat("Invalid Json Received {0}", json);
        Logger.Fatal(ex.Message);
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

我找不到任何等效的 JObject.Parse(json);

以及JsonProperty等效的属性是什么

public class ResponseJson
{
    [JsonProperty(PropertyName = "status")]
    public bool Status { get; set; }
    [JsonProperty(PropertyName = "message")]
    public string Message { get; set; }
    [JsonProperty(PropertyName = "Log_id")]
    public string LogId { get; set; }
    [JsonProperty(PropertyName = "Log_status")]
    public string LogStatus { get; set; }

    public string …
Run Code Online (Sandbox Code Playgroud)

c# serialization json.net .net-core-3.0 system.text.json

3
推荐指数
1
解决办法
811
查看次数