Tag: json.net

如何在序列化对象时加密选定的属性?

我正在使用JSON在我的应用程序中存储某些设置。 某些设置包含敏感信息(例如密码),而其他设置不敏感。 理想情况下,我希望能够序列化我的对象,其中敏感属性自动加密,同时保持非敏感设置可读。 有没有办法使用Json.Net做到这一点? 我没有看到任何与加密相关的设置。

JsonConvert.DeserializeObject (string)为$ id属性返回null值

我正在使用System.Net.WebClient.DownloadString下载JSON。 我收到了有效回复: { “FormDefinition”: [ { “$id”:”4″, “Class”:558, “ClassDisplayLabel”:”Punchworks”, “Name”:”Punchworks Form” }, { “$id”:”6″, “Class”:558, “ClassDisplayLabel”:”Punchworks”, “Name”:”Punchworks Form test second” }, { “$id”:”46″, “Class”:558, “ClassDisplayLabel”:”Punchworks”, “Name”:”any_Name” }, { “$id”:”47″, “Class”:558, “ClassDisplayLabel”:”Punchworks”, “Name”:”Punchworks Form test second” }, { “$id”:”49″, “Class”:558, “ClassDisplayLabel”:”Punchworks”, “Name”:”Testing Name ??´????? ???? ACEeiÅ¡uu { [ ( ~ ! @ # ” }, { […]

Json.NET按深度和属性序列化

例如,我们有两个类 class FooA { [SomeSpecialAttribute] public int SomeValueA { get; set; } public int SomeValueB { get; set; } public int SomeValueC { get; set; } } class FooB { public FooA FooA { get; set; } } 我使用Json.NET,最大深度是1.在序列化FooA时,它应该像往常一样输出所有属性,但是在序列化FooB时它应该只输出一个具有特殊属性的FooA属性。 因此,只有在解析嵌套引用属性(Depth> 0)时,我们才能获得单个字段。 输出应为:{“FooA”:{“SomeValueA”:“0”}} 有任何想法吗?

设置默认的全局json序列化程序设置

我正在尝试在我的global.asax设置这样的全局序列化设置。 var formatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter; formatter.SerializerSettings = new JsonSerializerSettings { Formatting = Formatting.Indented, TypeNameHandling = TypeNameHandling.Objects, ContractResolver = new CamelCasePropertyNamesContractResolver() }; 使用以下代码序列化对象时,不使用全局序列化程序设置? return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(JsonConvert.SerializeObject(page)) }; 是不是可以像这样设置全局序列化设置,还是我错过了什么?

如何在MVC5项目中使用Json.NET进行JSON模型绑定?

我一直在网上寻找答案或例子,但还找不到一个。 我只想更改默认的JSON序列化程序,用于在模型绑定到JSON.NET库时反序列化JSON。 我发现这个 SOpost,但到目前为止无法实现它,我甚至看不到System.Net.Http.Formatters命名空间,也看不到GlobalConfiguration 。 我错过了什么? UPDATE 我有一个ASP.NET MVC项目,它基本上是一个MVC3项目。 目前我的目标是.NET 4.5并使用ASP.NET MVC 5和相关的NuGet包。 我没有看到System.Web.Http程序集,也没有看到任何类似的命名空间。 在这种情况下,我想注入JSON.NET作为JSON类型请求的默认模型绑定器。

Json.NET在返回json序列化字符串时添加反斜杠

我试图使用Json.NET将列表序列化为json字符串,但返回字符串中有反斜杠,这反过来又失败了json解析。 var x = from d in entities.Books.ToList() select new { ID = d.ID, BookName = d.BookName }; return JsonConvert.SerializeObject(x.ToList()); 上面的代码返回 “[{\”ID\”:1,\”BookName\”:\”MVC Music Store – Tutorial – v3.0\”},{\”ID\”:2,\”BookName\”:\”Pro.ASP.NET.MVC.3.Framework\”},{\”ID\”:3,\”BookName\”:\”Application Architecture Guide v2\”},{\”ID\”:4,\”BookName\”:\”Gang of Four Design Patterns\”},{\”ID\”:5,\”BookName\”:\”CS4 Pocket Reference\”}]” 它失败了所有JSON解析。 我该如何删除它们。

无法将JSON数组反序列化为类型–Json.NET

我试图将json数据反序列化为模型类,但我失败了。 这是我做的: public CountryModel GetCountries() { using (WebClient client = new WebClient()) { var result = client.DownloadString(“http://api.worldbank.org/incomeLevels/LIC/countries?format=json”); var output = JsonConvert.DeserializeObject<List>(result); return output.First(); } } 这就是我的模型的样子: public class CountryModel { public int Page { get; set; } public int Pages { get; set; } public int Per_Page { get; set; } public int Total { […]

将特定枚举反序列化为Json.Net中的system.enum

我有一个相当通用的“规则”类,我用它来驱动我正在编写的分析引擎的行为: public class Rule { /// /// The general rule type. /// public RuleType RuleType { get; set; } /// /// The human-readable description of the rule. /// public string RuleDescription { get; set; } /// /// The integer magnitude of the rule, if applicable. /// public int? RuleInt { get; set; } /// /// […]

Newtonsoft JSON.net反序列化错误,其中JSON中的字段更改顺序

这是从Android设备获取请求的WCF服务。 相同的请求适用于Lollipop设备,而不是来自软糖设备,因为软糖在创建时以不同方式排列JSON。 例外: 反序列化对象时出现意外的标记:String。 路径’SearchFilters.config。$ type’,第1行,第212位。 不工作的Json: { “DeviceType”: 2, “SearchFilters”: { “config”: { “$values”: [ { “Collection”: { “DeviceType”: 2 }, “Category”: “” } ], “$type”: “System.Collections.Generic.List`1[[Yoosh.SharedClasses.YooshConfig, YooshSharedClassesDll]], mscorlib” } }, “RequestingUserId”: “66666666-6666-6666-6666-666666666666”, “APIKey”: “xxx” } 工作Json: { “APIKey”: “xxx”, “DeviceType”: 2, “RequestingUserId”: “66666666-6666-6666-6666-666666666666”, “SearchFilters”: { “config”: { “$type”: “System.Collections.Generic.List`1[[Yoosh.SharedClasses.YooshConfig, YooshSharedClassesDll]], mscorlib”, “$values”: [ […]

JsonConvert.SerializeObject总是在XamarinForms中返回{}

JsonConvert.SerializeObject似乎不能用于xamarin表单。 或者我可能会在这里遗漏一些东西。 这是我序列化一个简单对象时的输出。 看到它返回{}。 我也试过序列化一个类,它确实返回了相同的。 这可能是什么问题? UPDATE 仍然没有解决这个问题。 我尝试创建一个简单的控制台应用程序,serializeObject正在处理该项目,所以我认为它不仅仅在xamarin表单中工作。 有没有人有一个xamarin表单项目,JSON.NET的serializeobject正在工作? 我正在使用视觉工作室2017,json.net 10.0.3和现场播放器。