如何在每次单击按钮的情况下一次一个地推进一个JObject数组

时间:2017-01-05 16:34:40

标签: c# json json.net

我对JSON.NET中的JObject有疑问。我从Json那里得到了一张身份证。现在我想在单击按钮时在标签上显示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();
}

1 个答案:

答案 0 :(得分:0)

您可以在加载时从IEnumerator<JObject>数组中获取data并将其存储到类级变量中。然后,在每个按钮上单击,在枚举器上调用enumerator.MoveNext(),然后从JObject属性获取下一个enumerator.Current。从那里你可以得到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.