3

我从 2 个来源获得了一些数据。其中之一是将所需的数据作为子 json 发送。

"Shifts": [
        {
            "Shift": {
                "ShiftID": 126604,
                "Name": "Volunteers - High Intensity",
                "Description": "sfsd",
                "Venue": "",
                "StartDateTime": "2014-01-28T12:00:00",
                "EndDateTime": "2014-01-28T16:30:00",
                "LocationN": "0.0",
                "LocationE": "0.0"
            }
        }
]

与其他来源相比深一层:

"Shifts": [
            {
                    "ShiftID": 126604,
                    "Name": "Volunteers - High Intensity",
                    "Description": "sfsd",
                    "Venue": "",
                    "StartDateTime": "2014-01-28T12:00:00",
                    "EndDateTime": "2014-01-28T16:30:00",
                    "LocationN": "0.0",
                    "LocationE": "0.0"
            }
    ]

我正在阅读这段代码:

var shiftProperty = json.GetValue("Shifts");
                        if (shiftProperty != null)
                        {
                            ObservableCollection<Shift> shift = new ObservableCollection<Shift>();
                            MemoryStream memorystream = new MemoryStream(Encoding.UTF8.GetBytes(shiftProperty.ToString()));
                            DataContractJsonSerializer serializer = new DataContractJsonSerializer(shift.GetType());
                            shift = serializer.ReadObject(memorystream) as ObservableCollection<Shift>;
                            App.RootFrame.Dispatcher.BeginInvoke(() =>
                            {
                                Shifts = shift;
                            });
                        }

如何以与读取 2nd 格式相同的方式读取 1st 格式的数据?

4

1 回答 1

0
{
"Shifts": [
    {
        "Shift": {
            "ShiftID": 126604,
            "Name": "Volunteers - High Intensity",
            "Description": "sfsd",
            "Venue": "",
            "StartDateTime": "2014-01-28T12:00:00",
            "EndDateTime": "2014-01-28T16:30:00",
            "LocationN": "0.0",
            "LocationE": "0.0"
        }
    }
]
}


public class Shift2
{
    public int ShiftID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Venue { get; set; }
    public string StartDateTime { get; set; }
    public string EndDateTime { get; set; }
    public string LocationN { get; set; }
    public string LocationE { get; set; }
}

public class Shift
{
    public Shift2 Shift { get; set; }
}

public class RootObject
{
    public List<Shift> Shifts { get; set; }
}

现在只需使用反序列化,下载程序集。我会建议http://james.newtonking.com/json

要创建 JSON 的 C# 类表示,可以使用http://json2csharp.com/

于 2014-07-15T22:47:28.423 回答