Tag: json

自定义Json序列化类

我的代码结构如下。 public class Stats { public string URL { get; set; } public string Status { get; set; } public string Title { get; set; } public string Description { get; set; } public int Length { get; set; } } 和 public class UrlStats { public string URL { get; set; } public int […]

如何使用json.net将c#通用列表转换为json?

我正在将我的数据表转换为c#通用列表。 DataTable dt = mydata(); List list = dt.AsEnumerable().ToList(); 现在我如何使用json.net将此list转换为json? 任何建议。 json格式的样本应该是这样的, {“Table” : [{“userid” : “1”,”name” : “xavyTechnologies”,”designation” : “”, “phone” : “9999999999”,”email” : “test@test.com”,”role” : “Admin”,”empId” : “”, “reportingto” : “”},{“userid” : “2”,”name” : “chendurpandian”,”designation” : “softwaredeveloper”,”phone” : “9566643707”,”email” : “chendur.pandia@gmail.com”, “role” : “Super User”,”empId” : “1”,”reportingto” : “xavyTechnologies”}, {“userid” : “3”,”name” : […]

将JSON数组发布到mvc控制器

我正在尝试将JSON数组发布到MVC控制器。 但无论我尝试什么,一切都是0或null。 我有这个包含文本框的表。 我需要从所有这些文本框中将ID和值作为对象。 这是我的Javascript: $(document).ready(function () { $(‘#submitTest’).click(function (e) { var $form = $(‘form’); var trans = new Array(); var parameters = { TransIDs: $(“#TransID”).val(), ItemIDs: $(“#ItemID”).val(), TypeIDs: $(“#TypeID”).val(), }; trans.push(parameters); if ($form.valid()) { $.ajax( { url: $form.attr(‘action’), type: $form.attr(‘method’), data: JSON.stringify(parameters), dataType: “json”, contentType: “application/json; charset=utf-8”, success: function (result) { $(‘#result’).text(result.redirectTo) if (result.Success […]

更改JSON文件中的值(写入文件)

我的应用程序的Release文件夹中有一个settings.json文件。 我想要做的是更改它的值,而不是暂时,永久..这意味着,删除旧条目,写一个新条目并保存它。 这是JSON文件的格式 { “Admins”:[“234567”], “ApiKey”:”Text”, “mainLog”: “syslog.log”, “UseSeparateProcesses”: “false”, “AutoStartAllBots”: “true”, “Bots”: [ { “Username”:”BOT USERNAME”, “Password”:”BOT PASSWORD”, “DisplayName”:”TestBot”, “Backpack”:””, “ChatResponse”:”Hi there bro”, “logFile”: “TestBot.log”, “BotControlClass”: “Text”, “MaximumTradeTime”:180, “MaximumActionGap”:30, “DisplayNamePrefix”:”[AutomatedBot] “, “TradePollingInterval”:800, “LogLevel”:”Success”, “AutoStart”: “true” } ] } 假设我想更改密码值而不是BOT PASSWORD我希望它只是密码。 我怎么做?

使用Json.NET将JSON反序列化为对象

我正在使用新的StackOverflow API 。 不幸的是,我的JSON有点弱,所以我需要一些帮助。 我正在尝试反序列化用户的这个JSON: {“user”:{ “user_id”: 1, “user_type”: “moderator”, “creation_date”: 1217514151, “display_name”: “Jeff Atwood”, … “accept_rate”: 100 }} 到我用JsonProperty属性修饰的对象: [JsonObject(MemberSerialization.OptIn)] public class User { [JsonProperty(“user_id”, Required = Required.Always)] public virtual long UserId { get; set; } [JsonProperty(“display_name”, Required = Required.Always)] public virtual string Name { get; set; } … } 我得到以下exception: Newtonsoft.Json.JsonSerializationException:在JSON中找不到必需属性’user_id’。 这是因为JSON对象是一个数组吗? 如果是这样,我如何将其反序列化为一个User对象? […]

无法使用Json.net使用Complex键序列化Dictionary

我有一个自定义.net类型的字典作为其密钥。我正在尝试使用JSON.net将此字典序列化为JSON,但是它无法在序列化期间将密钥转换为适当的值。 class ListBaseClass { public String testA; public String testB; } —– var details = new Dictionary(); details.Add(new ListBaseClass { testA = “Hello”, testB = “World” }, “Normal”); var results = Newtonsoft.Json.JsonConvert.SerializeObject(details); var data = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary> results); 这个给我 – >“{\”JSonSerialization.ListBaseClass \“:\”Normal \“}” 但是,如果我将自定义类型作为字典中的值,它可以很好地工作 var details = new Dictionary(); details.Add(“Normal”, new ListBaseClass { testA = “Hello”, […]

使用自定义WCF正文反序列化而不更改URI模板反序列化

从这篇博文中 ,我能够创建一个使用JSON.NET序列化的自定义WCF IDispatchMessageFormatter 。 它有一点需要注意:与UriTemplate一起使用它并不一定按预期工作。 这是博客文章提供的实现: class NewtonsoftJsonDispatchFormatter : IDispatchMessageFormatter { private readonly OperationDescription od; private readonly ServiceEndpoint ep; private readonly Dictionary parameterNames = new Dictionary(); public NewtonsoftJsonDispatchFormatter(OperationDescription od, ServiceEndpoint ep, bool isRequest) { this.od = od; this.ep = ep; if (isRequest) { int operationParameterCount = od.Messages[0].Body.Parts.Count; if (operationParameterCount > 1) { this.parameterNames = new […]

客户端配置以使用WCF JSON Web服务

我已经将Web服务配置为使用Json,如本博客所述: http : //www.west-wind.com/weblog/posts/164419.aspx和其他各种博客,但我无法创建一个客户端来使用它服务。 我尝试了各种各样的东西,但总是有无意义的例外。 实现(我应该添加的WCF)客户端的正确方法是什么?

JToken:获取原始/原始JSON值

有没有办法从JToken获取原始/原始JSON值? 问题: var data = JObject.Parse(@”{ “”SimpleDate””:””2012-05-18T00:00:00Z””, “”PatternDate””:””2012-11-07T00:00:00Z”” }”); var value = data[“SimpleDate”].Value(); 该value现在是05/18/2012 00:00:00但我需要原始字符串2012-05-18T00:00:00Z 。 有没有办法获得这个原始价值? 另外,我无法改变创建JObject的方式(例如更改设置),因为它作为参数传递到我的类中… (参考: 最初的NJsonSchema问题 )

在c#中覆盖Json属性名称

我有一个包含以下字段的课程。 当需要调用外部rest API方法时,这些属性用于序列化为json对象。 public class Customer { [JsonProperty(PropertyName = “email”)] public string Email { get; set; } [JsonProperty(PropertyName = “prop[listId]”)] public string Test{ get; set; } // there are lot of properties } 在属性名称Test ,外部API服务调用需要遵循json文件名格式。 prop[7] 在我的情况下,这7可以根据test,dev和prod等环境进行更改。所以我正在寻找一种方法将listId值移动到app.config中。 我试图按照以下方式执行此操作,但不允许这样做。对于listIdValue如果分配常量值,它将起作用。 private string listIdValue = ConfigurationManager.AppSettings[“ListIdValue”]; [JsonProperty(PropertyName = “prop[“+listIdValue +”]”)] public string Test{ get; set; }