Tag: json

JSON序列化中属性名称的默认camel案例

我有一堆类将在某些时候被序列化为JSON,为了遵循后端的C#约定和前端的JavaScript约定,我一直在定义这样的属性: [JsonProperty(PropertyName=”myFoo”)] public int MyFoo { get; set; } 所以在C#中我可以: MyFoo = 10; 在Javascript中,我可以: if (myFoo === 10) 但是为每个房产做这件事都很乏味。 是否有一种快速简便的方法来设置JSON.Net处理属性名称的默认方式,因此除非另有说明,否则它将自动显示案例?

使用Json.Net在MVC中返回未转义的Json

我在使用Json.Net在MVC项目中找出如何返回未转义的Json时遇到了令人惊讶的麻烦。 它完全暗指我,但我确定我错过了一些简单的东西…… 到目前为止,我序列化了一个基本对象,并让Json.Net对其进行序列化: public JsonResult GetTimelineJson() { var result = new MyGraph([some data…]); return Json(JsonConvert.SerializeObject(result), JsonRequestBehavior.AllowGet); } 结果: “{\r\n \”id\”: \”myGraph\”,\r\n \”title\”: \”Graph title\”,\r\n […] 任何将它包装成HtmlString等的尝试都会导致在线上传递一个空集(尽管调试点显示它正确地未转义)。 我已经检查过HTTP标头中是否正确设置了内容类型。 任何和所有的帮助表示赞赏,谢谢。

在C#中使用newtonsoft查找并返回JSON差异?

我想得到一个使用Newtonsoft进行比较时不匹配的JSON部分列表。 我有这个代码比较: JObject xpctJSON = JObject.Parse(expectedJSON); JObject actJSON = JObject.Parse(actualJSON); bool res = JToken.DeepEquals(xpctJSON, actJSON); 但找不到任何可以返回差异的东西。

如何将DateTime从JSON转换为C#?

可能重复: 如何将UNIX时间戳转换为DateTime,反之亦然? 我有以下课程: [DataContractAttribute] public class TestClass { [DataMemberAttribute] public DateTime MyDateTime { get; set; } } 这是JSON: { “MyDateTime”:”1221818565″ } JSON正在从PHP Web服务返回。 我需要做的是将该epoch字符串转换为有效的C#DateTime。 这样做的最佳方法是什么? 我可以做这个: [IgnoreDataMemberAttribute] public DateTime MyDateTime { get; set; } [DataMemberAttribute(Name = “MyDateTime”)] public Int32 MyDateTimeTicks { get { return this.MyDateTime.Convert(…); } set { this.Created = new DateTime(…); } } 但问题是,MyDateTimeTicks是公共的(将其更改为private会导致序列化过程中出现exception)

使用Json.Net失败的多态JSON反序列化

我正在尝试使用自定义JsonConverter将一些JSON反序列化为各种子类 我几乎遵循了这一点。 我的抽象基类: abstract class MenuItem { public String Title { get; set; } public String Contents { get; set; } public List Submenus { get; set; } public String Source { get; set; } public String SourceType { get; set; } public abstract void DisplayContents(); } 而我派生的JsonConverter : class MenuItemConverter : JsonConverter { public […]

使用System.Web.Script.Serialization.JavascriptSerializer反序列化JSON – 如何?

注意:我发布了一个类似的问题 ,这是这个问题的祖先,因为我最初考虑使用JSON.NET来解析JSON,但我正在使用内置的反序列化器,所以这是一个不同的问题。 这就是我要做的事情:例如,我有一个名为Item的类。 json有许多“元素”(如果它们被称为 – 它们模仿Item类),每个元素包含3个字段:一个名为id的整数,一个名为name的字符串和一个名为creationTime的日期时间。 我想将所有这些Item“元素”从json解析为Item对象列表。 我在Item类中创建了3个字段以匹配JSON。 这就是我目前正在做的事情: JavaScriptSerializer ser = new JavaScriptSerializer(); List items = ser.Deserialize(new StreamReader(response.GetResponseStream()).ReadToEnd()); 但是,这不起作用,因为我“不能隐式地将类型’superapi.Item’转换为’System.Collections.Generic.List <superapi.Item >’”。 因此,我不知道如何处理这个问题,因为JSON中的Item架构有许多元素。 是否有可能在foreach循环中执行此操作,在JSON中查找每个反序列化的项,将其添加到列表中,并继续循环? 我将尝试使用一些类似的代码来做这件事,我将发布我的结果。 谢谢! 更新:这是一些JSON的样子: [{ “Id”: 1, “Name”: “First item name”, “creationTime”: “\/Date(1247258293690)\/” }, { “Id”: 2, “Name”: “Second item name”, “creationTime”: “\/Date(1247088323430)\/” }] 这是我的Item类的样子: public class Item { public int Id […]

使用DataContractJsonSerializer将数组值反序列化为.NET属性

我正在使用Silverlight 4中的DataContractJsonSerializer,并希望反序列化以下JSON: { “collectionname”:”Books”, “collectionitems”: [ [“12345-67890”,201, “Book One”], [“09876-54321”,45, “Book Two”] ] } 进入以下类: class BookCollection { public string collectionname { get; set; } public List collectionitems { get; set; } } class Book { public string Id { get; set; } public int NumberOfPages { get; set; } public string Title { get; […]

如何在WebAPI后端C#上接收JSON数据?

如何在C#中的WebAPI后端接收JSON数据? 我从我的JavaScript前端发送了以下JSON。 { “User_Id”: 1, “TotalPrice”: 35, “DeliveryAddress”: “At my house”, “CartItems”: [ { “Id”: 1009, “Name”: “Superman juni 2014”, “Quantity”: 1, “Price”: 35 } ] } 我有这个课程: public class PurchaseOrder { public List CartItems { get; set; } public string DeliveryAddress { get; set; } public int TotalPrice { get; set; } public int […]

阅读HttpwebResponse json响应,C#

在我的一个应用程序中,我收到了来自webrequest的回复。 该服务是Restful服务,将返回类似于以下JSON格式的结果: { “id” : “1lad07”, “text” : “test”, “url” : “http:\/\/twitpic.com\/1lacuz”, “width” : 220, “height” : 84, “size” : 8722, “type” : “png”, “timestamp” : “Wed, 05 May 2010 16:11:48 +0000”, “user” : { “id” : 12345, “screen_name” : “twitpicuser” } } 这是我目前的代码: byte[] bytes = Encoding.GetEncoding(contentEncoding).GetBytes(contents.ToString()); request.ContentLength = bytes.Length; using (var requestStream = […]

为什么在web api中返回一个具有一对多关系的实体会导致错误?

伙计我和同一个class级有一对多的关系叫做用户, 我在web apis get方法中返回一个用户实例,只要我没有,它就可以正常工作 User ID | Name 0 | A 1 | B Friends Table ( This table is used to build the one to many relationship ) User_ID | Friend_ID 1 | 0 0 | 1 这是我的GetMethod [HttpGet] public Models.User Authenticate() { try { return db.Users.SingleOrDefault(x => x.ID == 0 ) ; } […]