如何在每次单击按钮时一次浏览一个 JObject 数组



我有一个关于 JSON.NET 中的JObjects的问题。我从我的 Json 那里得到一个 ID。现在,我想在单击按钮时在标签上显示 JSON 中的下一个 ID。这有效,但是当我再次单击该按钮时,它不会转到下一个ID。假设我有六个 ID。每当我点击按钮时,我总是希望它转到下一个 ID。

这是我的代码:

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();
}
您可以在

加载时从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.

相关内容