Tag: json

如何解析Newtonsoft JSON中的TimeSpan值

我想解析JSON字符串并使用token.Type属性来检测JTokenType.TimeSpan类型的JTokenType.TimeSpan 。 我无法弄清楚如何在输入字符串中表达TimeSpan,一切似乎都被解释为JTokenType.String。 var timeSpanString = TimeSpan.FromHours(1).ToString(); testString = string.Format(@”{{“”Value””: “”{0}”” }}”, timeSpanString); var statObject = JObject.Parse(testString); JToken token = statObject[“Value”]; var tokenValue = token.ToString(); var tokenType = token.Type; // JTokenType.String 我甚至尝试过: JValue jValue = new JValue(“test”); jValue.Value = TimeSpan.FromHours(1); bool isTimeSpan = jValue.Type == JTokenType.TimeSpan; // true! testString = string.Format(@”{{“”Value””: “”{0}”” }}”, jValue.Value); var […]

C#:从JSON结构中提取/检索子节点

我们如何从C#中的JSON结构中提取或检索子节点值。 我的应用程序正在使用OpenWeatherMap ,我需要从city检索名称 , temp从列表和天气节点的描述 ,我的JSON和Class结构如下 { “cod”: “200”, “message”: 0.0284, “city”: { “id”: 2643743, “name”: “London”, “coord”: { “lon”: -0.12574, “lat”: 51.50853 }, “country”: “GB”, “population”: 0, “sys”: { “population”: 0 } }, “cnt”: 1, “list”: [ { “dt”: 1429268400, “temp”: { “day”: 12.21, “min”: 4.86, “max”: 13.18, “night”: 4.86, “eve”: 11.76, “morn”: 12.21 […]

如何使用json.net设置json路径的值

