我對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();
}
有在貼點擊一個按鈕的代碼沒有任何指示。如果代碼位於按鈕點擊處理程序中,那麼您每次點擊都要解析JSON。代碼如何知道「下一個ID」是什麼? –
我編輯我的問題,所以它看起來像 – myplanet
好吧,我編輯我的問題,現在有一個控制檯應用程序 – myplanet