SIGN IN YOUR ACCOUNT TO HAVE ACCESS TO DIFFERENT FEATURES

FORGOT YOUR PASSWORD?

FORGOT YOUR DETAILS?

AAH, WAIT, I REMEMBER NOW!
Openbaraza

Openbaraza

Openbaraza

Baraza is a Swahili word that means a deliberation meeting held by a collective group of people with wisdom.

T : +254 (20) 2243097 / 2227100
Email: info@openbaraza.org

OPENBARAZA
Dew CIS Solutions LTD Barclays Plaza, 12th Floor. Loita Street.

Open in Google Maps
  • Framework
  • Projects
    • Human Resource
      • Human Resource
      • Time Attendance
      • Project Planning
    • Finance Management
      • Business
      • Payroll
    • Investment Management
      • Banking
      • Chama
      • Sacco
    • Case Management
      • Judiciary
    • Real Estate Management
      • Property
    • Academic Management
      • UMIS
      • Schools
  • Forums
    • HCM HR Payroll
    • Business
    • Sacco
    • Banking
  • Blog & News
    • OpenBaraza Framework
    • Databases
    • Java Programming
    • Scrum
  • Contact Us
  • Home
  • Blog & News
  • Java Programming
  • 06. Classes: Store Student Details Using Json Format
February 7, 2023

06. Classes: Store Student Details Using Json Format

06. Classes: Store Student Details Using Json Format

by dennis@dennis.me.ke / Wednesday, 18 September 2019 / Published in Java Programming

JSON, is not a name of some tall, handsome guy that ladies always stare at or some guy that boys wish to grow into, just in case you are wondering! No, our JSON is an acronym for JavaScript Object Notation. It  is a syntax for storing and exchanging data written with JavaScript Object Notation.

 

It outputs data surrounded by curly braces and quotes separated by commas, making it easily readable by humans and at the same time efficiently parsed by code. Json string, outputs data in continous line.For this code we shall be using JSON raw to explain what classes are and how they are used in java.

 

Basically  a JSON string should appear as;

Example : {“Name”:”Jack”,”Age”:”24”,”Sex”:”Female”}

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

LIVE A LITTLE:

These two strings walk into a bar and sit down. The bartender says, “So what’ll it be?”

The first string says, “I think I’ll have a beer quag fulk boorg jdk^CjfdLk jk3s d#f67howe%^U r89nvy owmc63^Dz x.xvcu”

“Please excuse my friend,” the second string says, “He isn’t null-terminated.”

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

 

Let’s look at the code below of the Student class:

 

This other code for Project class:

 

Output:

 

 

Explanation:

This is the Student Class which does not have the main class hence cannot be compiled;

//Start of the student class

public class Student {

        //Declare variables as public to make them accessible by other classes

        public String name;

public String age;

public String sex;

 

        /*Setter and Getter Methods for the Student Class Attributes which are used to effectively    protect your data when creating classes.

            set() method sets the value followed by the variable name capitalised. e.g setName()

            set() method takes a parameter and assigns it to the attribute.

 

            get() method returns its value followed by the variable name capitalized. It returns the value of attribute. it’s written as getName().

*/

            public void setName(String name) {

this.name = name;

}
public String getName() {

return name;
}

 

public void setAge(String age) {

this.age = age;
}

public String getAge() {

return age;

}

 

            public void setSex(String sex) {

this.sex = sex;
}
public String getSex() {

return sex;

}

       

            //Method that returns the value of the Student object as  a JSON  String

            public String getJson() {

 

            //Initializing the Json characters as a String

                        String brace = “{“;

String close = “}”;

String quote = “\””;

String colon = “:”;

String comma = “,”;

 

            //Creating the Json Raw String

                        String json = brace + quote + “Name” + quote

+ colon + quote + name + quote

+ comma

+ quote + “Age”+ quote

+ colon + quote + age + quote

+ comma

+ quote + “Sex” + quote + colon + quote + sex + quote + close;

return json;

                       

            }//End of getJson method

}//End of Student class

 

