2017-01-05 23 views
0

我對JSON.NET中的JObjects有個疑問。我從我的Json獲得一個ID。現在,當我點擊一個按鈕時,我想在標籤上顯示JSON的下一個ID。這是有效的,但是當我再次點擊按鈕時,它不會轉到下一個ID。讓我們假設我有六個ID。每當我點擊按鈕,我總是希望它進入下一個ID。如何通過點擊每個按鈕一次一個JObjects數組前進

這裏是我的代碼:

private void MyWindow_Loaded(object sender, RoutedEventArgs e) 
{ 
    dynamic convert = JsonConvert.DeserializeObject(MyProperty); 

    string user = MyProperty; 
    //lbuser.Content = json; 

    //string tan = ""; 
    MainWindow main = new MainWindow(); 
    // main.alpha = tan; 

    string html = string.Empty; 
    string url = @"http://aa.worloud.at/?tag=question&token=" + Property; 

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 
    request.AutomaticDecompression = DecompressionMethods.GZip; 

    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) 
    using (Stream stream = response.GetResponseStream()) 
    using (StreamReader reader = new StreamReader(stream)) 
    { 
     html = reader.ReadToEnd(); 
    } 
    // dynamic magic = JsonConvert.DeserializeObject(html); 
    // string json2 = new JavaScriptSerializer().Serialize(html); 


    var j = new JavaScriptSerializer().DeserializeObject(user) as Dictionary<string, object>; 
    var d = j["data"] as Dictionary<string, object>; 
    lbuser.Content = d["fname"] + " " + d["lname"].ToString(); 

    JObject QuestionObject = JObject.Parse(html); 
    JToken question = QuestionObject["data"].First["q_text"]; 
    lbquestion.Content = question; 


    JObject IDObject = JObject.Parse(html); 
    JToken id = IDObject["data"].First["q_id"]; 
    JToken lastid = IDObject["data"].Last["q_id"]; 
    //JToken nextid = IDObject["data"].First["q_id"]; 
    lbid.Content = "Frage " + id + " von " + lastid; 
} 

class qq 
{ 

} 

private void bt_no_Click(object sender, RoutedEventArgs e) 
{ 
    string html = string.Empty; 
    string url = @"http://aa.worloud.at/?tag=question&token=" + Property; 

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 
    request.AutomaticDecompression = DecompressionMethods.GZip; 

    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) 
    using (Stream stream = response.GetResponseStream()) 
    using (StreamReader reader = new StreamReader(stream)) 
    { 
     html = reader.ReadToEnd(); 
    } 
    JObject IDObject = JObject.Parse(html); 
    JToken nextid = IDObject["data"].First.Next["q_id"]; 
    //int result = (int)nextid; 
    lbid.Content = nextid; 
} 

編輯
這裏是一個控制檯應用程序,希望這將使它更清楚我想做的事:

static void Main(string[] args) 
{ 
    string json = @"{""data"":[{""q_id"":""1"",""q_text"":""banana.""},{""q_id"":""2"",""q_text"":""apple.""}, {""q_id"":""3"",""q_text"":""mango.""},{""q_id"":""4"",""q_text"":""strawberries.""}],""tag"":""question"",""error"":null}"; 

    JObject IDObject = JObject.Parse(json); 
    JToken fruit = IDObject["data"].First["q_text"]; 
    Console.WriteLine(fruit); 

    // I do not know how to do a button click on a console Application, 
    // but this line should be in a button event. And when I click on 
    // the button it should always show the next fruit: first click apple, 
    // second click mango, etc., until the end. 
    JToken nextfruit = IDObject["data"].First.Next["q_text"]; 

    Console.WriteLine(nextfruit); 
    Console.ReadLine(); 
} 
+0

有在貼點擊一個按鈕的代碼沒有任何指示。如果代碼位於按鈕點擊處理程序中,那麼您每次點擊都要解析JSON。代碼如何知道「下一個ID」是什麼? –

+0

我編輯我的問題,所以它看起來像 – myplanet

+0

好吧,我編輯我的問題,現在有一個控制檯應用程序 – myplanet

回答

0

你可以得到在加載時從data數組獲取IEnumerator<JObject>,並將其存儲到類級變量中。然後,點擊每個按鈕,在統計員處撥打enumerator.MoveNext(),然後從enumerator.Current屬性獲取下一個JObject。從那裏你可以得到q_text並顯示它。

我不確定你在建什麼平臺,所以這裏有一個控制檯應用程序來演示這個概念。我將使用按鍵模擬按鈕點擊。

public class Program 
{ 
    public static void Main(string[] args) 
    { 
     MyForm form = new MyForm(); 

     form.LoadJson(); 

     while (form.HasNext) 
     { 
      // simulate button click with a keypress 
      Console.ReadKey(true); 
      form.ButtonClick(); 
     } 
    } 
} 

public class MyForm 
{ 
    private IEnumerator<JObject> Enumerator { get; set; } 
    public bool HasNext { get; private set; } 

    public void LoadJson() 
    { 
     string json = @"{""data"":[{""q_id"":""1"",""q_text"":""banana.""},{""q_id"":""2"",""q_text"":""apple.""}, {""q_id"":""3"",""q_text"":""mango.""},{""q_id"":""4"",""q_text"":""strawberries.""}],""tag"":""question"",""error"":null}"; 

     JObject IDObject = JObject.Parse(json); 
     Enumerator = IDObject["data"].Children<JObject>().GetEnumerator(); 
     ButtonClick(); // Advance to first fruit 
    } 

    public void ButtonClick() 
    { 
     HasNext = Enumerator.MoveNext(); 
     if (HasNext) 
     { 
      JObject nextfruit = Enumerator.Current; 
      Console.WriteLine(nextfruit["q_text"] + " Press any key to advance to the next fruit."); 
     } 
     else 
     { 
      Console.WriteLine("No more fruits."); 
     } 
    } 
} 

輸出(如你按一個鍵每一行出現一次一個):

banana. Press any key to advance to the next fruit. 
apple. Press any key to advance to the next fruit. 
mango. Press any key to advance to the next fruit. 
strawberries. Press any key to advance to the next fruit. 
No more fruits. 
+0

謝謝控制檯Apllication正在工作,但我如何做此代碼行:Console.WriteLine(nextfruit [「q_text」]帶標籤在wpf? – myplanet

+0

嘗試這樣:'label.Content =(string)nextfruit [「q_text」];' –

+0

謝謝您的建議,但我在嘗試之前,它出現了錯誤類型字符串不能隱含轉換爲System.Windows.Controls.Label – myplanet

相關問題