简体   繁体   English

您如何从jquery预订日历专业版反序列化此json?

[英]How do you deserialise this json from jquery booking calendar pro?

I am using a jquery plugin http://envato-help.dotonpaper.net/booking-calendar-pro-jquery-plugin.html for a calendar control. 我正在使用jquery插件http://envato-help.dotonpaper.net/booking-calendar-pro-jquery-plugin.html进行日历控制。 The calendar returns JSON as below. 日历返回JSON ,如下所示。

    "YYYY-MM-DD":{"available": "", // Number of Available Items
                  "bind": 0, // Set if a day is a part of a group (0 = none; 1 = first day of a group; 2 = in the group; 3 = last day of a group)
                  "info": "", // Day informations
                  "notes": "", // Day notes
                  "price": "", // Price
                  "promo": "", // Promotional Price
                  "status": ""}, // Day status (none, available, booked, special, unavailable)
    // Another day              
    "YYYY-MM-DD":{"available": "",
                  "bind": 0,
                  "info": "",
                  "notes": "",
                  "price": "",
                  "promo": "",
                  "status": ""}

Im struggling to understand how to convert this into an object, because of the nesting. 由于存在嵌套,我难以理解如何将其转换为对象。 Shouldn't the date be a name, rather than the actual data? 日期不应该是名称,而不是实际数据吗?

I know that I can use a dictionary like this, 我知道我可以使用这样的字典,

var serializer = new JavaScriptSerializer();
var result = serializer.DeserializeObject(s);

but I would rather have a proper object. 但我宁愿有一个合适的对象。 Can it be done with this JSON string? 可以使用此JSON字符串完成吗?

First I would suggest using Newtonsoft.Json instead of the pre-packaged JavaScriptSerializer, even Microsoft is nowadays using it and has discontinued further development going into the JavaScriptSerializer component as far as I know. 首先,我建议使用Newtonsoft.Json而不是预先打包的JavaScriptSerializer,就连微软现在也正在使用它,并且据我所知,已经停止了对JavaScriptSerializer组件的进一步开发。 Newtonsoft.Json is faster and easier to use. Newtonsoft.Json更快,更易于使用。 See http://james.newtonking.com/json for more information on that. 有关更多信息,请参见http://james.newtonking.com/json

Regardless of what serializer you choose, you can apply the following things to both (maybe some class/method names are different): 无论选择哪种序列化程序,都可以将以下内容应用于两者(也许某些类/方法的名称不同):

I think the easiest approach here would be to declare a Dictionary<string, DayInfo> and try deserializing it using the Deserialize<T> method instead of DeserializeObject . 我认为这里最简单的方法是声明一个Dictionary<string, DayInfo>并尝试使用Deserialize<T>方法而不是DeserializeObject

You need to define a DayInfo class yourself that contains all the properties that you'd like to have deserialized, potentially all of them, eg: 您需要自己定义一个DayInfo类,其中包含您想要反序列化的所有属性,可能包括所有属性,例如:

public class DayInfo {
    public string Available { get; set; } // would map to the "available" property in the JSON
    public int Bind { get; set; } // would map to the "bind" property in the JSON
    // ...
}

You need to make sure that the properties you define on DayInfo are of the correct type. 您需要确保在DayInfo上定义的属性的类型正确。

The deserialization should then be: 反序列化应为:

var dictionary = serializer.Deserialize<Dictionary<string, DayInfo>>(jsonString);

Once you have the JSON deserialized into Dictionary<string, DayInfo> you can still apply further transformations to convert into what you consider a proper object . 将JSON反序列化为Dictionary<string, DayInfo>您仍然可以应用进一步的转换以转换为您认为合适的对象

Here is the resulting code thanks to the answer from PermaFrost. 这是由于PermaFrost的回答而产生的代码。

public class DayInfo
{
    public string Available { get; set; }
    public int Bind { get; set; } 
    public string Info { get; set; }
    public string Notes { get; set; }
    public string Price { get; set; }
    public string Promo { get; set; }
    public string Status { get; set; }
}

.... Then ..... .... 然后 .....

using Newtonsoft.Json;

[HttpPost]
public void SaveCalendarData()
{
    string json = Request.Form["dopbcp_schedule"];
    Dictionary<string, DayInfo> calendarData = JsonConvert.DeserializeObject<Dictionary<string, DayInfo>>(json);

    // Process dictionary...
}

In the html/javascript 在html / javascript中

$(document).ready(function () {

    /* Set up the calendar */
    $('#backend').DOPBackendBookingCalendarPRO({
         'SaveURL': '/Calendar/SaveCalendarData'
    });

// Code omitted.... 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM