Image default
Tech

How To Do Parse Json In Java? 

JSON (JavaScript Object Notation) is a lightweight, text-based, language-free information trade design that is simple for people and machines to peruse and compose. JSON can address two organized types: items and clusters.

An item is an unordered assortment of nothing or more name/esteem matches. An exhibit is an arranged grouping of nothing or more qualities. The qualities can be strings, numbers, Booleans, invalid, and these two organized types.

The following is a basic model from Wikipedia that shows the JSON portrayal of an item that depicts an individual. The item has string values for first name and last name, a number incentive for age, item esteem addressing the individual’s location, and a cluster worth of telephone number articles.

{

 

“firstName”: “John”,

 

“lastName”: “Smith”,

 

“age”: 25,

 

“address”: {

        “streetAddress”: “21 second Street”,

        “city”: “New York”,

        “state”: “NY”,

        “postalCode”: 10021

 

},

 

“phoneNumbers”: [

        {

            “type”: “home”,

            “number”: “212 555-1234”

        },

        {

            “type”: “fax”,

            “number”: “646 555-4567”

        }

 

]

}

JSON Processing in Java: The Java API for JSON Processing JSON.simple is a basic Java library that permits parsing, creating,

form, and question JSON.

Getting everything rolling: You want to download the JSON-basic 1.1 container and put it in your CLASSPATH before aggregating and running the underneath model codes.

  •         For bringing in a container in IDE-like obscuration, allude here.
  •         Assuming you are utilizing an expert you might utilize the accompanying expert connection https://mvnrepository.com/ancient rarity/com.google code.JSON-straightforward/JSON-basic/1.1.1

Jason-Simple API: It gives object models to JSON articles and cluster structures. These JSON structures are addressed as item models utilizing types JSONObject and JSONArray. JSONObject gives a Map view to get to the unordered assortment of nothing or more name/esteem matches from the model. Likewise, JSONArray gives a List view to get to the arranged grouping of nothing or more qualities from the model.

Compose Parse JSON to a record

Allow us to see a model that composes the above JSON information into a record “JSONExample.json”, with the assistance of JSONObject and JSONArray.

// Java program for composing JSON to a record

 

import java.io.FileNotFoundException;

 

import java.io.PrintWriter;

 

import java.util.LinkedHashMap;

 

import java.util.Map;

 

import org.json.simple.JSONArray;

 

import org.json.simple.JSONObject;

 

public class JSONWriteExample

{

 

public static void main(String[] args) tosses FileNotFoundException

 

{

 

        // making JSONObject

 

        JSONObject jo = new JSONObject();

 

        // putting information to JSONObject

 

        jo.put(“firstName”, “John”);

 

        jo.put(“lastName”, “Smith”);

 

        jo.put(“age”, 25);

 

        // for address information, first make LinkedHashMap

 

        Map m = new LinkedHashMap(4);

 

        m.put(“streetAddress”, “21 second Street”);

 

        m.put(“city”, “New York”);

 

        m.put(“state”, “NY”);

 

        m.put(“postalCode”, 10021);

 

        // putting address to JSONObject

 

        jo.put(“address”, m);

 

        // for telephone numbers, first make JSONArray

 

        JSONArray ja = new JSONArray();

 

        m = new LinkedHashMap(2);

 

        m.put(“type”, “home”);

 

        m.put(“number”, “212 555-1234”);

 

        // adding a guide to the list

 

        ja.add(m);

 

        m = new LinkedHashMap(2);

 

        m.put(“type”, “fax”);

 

        m.put(“number”, “212 555-1234”);

 

        // adding a guide to the list

 

        ja.add(m);

 

        // putting phoneNumbers to JSONObject

 

        jo.put(“phoneNumbers”, ja);

 

        // composing JSON to file:”JSONExample.json” in cwd

 

        PrintWriter pw = new PrintWriter(“JSONExample.json”);

 

        pw.write(jo.toJSONString());

 

        pw.flush();

 

        pw.close();

 

}

}

Yield from record “JSONExample.json” :

{

 

“lastName”:”Smith”,

 

“address”:{

        “streetAddress”:”21 second Street”,

         “city”:”New York”,

         “state”:”NY”,

         “postalCode”:10021

 

},

 

“age”:25,

 

“phoneNumbers”:[

            {

            “type”:”home”, “number”:”212 555-1234″

            },

         {

            “type”:”fax”, “number”:”212 555-1234″

         }

 

],

 

“firstName”:”John”

}

Note: In JSON, An article is an unordered arrangement of name/esteem matches, so JSONObject doesn’t protect the request for an item’s name/esteem matches, since it is (by definition) not critical. Subsequently, in our result document, the request isn’t safeguarded.

 

Related posts

How to Discuss the Dangers of Sexting with Your Kids?

Anthony W. Johnson

3 Crucial EMR Integrations for Healthcare Practices

Wilbert B. Henry

What are the best ways to get better reach on Instagram?

Anthony W. Johnson