This is the Project class which contains the main method;

 

import java.util.Scanner;

//Start of the Project class

public class Project {

 

//start of the main method

            public static void main(String []args){

                        //Call the readStudent method to the main method

                        Student st1 = readStudent();

System.out.println(st1.getJson());

}//End of main method

 

            //Method to read and return a student object

            public static Student readStudent() {

                        //Call the student class to be used as an object

                        Student st = new Student();

            //Prompts user to enter values for first name, age and gender respectively and stores the value in object st

                        Scanner n = new Scanner(System.in);

System.out.print(“First Name: “);

//the object st stores the input entered. For this case, the name.

st.setName(n.next());

 

System.out.print(“Age: “);

st.setAge(n.next());

 

System.out.print(“Gender: “);

st.setSex(n.next());

System.out.println();

                        //returns the value of the object

                        return st;

            }//End of readStudent method

           

}//End of Project Class

 

We followed the following steps in order to achieve the above task.

  1. Understanding JSON and how it works.
  2. Use JSON to understand classes in java and how they are used.
  3. Understanding and Explaining the code.
0
  • Tweet

About dennis@dennis.me.ke

What you can read next

02. Learning Java in Linux
03. Operators
05. Variables

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • 08. BODMAS IN JAVA

  • 07. JAVA ARRAYS: Store Student Details Using Arrays and List

  • 02. SPRINT RETROSPECTIVE

  • 01. SCRUM (DEMO)

  • 05. Variables

Categories

  • Databases
  • Java Programming
  • OpenBaraza Framework
  • Scrum

QUICK LINKS

  • Home
  • Our Stories
  • Contact Us
  • Terms and Conditions
  • Our Policy

VISIT US

Open Baraza
Barclays Plaza, 12th Floor.
Loita Street, Nairobi.

Open in Google Maps

GET IN TOUCH

Tel: +254 (20) 2243097 / 2227100
     +254(726)209214, +254(738)819505
Email: info@openbaraza.org

Follow Us

323 days agoRT @dewcis: Manage your HR,Payroll and Time attendance with just a click of a button with our Easy to use Baraza Human Capital Management(H…
over a year agoInstalling Java Development Kit(JDK) Tutorial #Java #javascript" title="https://t.co/a2K2VzYhYq #Java #javascript" target="_blank">https://t.co/a2K2VzYhYq #Java #javascript
over a year agoNew Blog alert #java#arrays" title="https://t.co/EBDMlqL2Kq #java#arrays" target="_blank">https://t.co/EBDMlqL2Kq #java#arrays #classes
over a year agoAre you a fresh university graduate with a computer related degree and interested in being a software developer or… https://t.co/94CzteMjLq
over a year agoCheck out our new blog on operators #operators #codinglife" title="https://t.co/vOcebUw1xI #operators #codinglife" target="_blank">https://t.co/vOcebUw1xI #operators #codinglife
over a year agoLearning Java language on windows #Windows" title="https://t.co/HfsLDSWcVP #Windows" target="_blank">https://t.co/HfsLDSWcVP #Windows #Java #javascript #codinglife
over a year agoRT @dewcis: Fair Trade Software Foundation (FTSF) is venturing into a new cooperative enterprise named workpamoja. As part of the initiativ…
over a year agoWhat is SQL? Why SQL? SQLProcess? This are some the question our continuing database series article will be answeri… https://t.co/HgiUf3XwYz
over a year agoAs businesses grow, there is a need for the business owners to embrace emerging technologies. Check out our continu… https://t.co/Vq6gaF3n1o
over a year agoLike storage boxes, a database is an arranged chest of tables and columns that hold information. Check our our late… https://t.co/krRINpg03x
Follow @openbaraza
  • GET SOCIAL
Openbaraza

© All rights reserved | Open Baraza developed by DewCis Solutions.

TOP