How to resolve the algorithm JSON step by step in the Fortran programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm JSON step by step in the Fortran programming language

Table of Contents

Problem Statement

Load a JSON string into a data structure.
Also, create a new data structure and serialize it into JSON. Use objects and arrays (as appropriate for your language) and make sure your JSON is valid (https://jsonformatter.org).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm JSON step by step in the Fortran programming language

Source code in the fortran programming language

program json_fortran
   use json_module
   implicit none

   type phonebook_type
      character(len=:),allocatable :: name
      character(len=:),allocatable :: phone
   end type phonebook_type

   type(phonebook_type), dimension(3) :: PhoneBook
   integer :: i
   type(json_value),pointer :: json_phonebook,p,e
   type(json_file) :: json

   PhoneBook(1) % name = 'Adam'
   PhoneBook(2) % name = 'Eve'
   PhoneBook(3) % name = 'Julia'
   PhoneBook(1) % phone = '0000001'
   PhoneBook(2) % phone = '0000002'
   PhoneBook(3) % phone = '6666666'

   call json_initialize()

   !create the root structure:
   call json_create_object(json_phonebook,'')

   !create and populate the phonebook array:
   call json_create_array(p,'PhoneBook')
   do i=1,3
      call json_create_object(e,'')
      call json_add(e,'name',PhoneBook(i)%name)
      call json_add(e,'phone',PhoneBook(i)%phone)
      call json_add(p,e) !add this element to array
      nullify(e) !cleanup for next loop
   end do
   call json_add(json_phonebook,p) !add p to json_phonebook
   nullify(p) !no longer need this

   !write it to a file:
   call json_print(json_phonebook,'phonebook.json')

   ! read directly from a character string
   call json%load_from_string('{ "PhoneBook": [ { "name": "Adam", "phone": "0000001" },&
   { "name": "Eve", "phone": "0000002" }, { "name": "Julia", "phone": "6666666" } ]}')
   ! print it to the console
   call json%print_file()

end program json_fortran


  

You may also check:How to resolve the algorithm Giuga numbers step by step in the J programming language
You may also check:How to resolve the algorithm Metered concurrency step by step in the zkl programming language
You may also check:How to resolve the algorithm Abundant, deficient and perfect number classifications step by step in the SETL programming language
You may also check:How to resolve the algorithm Entropy step by step in the Jsish programming language
You may also check:How to resolve the algorithm Range consolidation step by step in the Nim programming language