我试图在JSON结构中设置一个任意路径,我很难搞清楚如何做一个简单的设定值… 我想要的是像SetValue(path,value)这样的方法,它像SelectToken一样运行,但如果它不存在则创建路径并设置值。 public void SetPreference(string username, string path, string value) { var prefs = GetPreferences(username); var jprefs = JObject.Parse(prefs ?? @”{}”); var token = jprefs.SelectToken(path); if (token != null) { // how to set the value of the path? } else // how to add the path and value, example {“global.defaults.sort”: { “true” }} } […]

如何在C#中过滤JSON数组

我花了很多时间为我的问题找到解决方案。 在这个例子中,我在SetNavRecords数组中有2条记录。 第一个是“Artikelnummer”:“21700” ,第二个是“Artikelnummer”:“21701” 每条记录都有一个数组“OfflineVerkaufspreis”。 对我来说重要的是“OfflineVerkaufspreis”中的“Location_Code”字段我只需要一个已过滤的位置代码的孔信息。 如何选择一个位置代码的数据,例如“MH”? 我正在使用C#和Newtonsoft类进行JSON解析。 我用LINQ尝试了一些版本但没有成功。 { “SetNavRecords” : [ { “Artikelbeschreibung” : “Trikot \”Home\” 2012/2013″, “Artikelbeschreibung2” : “weiß”, “Artikelnummer” : “21700”, “Artikelrabattgruppe” : “MERCH”, “Gutschein” : false, “MwStProduktgruppe” : “VOLLNEU”, “OfflineVerkaufspreis” : [ { “Allow_Line_Discount” : true, “Date” : “2014-05-16T00:00:00”, “Item_No” : “21700”, “Location_Code” : “BP”, “Unit_Price” : 5.0 }, { […]

问题在C#中反序列化JSON日期 – 增加2小时

在将JSON日期反序列化为C#DateTime时,我们遇到了这样一个令人讨厌的问题。 代码是: JavaScriptSerializer serializer = new JavaScriptSerializer(); jsonTrechos = jsonTrechos.Replace(“/Date(“, “\\/Date(“).Replace(“)/”, “)\\/”); Trecho[] model = serializer.Deserialize(jsonTrechos); jsonTrechos是一串json2.js的JSON.stringify(); 。 问题是:反序列化工作,在Trechos对象的所有日期加上2小时。 我的时区是巴西(UTC -3),我们正在夏令时(所以我们目前在UTC -2),如果它有任何关系。 我想也许本地化和时区可能会在这方面发挥作用,如果真的如此,我不知道如何解决它。

Json.net:序列化/反序列化不适用于具有循环引用的ISerializable对象

一段时间后我报告了一个问题 ,我在Json.net 4.5 R11中得到了修复。 如果我的循环引用属性Manager为NULL,则序列化和反序列化工作正常。 但是当循环引用属性Manager设置为NON NULL值时,它会在序列化字符串中被忽略,因此它会在反序列化中引发exception。 Json.net问题基础说问题在你的代码中,但我无法弄清楚。 有人可以帮我吗? 问题: 下面的代码有什么问题吗? 如果是,我应该怎么做才能解决问题? 如果没有,那么在Json.net代码中应该做些什么来解决这个问题呢? 一些更新 :在当前使用二进制序列化的遗留应用程序中需要这样做。 由于更改很大,因此使用Json序列化标记标记序列化中涉及的所有私有字段的工作量太大。 由于Json.net可以对ISerializable对象进行序列化,我们希望这样做。 如果没有循环引用对象,则此方法有效。 我的课程 [Serializable] class Department : ISerializable { public Employee Manager { get; set; } public string Name { get; set; } public Department() { } public Department( SerializationInfo info, StreamingContext context ) { Manager = ( Employee […]

在C#中使用嵌套数组反序列化JSON

我在这里尝试反序列化这个JSON时遇到了麻烦: { “response”: { “numfound”: 1, “start”: 0, “docs”: [ { “enID”: “9999”, “startDate”: “2013-09-25”, “bName”: “XXX”, “pName”: “YYY”, “UName”: [ “ZZZ” ], “agent”: [ “BobVilla” ] } ] } } 我为此创建的类是: public class ResponseRoot { public Response response; } public class Response { public int numfound { get; set; } public int start { […]

使用angular post将两个参数传递给WEB API调用

我的WEB API控制器中有以下post方法: public async Task SendPost(Application application) 我使用angular.js $http.post通过javascript调用它,并将应用程序参数作为JSON传递: $http.post(“/api/AController/SendPost”, JSON.stringify(application)). success(function (data, status, headers, config) { } 这很有效。 现在我想将第二个参数作为一个简单的字符串传递(我无法修改现有的应用程序JSON对象)。我尝试了几种不同的方式在网上建议,但它们似乎都没有工作。 我需要能够像这样做: 控制器: public async Task SendPost(RentalApplication application,string test) 使用Javascript: $http.post(“/api/TessIntegration/SendPost”, {application:JSON.stringify(application),test:”Some value”}). success(function (data, status, headers, config) { }

解析ASP.NET MVC中的JSON值时出错?

我正在尝试使用StackOverflow的搜索API来搜索问题。 我正在使用此操作来执行解析: public ActionResult StackExchange(string sq) { string url = “http://api.stackoverflow.com/1.1/search?intitle=” + sq + “&order=desc”; var client = new WebClient(); var response = client.DownloadString(new Uri(url)); JObject o = JObject.Parse(response);// ERROR int total = (int)o[“total”]; return View(total); } 这是我要解析的JSON url: http://api.stackoverflow.com/1.1/search?intitle=asp.net%20custom%20404&order=desc 我想提取以下数据: `”total”: 3` , `”question_timeline_url”: “/questions/10868557/timeline”,` `”title”: “Asp.net custom 404 not working using Intelligencia rewriter”` […]

JavaScriptSerializer.Deserialize()到字典中

我试图在Json中解析Open Exchange Rates JSON ,我正在使用这种方法: HttpWebRequest webRequest = GetWebRequest(“http://openexchangerates.org/latest.json”); HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse(); string jsonResponse = string.Empty; using (StreamReader sr = new StreamReader(response.GetResponseStream())) { jsonResponse = sr.ReadToEnd(); } var serializer = new JavaScriptSerializer(); CurrencyRateResponse rateResponse = serializer.Deserialize(jsonResponse); 如果我理解JavaScriptSerializer.Deserialize正确我需要定义和对象将Json转换为。 我可以使用这样的数据类型成功地序列化它: public class CurrencyRateResponse { public string disclaimer { get; set; } public string license { get; […]