What is JSON

 


What is JSON


JSON stand for Java Script Object Notation.

We use JSON to Parse data from one device to another or from one language to another language.

JSON is kind of textual data which contain text information.

Using JSON we can communicate with any technology.

Single object always represented by curly braces {}.

Group of objects always represented by square braces [].

JSON data always hold the data in key value pair.


//single objects

{"name":"deep singh","roll":1}

//Group of objects

[

{"name":"deep singh","roll":1},

{"name":"deep singh","roll":1}

]

//java object

Student std=new Student();


//api to convert into json

we have third party api that is Gson Api and by default android provide an json api that is called org.json.


//how to convert java object into json(single object)

  //compose java object

        Student s = new Student();

        s.setRoll(1);

        s.setName("deep singh");

        s.setMarks(78.8f);

        Log.e("error", s.toString());


        //convert java object into json

        Gson g = new Gson();

        String json = g.toJson(s);

        Log.e("error", json);


//how to convert group of java object into json

//compose java object

        Student s = new Student();

        s.setRoll(1);

        s.setName("deep singh");

        s.setMarks(78.8f);


        Student s1 = new Student();

        s1.setRoll(2);

        s1.setName("deep singh1");

        s1.setMarks(78.4f);

        Log.e("error", s1.toString());


        List<Student> list = new ArrayList<>();

        list.add(s);

        list.add(s1);


        //convert java object into json

        Gson g = new Gson();

        String json = g.toJson(list);

        Log.e("error", json);


//how to convert Json into Java Object first way:

    try {

            JSONArray ja = new JSONArray(json);

            for (int i = 0; i < ja.length(); i++) {

                JSONObject jo = ja.getJSONObject(i);

                int roll = jo.getInt("roll");

                String name = jo.getString("name");

                float marks = (float) jo.getDouble("marks");

                Student ss = new Student();

                ss.setRoll(roll);

                ss.setName(name);

                ss.setMarks(marks);


                Log.e("data here", ss.getRoll() + "\t" + ss.getName());

            }

        } catch (Exception e) {

            Log.e("error", e.toString());

        }


//how to convert Json into Java Object another way:

    try {

            JSONArray ja = new JSONArray(json);

            for (int i = 0; i < ja.length(); i++) {

                JSONObject jo = ja.getJSONObject(i);

                Gson gg = new Gson();

                Student ss = gg.fromJson(jo.toString(), Student.class);

                Log.e("data here", ss.getRoll() + "\t" + ss.getName());

            }

        } catch (Exception e) {

            Log.e("error", e.toString());

        }


1 comment:

Anonymous said...

nice explanation