Processing Json data using Newtonsoft

Parent Previous Next

Processing Json data using Newtonsoft


The Newtonsoft.Json library has been added to work with Json data

The official page: https://www.newtonsoft.com/json


As an example, we use a Rest request to the address 

https://jsonplaceholder.typicode.com/posts/1/

to get test data in Json format:


{ "userId": 1, "id": 1, "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto" }


Next, with the help of Newtonsoft.Json processing of the received result.

First, you need to describe a separate class that lists the fields that are present in the Json


    public class TestJson

    {

        public int userId { get; set; }

        public int id { get; set; }

        public string title { get; set; }

        public string body { get; set; }

    }


We execute a Rest request and get the Json data


    string result = await tester.RestGetAsync(@"https://jsonplaceholder.typicode.com/posts/1/", TimeSpan.FromDays(1), "UTF-8");


We process the received data and output them


    TestJson dataJson = JsonConvert.DeserializeObject<TestJson>(result);

    tester.ConsoleMsg("UserID: " + dataJson.userId.ToString());


Full example:


File: ExampleTest3.cs

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Windows.Forms;

using System.Threading;

using System.Threading.Tasks;

using System.IO;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Text.RegularExpressions;

using System.Net;

using System.Net.Http;

using System.Net.Http.Headers;

using System.Reflection;

using Newtonsoft.Json;

using HatFramework;


namespace Hat

{

    // Auxiliary class for JSON data

    public class TestJson

    {

        public int userId { get; set; }

        public int id { get; set; }

        public string title { get; set; }

        public string body { get; set; }

    }

    

    public class ExampleTest3

    {

        Tester tester; // The main variable for the autotest operation


        // The main input function (the autotest starts with this function)

        public async void Main(Form browserWindow)

        {

            tester = new Tester(browserWindow); // Initializing the main variable


            await setUp(); // Функция начала работы с автотестом

            await test(); // Функция выполнения действий теста

            await tearDown(); // Функция завершения работы с автотестом

        }


        public async Task setUp()

        {

            await tester.BrowserFullScreenAsync(); // Sets the browser resolution to full screen

        }


        public async Task test()

        {

            await tester.TestBeginAsync(); // The beginning of the execution of actions

            await tester.GoToUrlAsync("https://jsonplaceholder.typicode.com", 5); // Loading the page at the specified address


            // Executes a Rest request to the API at the specified URL and gets the result in JSON format

            string result = await tester.RestGetAsync(@"https://jsonplaceholder.typicode.com/posts/1/", TimeSpan.FromDays(1), "UTF-8");

            tester.ConsoleMsg(result); // Outputs the result to the console


            // Fetches data from the received JSON

            TestJson dataJson = JsonConvert.DeserializeObject<TestJson>(result);


            // Outputs data to the console

            tester.ConsoleMsg("UserID: " + dataJson.userId.ToString()); 

            tester.ConsoleMsg("ID: " + dataJson.id.ToString());

            tester.ConsoleMsg("Title: " + dataJson.title.ToString());

            tester.ConsoleMsg("Body: " + dataJson.body.ToString());

            

            await tester.TestEndAsync(); // Completing actions

        }


        public async Task tearDown()

        {

            await tester.BrowserCloseAsync(); // Closes браузер

        }

    }

}



Created with the Personal Edition of HelpNDoc: Easily create Web Help sites