Tag: json.net

编码的UI测试现在收到有关无法找到Newtonsoft.json的错误

我有一个简单的Coded UI测试来登录应用程序。 测试是数据驱动的,并使用来自TFS的数据。 我们正在使用服务器和代理系统上的TfsTestAgent用户(具有管理员权限)。 当我执行测试时,我看到以下错误: The unit test adapter failed to connect to the data source or to read the data. For more information on troubleshooting this error, see “Troubleshooting Data-Driven Unit Tests” (http://go.microsoft.com/fwlink/?LinkId=62412) in the MSDN Library.Error details: Could not load file or assembly ‘Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed’ or one of its dependencies. The […]

Azure移动应用程序自定义json序列化

我似乎无法在Azure移动应用程序中自定义JSON序列化。 为了避免我自己的代码的复杂性,我从头开始设置一个新项目。 Visual Studio Community 2015 Update 2,Azure App Service Tools v2.9(如果重要)。 新项目,Visual C#,云,Azure移动应用程序。 在App_Start\Startup.MobileApp.cs这是模板中的内容: public static void ConfigureMobileApp(IAppBuilder app) { HttpConfiguration config = new HttpConfiguration(); new MobileAppConfiguration() .UseDefaultConfiguration() .ApplyTo(config); // Use Entity Framework Code First to create database tables based on your DbContext Database.SetInitializer(new MobileServiceInitializer()); MobileAppSettingsDictionary settings = config.GetMobileAppSettingsProvider().GetMobileAppSettings(); if (string.IsNullOrEmpty(settings.HostName)) { app.UseAppServiceAuthentication(new AppServiceAuthenticationOptions […]

反序列化GUID数组时的JSON.NETexception

我正在使用JSON.NET反序列化从浏览器发送的AJAX HTTP请求,并且遇到使用Guid []作为参数的Web服务调用的问题。 当我使用内置的.NET序列化程序时,这很好用。 首先,流中的原始字节如下所示: System.Text.Encoding.UTF8.GetString(rawBody); “{\”recipeIds\”:[\”d9ede305-d244-483b-a435-abcf350efdb2\”]}” 然后我打电话给: Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer(); parameters[0] = serializer.Deserialize(sr, operation.Messages[0].Body.Parts[0].Type); .Type是System.Guid[] 然后我得到了例外: Cannot deserialize the current JSON object (eg {“name”:”value”}) into type ‘System.Guid[]’ because the type requires a JSON array (eg [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (eg […]

在json反序列化期间确定类型

我正在研究一种协议,其中接收器将接收某些指定自定义类型的json消息(当前为5,但可能为10-20)。 我正在努力想出一个最优/快速的解决方案,它将自动反序列化json并返回正确类型的对象。 例: public class MessageA { public string Message; } public class MessageB { public int value; } public class MessageC { public string ValueA; public string ValueB; } 理想情况下,该方法应该是这样的 Object Deserialize(string json); 并且它将返回三种消息类型之一或null – 如果存在解析错误/ json与任何预定义类型都不匹配。 更新:我可以控制发送器/接收器以及协议设计。

通过json将javascript数组传递给C#

我正在使用这个javascript数组: var exportOptions = [{ jobName: “”}, {exportType: “”}, {attachToEmail: “”}, {distributorName: “”}, {vistaNumber: “”}, {customerName: “”}, {competitors: “”}, {agreementType: “”}, { annualPotential: “” }, {businessCase: “” }]; 我用这段代码传递给ASP.NET codebehind(C#): $.ajax({ type: ‘POST’, url: ‘Epad.aspx/generateReport’, data: “{‘columnList’: ‘” + columnList + “‘, ‘exportOptions’:” + JSON.stringify( exportOptions ) + “}”, contentType: ‘application/json; charset=utf-8’, dataType: ‘json’, async: […]

Json.net将数字属性序列化为字符串

我正在使用JsonConvert.SerializeObject来序列化模型对象。 服务器期望所有字段都是字符串。 我的模型对象具有数字属性和字符串属性。 我无法向模型对象添加属性。 有没有办法将所有属性值序列化,就好像它们是字符串一样? 我必须只支持序列化,而不是反序列化。

是否可以在SignalR集线器方法中返回预编码的JSON字符串?

在MVC项目中,我在类似于此的集线器上有一个方法: public string Foo() { return DoCrazyThingThatReturnsJson(); } 不幸的是,SignalR(或其他东西)接受编码的JSON字符串并愉快地对其进行编码,然后将其返回,因此浏览器就像LOLWTF。 有没有办法跳过第二次编码?

为什么不能在JObject上使用LINQ方法?

Newtonsoft.Json.Linq.JObject实现了IEnumerable ,而不是显式实现,但为什么不能这样做: using System.Linq; … var jobj = new JObject(); var xxx = jobj.Select(x => x); //error foreach(var x in jobj) { } //no error 为什么? 谢谢。

使用Json.NET来(de)序列化DataContractJsonSerializer使用的结构中的字典?

有没有办法使用Json.NET进行(反)序列化,但继续使用DataContractJsonSerializer的字典序列化约定? 换句话说,有没有办法在这个结构中读写JSON: { “MyDict” : [ { “Key” : “One”, “Value” : 1 }, { “Key” : “Two”, “Value” : 2 } ] } 使用这样的类(使用Json.NET属性): public class MyClass { public Dictionary MyDict { get; set; } } 似乎Json.NET的KeyValuePairConverter可能会以某种方式提供帮助,但如果是这样,我找不到正确的方法来应用它。 我尝试使用[JsonProperty(ItemConverterType = typeof(KeyValuePairConverter))]归因MyDict , 这个页面甚至似乎暗示可以工作,但是在序列化期间添加这样的属性会导致ArgumentOutOfRangeException 。

使用JsonConverterAttribute时,自定义inheritanceJsonConverter失败

我试图反序列化派生类型,我想使用自定义属性Type来区分派生类型。 [ { “Type”: “a”, “Height”: 100 }, { “Type”: “b”, “Name”: “Joe” } ] 我找到的解决方案是创建一个自定义的JsonConverter 。 在ReadJson我读取了Type属性,并通过ToObject函数实例化该类型。 一切正常,直到我使用JsonConverterAttribute 。 ReadJson方法无限循环,因为该属性也应用于子类型。 如何防止将此属性应用于子类型? [JsonConverter(typeof(TypeSerializer))] public abstract class Base { private readonly string type; public Base(string type) { this.type = type; } public string Type { get { return type; } } } public class AType : […]