這是我的代碼看起來像我的公共Form1中()靜態與非靜態類
while (accessReader.Read())
{
for (int i = 0; i < count; i++)
{
string urlpart2= accessReader.GetValue(i).ToString();
WebRequest request = WebRequest.Create("urlpart1" + urlpart2+ "urlpart3");
string json;
var response = request.GetResponse();
request.ContentType = "application/json; charset=utf-8";
using (var streamr = new StreamReader(response.GetResponseStream()))
{
json = streamr.ReadToEnd();
List<MyObject> list = JsonConvert.DeserializeObject<List<MyObject>>(json);
var date = MyObject.Start;
//MessageBox.Show(date.ToString());
這是我代表不同的變量我的JSON字符串類返回
public class MyObject
{
public int Type { get; set; }
public string Country { get; set; }
public string Channel { get; set; }
public string Code { get; set; }
public DateTime Start { get; set; }
public DateTime End { get; set; }
}
這是什麼json字符串將返回,唯一的區別將改變可能是類型,代碼,開始和結束。 - 我想要開始和結束值。
[{"Type":1,"Country":"CA","Channel":"","Code":"1EZ","Start":"2014-10-24T00:00:00","End":"2015-10-23T00:00:00"},{"Type":2,"Country":"","Channel":"","Code":"UAD","Start":"2014-10-24T00:00:00","End":"2017-10-23T00:00:00"},{"Type":2,"Country":"","Channel":"","Code":"TPQ","Start":"2014-10-24T00:00:00","End":"2017-10-23T00:00:00"},{"Type":3,"Country":"","Channel":"","Code":"SVC_PRIORITY","Start":"2014-10-24T00:00:00","End":"2017-10-23T00:00:00"}]
我是相當新的編程,我遇到了一個錯誤我真的不明白「是必需的非靜態字段,方法或屬性的對象引用」。
而且我創造了這個班我Form1.cs的範圍內,而不是在我的項目解決方案創建一個新類(如果該事項?)
哇,我明白了!所以爲了像Type:1的開始日期和結束日期,類型2的...和類型3的...我可以做一個foreach(MyObject類型,開始,列表中的最終項目)()'? – Havoux
@Havoux所有的數據都在一個MyObject實例中。當你在你的json中有多個時,遍歷它們是很有用的。還是我誤解了你? –
不用你100%非常感謝你的先生! – Havoux