Tag: 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; } /// /// […]

将json转换为C#数组?

有谁知道如何将包含json的字符串转换为C#数组。 我有这个从webBrowser读取text / json并将其存储到字符串中。 string docText = webBrowser1.Document.Body.InnerText; 只需要以某种方式将json字符串更改为数组。 一直在看Json.NET,但我不确定这是否是我需要的,因为我不想将数组更改为json; 但反过来说。 谢谢您的帮助!

在JObject层次结构中按名称搜索特定的JToken

我有一些来自服务器的Json响应,例如: {“routes” : [ { “bounds” : { “northeast” : { “lat” : 50.4639653, “lng” : 30.6325177 }, “southwest” : { “lat” : 50.4599625, “lng” : 30.6272425 } }, “copyrights” : “Map data ©2013 Google”, “legs” : [ { “distance” : { “text” : “1.7 km”, “value” : 1729 }, “duration” : { “text” : […]

使用动态密钥反序列化JSON

我对JSON很新,目前正在学习(de)序列化。 我正在从网页中检索JSON字符串并尝试将其反序列化为对象。 问题是,根json密钥是静态的,但底层密钥是动态的,我无法预期它们反序列化。 这是一个字符串的迷你示例: {“daily”:{“1337990400000″:443447,”1338076800000″:444693,”1338163200000″:452282,”1338249600000″:462189,”1338336000000”:466626} 对于我的应用程序中的另一个JSON字符串,我使用的是JavascriptSerializer并使用类结构预测密钥。 将此字符串反序列化为对象的最佳方法是什么?

使用时,JSON.Net抛出StackOverflowException

我将这个简单的代码编写为Serialize类为flatten,但是当我使用[JsonConverter(typeof(FJson))]注释时,它会抛出StackOverflowException 。 如果我手动调用SerializeObject ,它可以正常工作。 如何在注释模式下使用JsonConvert: class Program { static void Main(string[] args) { A a = new A(); a.id = 1; abname = “value”; string json = null; // json = JsonConvert.SerializeObject(a, new FJson()); without [JsonConverter(typeof(FJson))] annotation workd fine // json = JsonConvert.SerializeObject(a); StackOverflowException Console.WriteLine(json); Console.ReadLine(); } } //[JsonConverter(typeof(FJson))] StackOverflowException public class A { public […]

使用Json.Net以dd / mm / yyyy格式反序列化日期

我正在尝试将对象从JSON数据反序列化为C#类(我使用的是Newtonsoft Json.NET):数据包含字符串值中的日期,如09/12/2013格式为dd/mm/yyyy 。 如果我调用JsonConvert.DeserializeObject(data); 日期加载到C#类的日期时间属性,如mm/dd/yyyy ,然后日期值是2013年9月12日(而不是2013年12月9日)。 是否可以配置JsonConvert以正确的格式获取日期?

如何在序列化json时忽略JsonProperty(PropertyName =“someName”)?

我有一些使用ASP.Net MVC的C#代码,它使用Json.Net来序列化一些DTO。 为了减少有效负载,我使用[JsonProperty(PropertyName =“shortName”)]属性在序列化期间使用较短的属性名称。 当客户端是另一个.Net应用程序或服务时,这非常有用,因为反序列化将对象层次结构重新组合在一起,使用更长的更友好的名称,同时保持实际的传输负载低。 当客户端通过浏览器javascript / ajax时,问题就出现了。 它发出请求,并获取json …但是json正在使用缩短的不太友好的名称。 如何让json.net序列化引擎以编程方式忽略[JsonProperty(PropertyName =“shortName”)]属性? 理想情况下,我的MVC服务将在那里运行,并且通常使用缩短的属性名称进行序列化。 当我的代码检测到特定参数时,我想使用较长的名称序列化数据并忽略[JsonProperty()]属性。 有什么建议? 谢谢, 凯文

如何处理返回字符串和字符串数组的json?

我正在使用Yahoo fantasy sports api。 我得到这样的结果: “player”: [ { … “eligible_positions”: { “position”: “QB” }, … }, { … “eligible_positions”: { “position”: [ “WR”, “W/R/T” ] }, … }, 我怎么能反序化呢? 我的代码如下所示: var json = new JavaScriptSerializer(); if (response != null) { JSONResponse JSONResponseObject = json.Deserialize(response); return JSONResponseObject; } 在我的JSONResponse.cs文件中: public class Player { public string player_key […]

如何使用C#将JSON发布到服务器?

这是我正在使用的代码: // create a request HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url); request.KeepAlive = false; request.ProtocolVersion = HttpVersion.Version10; request.Method = “POST”; // turn our request string into a byte stream byte[] postBytes = Encoding.UTF8.GetBytes(json); // this is important – make sure you specify type this way request.ContentType = “application/json; charset=UTF-8”; request.Accept = “application/json”; request.ContentLength = postBytes.Length; request.CookieContainer […]