Tag: json.net

为什么我不能使用Json.Net反序列化这个自定义结构?

我有一个表示DateTime的结构,它也有如下区域信息: public struct DateTimeWithZone { private readonly DateTime _utcDateTime; private readonly TimeZoneInfo _timeZone; public DateTimeWithZone(DateTime dateTime, TimeZoneInfo timeZone, DateTimeKind kind = DateTimeKind.Utc) { dateTime = DateTime.SpecifyKind(dateTime, kind); _utcDateTime = dateTime.Kind != DateTimeKind.Utc ? TimeZoneInfo.ConvertTimeToUtc(dateTime, timeZone) : dateTime; _timeZone = timeZone; } public DateTime UniversalTime { get { return _utcDateTime; } } public TimeZoneInfo TimeZone { […]

强制JsonConvert.SerializeXmlNode将节点值序列化为整数或布尔值

Newtonsoft.Json.JsonConvert类的SerializeXmlNode函数始终在序列化过程中将XML的最后一个子节点的值作为字符串类型输出,有时您可能需要将它们序列化为整数或布尔值。 示例代码: 12 mytitle false 输出: { “ID” : “12”, “Title” : “mytitle”, “Visible” : “false” } 期望的输出: { “ID” : 12, “Title” : “mytitle”, “Visible” : false } 有没有办法强制XML节点序列化为整数或布尔值? 谢谢。 注意:当XML已经序列化为JSON字符串时,请避免发布变通办法,因为这些变通办法是我们愿意避免的。

如何序列化原始json字段?

我在db中有一个存储json字符串的字段,当我在json结果中返回它时,我想要它将作为json原始数据返回,而不是用引号作为字符串扭曲。 更新1(更多信息):如果您查看图像字段,它包含原始的json字符串值 但是在用JsonResult序列化之后,它会被引号扭曲,因为它是一种String类型,我怎么能告诉序列化器将图像字段视为原始json数据呢? var db = new ModelsContainer(); var res = db.Images.OrderByDescending(i=>i.DateCreated).Skip(skip).Take(take).Select( i => new { id = i.Id, dateCreated = i.DateCreated, images = i.Images , user = new { id = i.User.Id, facebookId = i.User.FacebookId, displayName = i.User.DisplayName }, tags = i.Tags.Select( t => t.Value ) }).ToList(); return Json(res, JsonRequestBehavior.AllowGet); [ { “id”:”5c528e88-f3a7-4b30-9746-980867325fd1″, “dateCreated”:”\/Date(1364381593000)\/”, […]

使用JSON.NET获取JSON值的路径

我试图找到一个JSON值的路径。 考虑以下JSON: { “car”: { “type”: [{ “sedan”: { “make”: “honda”, “model”: “civics” } }, { “coupe”: { “make”: “ford”, “model”: “escort” } }] } } 我怎样才能获得价值“本田”的路径? 我想找到这样的东西…… car_type_0_sedan_make_ honda JSON.NET是否支持此function? 我看到有一个JToken.Path属性,但它目前不可用。 http://json.codeplex.com/workitem/24136

如何反序列化不同类型的集合?

我有一个看起来像这样的JSON提要(我删除了一些这个例子不需要的字段): { “total_count”: 2, “num_pages”: 1, “current_page”: 1, “balance”: { “amount”: “0.00001199”, “currency”: “BTC” }, “transactions”: [ { “transaction”: { “id”: “5018f833f8182b129c00002f”, “created_at”: “2012-08-01T02:34:43-07:00”, “sender”: { “id”: “5011f33df8182b142400000e”, “name”: “User Two”, “email”: “user2@example.com” }, “recipient”: { “id”: “5011f33df8182b142400000a”, “name”: “User One”, “email”: “user1@example.com” } } }, { “transaction”: { “id”: “5018f833f8182b129c00002e”, “created_at”: “2012-08-01T02:36:43-07:00”, “hsh”: “9d6a7d1112c3db9de5315b421a5153d71413f5f752aff75bf504b77df4e646a3”, […]

为什么有JConstructor?

Json.NET定义了一个JConstructor类型 。 这很令人困惑,因为(据我所知)构造函数不是JSON的一部分。 我仔细检查了JSON规范并浏览了json.org但却找不到任何东西。 网上任何地方似乎都没有关于此类型的文档。 因为Json.NET被如此广泛地使用(它甚至由微软委托),我认为必须有一些合理的动机将这种表示包含在对象模型中。 问题是,我决定动机的任何尝试都只是猜测。 我测试了类型及其序列化,明显的行为是只包装JavaScript代码,例如new constructorName(…) ,例如: new JConstructor(“ctorExample”, new JValue(“value”), new JObject( new JProperty(“prop1”, new JValue(1)), new JProperty(“prop2”, new JValue(2))) ) .ToString() 输出 new ctorExample( “value”, { “prop1”: 1, “prop2”: 2 } ) 那么, JConstructor类型旨在表示什么以及它为什么存在?

如何反序列化JSON数组并忽略根节点?

我有来自服务器的下一个响应 – {“response”:[{“uid”:174952xxxx,”first_name”:”xxxx”,”last_name”:”xxx”}]} 我试图以下一种方式反序化 – JsonConvert.DeserializeObject(json); 其中T = VkUser的列表,但是我收到了错误。 [JsonObject] public class VkUser { [JsonProperty(“uid”)] public string UserId { get; set; } [JsonProperty(“first_name”)] public string FirstName { get; set; } [JsonProperty(“last_name”)] public string LastName { get; set; } } 我总是试着 public class SomeDto // maybe Response as class name will fix it but I don’t […]

Json.NET MissingMemberHandling设置

当Json字符串缺少C#类所需的属性时,我希望Json.NET抛出JsonSerializationException 。 还有MissingMemberHandling Enumeration 在反序列化期间遇到缺少的成员时抛出JsonSerializationException。 但我认为这与我想要的相反。 我认为这意味着c#类中缺少一个成员。 我想要一个失踪的Json成员。 我的代码是 public MyObj Deserialise(string json) { var jsonSettings = new JsonSerializerSettings(); jsonSettings.MissingMemberHandling = MissingMemberHandling.Error; return JsonConvert.DeserializeObject(json, jsonSettings); } 例如 public class MyObj { public string P1 { get; set; } public string P2 { get; set; } } string json = @”{ “”P1″”: “”foo”” }”; json缺少P2。 我想知道这是什么情况。 […]

Json:如何使用json.net正确剥离转义字符

我有以下格式的json响应。 “[{\\\”JobID\\\”:\\\”1\\\”,\\\”BillGenerationDate\\\”:\\\”4/29/2013 2:53:34 PM\\\”,\\\”BillID\\\”:\\\”115743\\\”,\\\”BillNo\\\”:\\\”115743\\\”,\\\”CustomerID\\\”:\\\”4041705\\\”,\\\”PayStatus\\\”:\\\”0\\\”,\\\”PaymentRequiredStatus\\\”:\\\”True\\\”,\\\”ProductName\\\”:\\\”Epic FBO test\\\”,\\\”Description\\\”:\\\”Epic Automation 2\\\\r\\\\n\\\”,\\\”ProductType\\\”:\\\”eBill \\\”,\\\”DueType\\\”:\\\”-1\\\”,\\\”DueDate\\\”:\\\”2013-03-15\\\”,\\\”Amount\\\”:\\\”63.70\\\”,\\\”Cost\\\”:\\\”\\\”}, {\\\”JobID\\\”:\\\”9\\\”,\\\”BillGenerationDate\\\”:\\\”5/2/2013 10:21:39 AM\\\”,\\\”BillID\\\”:\\\”115743\\\”,\\\”BillNo\\\”:\\\”115743\\\”,\\\”CustomerID\\\”:\\\”4041705\\\”,\\\”PayStatus\\\”:\\\”0\\\”,\\\”PaymentRequiredStatus\\\”:\\\”True\\\”,\\\”ProductName\\\”:\\\”FBO Test Product\\\”,\\\”Description\\\”:\\\”FBO Product Test\\\”,\\\”ProductType\\\”:\\\”eBill \\\”,\\\”DueType\\\”:\\\”-1\\\”,\\\”DueDate\\\”:\\\”2013-05-01\\\”,\\\”Amount\\\”:\\\”150.70\\\”,\\\”Cost\\\”:\\\”\\\”}] 我相信json.net处理转义字符,我使用下面的代码将它反序列化为字典集合。 var billList = JsonConvert.DeserializeObject<List<Dictionary>>(contentCorrected); 但是这个json解析会抛出exception“无效的属性标识符:.Path'[0]’,第1行,第2位。” 我们可以通过操纵json响应字符串来解决这个问题吗?

如何使用Json.Net反序列化枚举数组?

我有这样的JSON: [{ “agencyId”: “myCity”, “road”: { “note”: “”, “lat”: “45.321”, “lon”: “12.21”, “streetCode”: “290”, “street”: “street1”, “fromNumber”: “”, “toNumber”: “”, “fromIntersection”: “”, “toIntersection”: “” }, “changeTypes”: [“PARKING_BLOCK”, “ROAD_BLOCK”], },] 和这样的一个类: public class AlertRoad : BaseAlert { [JsonProperty(“agencyId”)] [JsonConverter(typeof(StringEnumConverter))] public AgencyType AgencyId { get; set; } [JsonProperty(“changeTypes”)] [JsonConverter(typeof(StringEnumConverter))] public ChangeType[] ChangeTypes { get; set; } [JsonProperty(“road”)] […]