Tag: json.net

嵌套的Json String到DataTable

我需要将以下Json字符串转换为DataTable。 { “pnr”:”1234567890″, “train_num”:”12311″, “train_name”:”HWH DLIKLK MAI”, “doj”:”23-12-2013″, “from_station”: { “code”:”DLI”, “name”:”Delhi” }, “to_station”: { “code”:”KLK”, “name”:”Kalka” } } 在DataTable中我需要显示 train_num train_name doj from_station(name only) to_station(name only) 我到现在所拥有的是, public class Train { public string train_num { get; set; } public string train_name { get; set; } public string doj { get; set; } public from_station […]

使用catch all dictionary属性将json序列化为一个对象

我想使用JSON.net反序列化为对象,但将未映射的属性放在字典属性中。 可能吗? 比如给json, {one:1,two:2,three:3} 和c#类: public class Mapped { public int One {get; set;} public int Two {get; set;} public Dictionary TheRest {get; set;} } JSON.NET可以反序列化为值为1 = 1,2 = 1的实例,TheRest = Dictionary {{“three,3}}

使用带有JSON.Net的自定义反序列化器反序列化JSON

我有这样的JSON: { “MobileSiteContents”: { “au/en”: [ “http://www.url1.com”, “http://www.url2.com”, ], “cn/zh”: [ “http://www.url2643.com”, ] } } 我正在尝试将其反序列化为IEnumerable类,如下所示: public class MobileSiteContentsContentSectionItem : ContentSectionItem { public string[] Urls { get; set; } } public abstract class ContentSectionItem { public string Culture { get; set; } } 那可能吗? 我意识到我可能需要为此使用Custom JsonConverter,但找不到任何示例。 我开始编写一个使用JObject.Parse进行转换的方法,但不确定这是否是正确/最有效的路径: public IEnumerable Parse(string json) { var jobject = […]

始终有错误“ObjectContent 1类型无法序列化响应正文…”

我使用Web api从数据库中检索数据。 我只有一个表“tblMessage”,并希望从该表中获取数据。 我设置了所有内容但是当我运行网站时。 错误总是说 ‘ObjectContent`1’类型无法序列化内容类型’application / xml的响应主体 我在stackoverflow上读了一些post,说明错误可以通过告诉浏览器以json格式输出数据来修复。 之后,错误就变成了 ‘ObjectContent`1’类型无法序列化内容类型’application / json的响应主体 我已尝试过以下post中的所有解决方案,但他们没有解决问题(浏览器报告相同的错误) Web API错误:’ObjectContent`1’类型无法序列化内容类型的响应正文 无法序列化内容类型的响应正文 Web API错误:’ObjectContent`1’类型无法序列化内容类型的响应正文 这个错误究竟是什么? public interface IMessage { IQueryable GetAll(); } public class Message { [Key] public int i_StmID { get; set; } public string vch_MsgString { get; set; } } public class EFDBContext : DbContext { public DbSet […]

ASP.NET MVC 6上每个控制器的特定JSON设置

我需要在ASP.NET MVC 6 webApi中为每个控制器设置特定的JSON设置。 我发现这个样本(我希望!)适用于MVC 5: 在每个控制器的ASP.NET WebAPI上强制使用CamelCase using System; using System.Linq; using System.Web.Http.Controllers; using System.Net.Http.Formatting; using Newtonsoft.Json.Serialization; public class CamelCaseControllerConfigAttribute : Attribute, IControllerConfiguration { public void Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor) { var formatter = controllerSettings.Formatters.OfType().Single(); controllerSettings.Formatters.Remove(formatter); formatter = new JsonMediaTypeFormatter { SerializerSettings = {ContractResolver = new CamelCasePropertyNamesContractResolver()} }; controllerSettings.Formatters.Add(formatter); } }

将动态类型转换为字典C#

我有一个看起来像这样的动态对象, { “2” : “foo”, “5” : “bar”, “8” : “foobar” } 如何将其转换为dictionary ?

无法加载文件或程序集’Newtonsoft.Json,Version = 7.0.0.0

我正面临下面的错误 无法加载文件或程序集“Newtonsoft.Json,Version = 7.0.0.0,Culture = neutral,PublicKeyToken = 30ad4fe6b2a6aeed”或其中一个依赖项。 定位的程序集的清单定义与程序集引用不匹配。 (HRESULTexception:0x80131040) 我可以在Web.config中看到下面的内容 所以我改成了 在packeges.config中,我可以看到以下条目 但我仍面临同样的问题。 请帮忙

使用Web API和JSON.NET序列化对象时,防止$ id / $ ref

在序列化对象时,我似乎无法阻止Web API / JSON.NET使用Newtonsoft.Json.PreserveReferencesHandling.Objects 。 换句话说,尽管使用了以下设置,但$ id / $ ref总是在序列化对象中使用: public class MvcApplication : System.Web.HttpApplication { protected void Application_Start () { WebApiConfig.Register(GlobalConfiguration.Configuration); } } public static class WebApiConfig { public static void Register (HttpConfiguration config) { JsonMediaTypeFormatter jsonFormatter = config.Formatters.OfType().Single(); jsonFormatter.UseDataContractJsonSerializer = false; jsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented; jsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; jsonFormatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None; } } 有任何想法吗?

如何使用JSON.Net反序列化为匿名类型?

只是尝试从JSON创建一个匿名类型,而不必提前了解任何参数,并完全解释它们(可能带有提示)。 即该值“看起来”像int,string或date。 到目前为止,我所知道的唯一方法是创建一个您已经预先了解的匿名类型。 请参阅下面的.DeserializeAnonymousType(…)方法。 任何人都可以比这更好吗? 谢谢。 var jsonString = “{\”user_id\”: 1, \”user_type\”: \”moderator\”, \”name\”: \”Fred\”}”; JToken root = JObject.Parse(jsonString); var anonTemplate = new{user_id=0, user_type=””, name=”” }; var a = JsonConvert.DeserializeAnonymousType(root.ToString(), anonTemplate); var b = JsonConvert.DeserializeObject(root.ToString()); // actually turns into a JsonObject which is something differet. 更新 我下载了dynamicduck并且正在玩它。 这种奇怪的动态“包装”理念是否会以我需要的方式(可序列化等)实现? http://weblogs.asp.net/britchie/archive/2010/08/05/json-net-dynamic-extensions.aspx http://weblogs.asp.net/britchie/archive/2010/08/03/dynamicduck-duck-typing-in-a-dynamic-world.aspx

C#操作JSON数据

我有一个“简单”的场景:读取一些JSON文件,过滤或更改一些值,然后将生成的json写回,而不更改原始格式。 所以例如改变这个: { “type”: “FeatureCollection”, “crs”: { “type”: “EPSG”, “properties”: { “code”: 28992 } }, “features”: [ { “type”: “Feature”, “geometry”: { “type”: “Polygon”, “coordinates”: [ [ [ 149886.192, 374554.705 ], [ 149728.583, 374473.112 ], [ 149725.476, 374478.215 ] ] ] } } ] } 进入这个: { “type”: “FeatureCollection”, “crs”: { “type”: “EPSG”, “properties”: { […]