我有一个问题: Json.NET是否正确地与泛型一起工作?我有下一个代码:
[TestClass]
public class TestClass1_Test
{
[TestMethod]
public void ToJson()
{
var mot = new TestClass1(1, "title");
var result = mot.ToJson();
Assert.IsNotNull(result);
var pobject = TestClass1.FromJson(resul
Newtonsoft的Json.Net显然具有反序列化NaN的能力,但我似乎不能强迫它进入提供的浮动字段。我得到了一个Unexpected character "N"异常。
在以下代码中:
using Newtonsoft.Json;
namespace TestNanDeserialize
{
public class Number
{
public float Float;
}
public class Empty
{
}
internal class Program
{
以下程序在.NET 5(或.NET核心)和.NET框架上运行时会产生不同的结果。
为什么行为是不同的?我不是在寻找反序列化问题的解决方案;我在这里的目标是了解发生了什么。
class Versioned
{
public Version V {get; set;} = new Version(1,0);
}
static void Main(string[] args)
{
// Serialised with version Newtonsoft.Json 9.0.1
var json = "{\"V\":{\"Major\"
我想从restful web服务中检索数据,以便将这些数据保存到object中。数据检索在原则上是可行的,JSON字符串可以显示在文本框中。然而,我很难使数据在C#中可用--经过一些研究和自己的编程之后,当deserialising无法修复时,我仍然会遇到错误:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System
在执行.NET C#代码时,会出现间歇性错误。下面是错误。
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Newtonsoft.Json.Linq.JValue' does not contain a definition for 'value'
下面是我使用的代码-
dynamic obj = JsonConvert.DeserializeObject(response);
string value = obj.responseObject.value.token;
当我试图执行上面的代码
我现在走进了死胡同:(我正在尝试通过NewtonSoft.Json序列化我的对象
string json = JsonConvert.SerializeObject(doc);
并得到这样的异常:
Error getting value from 'IsCurrentTimestampSelectStringCallable' on 'NHibernate.Dialect.SQLiteDialect'.
内部异常:
Database does not known how to define a current timestamp function
我使用Newtonsoft.Json来序列化/反序列化对象。
据我所知,如果类没有无参数构造函数,反序列化就不会成功。例如,
public class Dog
{
public string Name;
public Dog(string n)
{
Name = n;
}
}
对于下面的类,代码正确地生成对象。
Dog dog1 = Newtonsoft.Json.JsonConvert.DeserializeObject<Dog>("{\"Name\":\"Dog1\"}");
对我
对于Json.net非常陌生,并尝试了下面的简单示例,序列化并反序列化一个对象,得到下面的错误:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Collections;
namespace Timehunter.Base.S
让我们想象一下,我有一门课
public class Foo
{
public int Prop1 { get; set; }
public int Prop2 { get; set; }
public int Prop3 { get; set; }
}
想象一下,在某些控制器FooController中,我创建了这样一个类Foo的List<>,用数据填充它,用Newtonsoft.Json序列化它,然后发送到客户端。
没关系,没问题。
但是,我也有每个用户的权限系统,其中说User1不能看到Prop1的数据,User2不能看到Prop3的数据。而且我有很多这样的类Fo
以前,我有一个OrderedDictionary,我把它转换成JSON,然后在代码的某些部分,我需要将它反序列化回OrderedDictionary。
现在,我需要将该属性的类型更改为列表。我需要向后兼容。我如何编写代码,以便在反序列化JSON时,将其反序列化为一个对象,然后检查对象是否是List,做些什么,如果对象是OrderedDictionary,则执行一些操作。
目前,如果我将它反序列化为一个对象,就会得到一个JObject。我在用Newtonsoft.Json。
我想要的是:
var x = JsonConvert.DeserializeAnonymousType(jsonStrin
目前,我正在使用这段丑陋的代码向JObject添加一组键/值对(保存在Dictionairy中)。它并不是把他们作为孩子,而是作为兄弟姐妹。好的,代码可以工作,但是如果将它们作为子节点添加,则会更干净。(动态对象e持有JObject)
public void trigger(dynamic e ,Pairs extras)
{
if (Post != null)
{
foreach (KeyValuePair<string, object> entry in extras)
{
在下面的代码中,我使用Json.Net序列化一个对象。这个Json嵌入了类型名。然后,我更改其中一个类型名称以导致错误(这是一个测试,我正在处理现有项目中的一个实际问题)。当我反序列化Json时,我希望得到一个对象,该对象对于带有篡改类型名称的属性具有一个空值。相反,序列化程序退出并返回null。我的期望正确吗?我是否可以以某种方式更改设置,以便为根对象获得一个非空对象?请注意,我得到的第二个错误表明序列化程序中存在一个错误。
static public class JsonTest
{
static public void Test()
{
// Create
我正在尝试从Newtonsoft.Json迁移到System.Text.Json."Newtonsoft.Json.JsonIgnore“用于在序列化期间忽略常量属性,但"System.Text.Json.Serialization.JsonIgnore”不起作用。我想知道周围是否有工作。
所以我试着从;
using Newtonsoft.Json;
public class MyClass: MyBaseClass
{
[JsonIgnore]
public const string MyConstString = "lets get rid of
我希望使用如下的"True"方法将一个简单的字符串Newtonsoft.Json.JsonConvert.DeserializeObject反序列化为布尔值true:
var b = Newtonsoft.Json.JsonConvert.DeserializeObject("True");
但它给了我一个错误。'Unexpected character encountered while parsing value: T. Path '', line 0, position 0.'
我听说了Newtonsoft uses cas
我尝试编写JSON反序列化程序已经有一段时间了,但是一直没能找到我的错误。为什么Newtonsoft在反序列化之后告诉我,Unexpected token when deserializing object: StartObject?
type ThisFails =
{ a : string * string
m : Map<string, string> }
type ThisWorks =
{ y : Map<string, string>
z : string * string }
testCase "failing test
我意外地在我的API中引入了一个突破性的更改,我在我的System.Text.Json核心应用程序中切换到了ASP.NET。我有一个客户端正在发送一个JSON文档,并且使用数字1或0作为布尔字段,而不是true或false
// What they're sending.
{ "Active": 1 }
// What they should be sending.
{ "Active": true }
Newtonsoft.Json库通过将数字转换为bools (0 = false,everything = true)自动处理,但是System.Te
我想用带有JSON的简单请求来测试我的POST端点。
我有一个ASP.NET Core3.0项目,其中有一个控制器和一个端点。我主要创建了空的ASP.NET核心项目。然后,我创建了一个简单的控制器来处理POST请求。
[ApiController]
[Route("api/[controller]")]
public class UpdateController : Controller
{
// POST api/update
[HttpPost]
public async Task<IAc
返回字典的webAPI方法的结果不按IsoDateTimeConverter中定义的格式序列化
这是我的配置:
public static class WebApiConfig
{
//Web API configuration and services
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Formatters.JsonFormatter.SerializerSettings.Date