3

2 つのソースからデータを取得しています。そのうちの 1 つは、必要なデータを子 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"
            }
        }
]

他のソースと比較して 1 レベル深い:

"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;
                            });
                        }

2 番目のフォーマットを読み取るのと同じ方法で、1 番目のフォーマットのデータを読み取るにはどうすればよいですか?

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 に答える