다음은 샘플 코드입니다.
bool 목록을 포함하는 TargetEntity가 있으며 기본적으로 true 값 의 7 개 요소 목록으로 설정되어 있습니다.
내가하려는 것은 7 개의 거짓 값 목록을 포함하는 JSON에서 역 직렬화하는 것입니다 . 그러나 역 직렬화 할 때 기본 값을 포함하고 다음과 같이 JSON에서 역 직렬화 된 값을 첨부 한 14 개의 값 목록을 구성한 것 같습니다. { true, true, true, true, true, true, true, false, false, false, false, false, false, false }
What is wrong here?
using Newtonsoft.Json;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var _entityList = JsonConvert.DeserializeObject<List<TargetEntity>>(
"[{\"BoolsList\": [false,false,false,false,false,false,false]}]",
new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore });
Console.WriteLine("List size: " + _entityList[0].BoolsList.Count);
for(int i = 0; i < _entityList[0].BoolsList.Count; i++)
{
Console.WriteLine($"List element {i}: " + _entityList[0].BoolsList[i]);
}
}
public class TargetEntity
{
public List<bool> BoolsList { get; set; } = new List<bool> { true, true, true, true, true, true, true };
}
}
이 코드와 같은 C # 바이올린이 있습니다 : https://dotnetfiddle.net/VMIXeg
serializer 설정 및 새 개체가 구성되고 채워지는 방식과 관련된 매우 흥미로운 동작입니다. :)
원하는 것이 기본 목록을 바꾸고을 계속 사용하는 것이라면 Newtonsoft.Json
설정을 바꿀 수 있습니다.
new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore }
으로
new JsonSerializerSettings { ObjectCreationHandling = ObjectCreationHandling.Replace }
.NET 버전에 따라 다음을 사용할 수도 있습니다 System.Text.Json
.
using System;
using System.Collections.Generic;
using System.Text.Json;
public class Program
{
public static void Main()
{
var _entityList = JsonSerializer.Deserialize<List<TargetEntity>>("[{\"BoolsList\": [false,false,false,false,false,false,false]}]");
...
}
...
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다