0

I am trying to create my first UWP C# app. When the button named 'Button' is clicked, JSON shows up within the text box named 'TextBox'.

I am trying to work out how I can access only one part of the JSON text (data.best_day.text) for example (in JavaScript) data.best_day.text would give 18 hrs 3 mins. I would like to do the same for this UWP app. I have read some resources but they didn't work or were way too complex to understand. Any help would be greatly appreciated. :)

picture of JSON that is shown when button is clicked

    // The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
    
    namespace WakaTime
    {
        /// <summary>
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// </summary>
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }
    
            private async void Button_Click(object sender, RoutedEventArgs e)
            {
                var client = new HttpClient();
                var text = await client.GetStringAsync("https://wakatime.com/share/@DontBugMie/bef7afe4-102d-47a9-9678-6335510ebedd.json");
              
                TextBox.Text = text;
            }
        }
    }
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
Maya Wright
  • 77
  • 3
  • 9

2 Answers2

1

If you are confident in the path, try this

using System.Text.Json;
...
var jsonDoc = JsonDocument.Parse(text);
var best_day = jsonDoc.RootElement.GetProperty("data").GetProperty("best_day").GetProperty("text").GetString();
TextBox.Text = best_day;

If you need more than one value, then it would be better to create a model (DTO) and deserialize to it

Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
  • Thank you for your help :). I tried this solution but I get a red line under the 'Json' part of the 'using System.Text.Json;' and the 'JsonDocument' part of the JsonDocument.Parse(text). I looked up some solutions to the errors; however, these just ended up giving me more errors haha – Maya Wright Feb 24 '21 at 16:23
  • @MayaWright It is because you didn't specify what version of .net do you use. How can we know that? You need to install NuGet package System.Text.Json. Or use the same from JSON.NET: https://stackoverflow.com/questions/21678126/parse-json-string-to-find-and-element-key-value – Roman Marusyk Feb 24 '21 at 20:20
0

Get the actual JSON and go to a site which converts JSON to C# (Search for that). Also depending on the version Visual Studio, it may have the menu option of Edit - Paste Special - Paste JSON As CLasses which will also work. You will now have classes which you can add to your project which are based off of the JSON.

Then in your code deserialize to the model (or a list of the models). Once it is deserialized into an instance, access best_day as needed.


enter image description here

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • Thank you. I have been able to add the JSON As Classes into my MainPage.xaml.cs file. The deserializing part is proving a little trickier. I have looked at other stack overflow questions and some YouTube videos and a resource online. Most of them didn't work. But one said to add ' Data grandTotal = JsonConvert.DeserializeObject(text);' which I have. However, I am now not sure how to see the text. The line 'Console.WriteLine(grandTotal.grand_total);' doesnt show errors but also doesnt show in the output and 'TextBox.Text = grandTotal.grand_total;' shows errors. Any idea why please? – Maya Wright Feb 24 '21 at 16:48