How to Parse JSON from URL in Java

In this instructional exercise, you will figure out how to parse JSON from URL in java.
If you don’t know how to parse JSON from URL in java then you are at the right place to know your problem’s solution.

Parse JSON From URL In Java

Let’s start with what JSON is-JSON javascript object notation is simply a data representation format very similar to XML.
It is commonly used for API and configurations. It’s used because of lightweight and easy to read and write. It integrates easily with most of the languages.
Throughout your programming career, you’re going to use JSON all the time whether it’s creating an API or consuming an API or for creating config files for you to use or for other people to use for your application.

Let’s dive into some of the syntax involved in JSON by starting the types that JSON can represent. As we know JSON is a data representation format so we need to able to represent certain data types within it and JSON supports Strings, Numbers, Booleans, Null which represents nothing and Arrays which can be a list of any data types and an Object.

Also learn:

The first thing you need to do is to create a file with the dot JSON extension and that will create your JSON file inside of this file you need to take one of the types that JON supports and you put that inside of your file example you could put a string of numbers or whatever data you want.

Here below is the JSON file named user.json, you can create it by-

//JSON file with json extension i.e.,user.json
{
    "name":"shubham",
    "favourite no":4,
    "is programmer":"true",
    "age":19,
    "address":{
               "street":"iel colony",
               "city":"bokaro",
               "state":"jharkhand",
               "postal code":829112
              }
}

The below code will help you to achieve your goal-




import java.io.*; 
import java.util.*;  
import org.json.simple.*;  
import org.json.simple.parser.*; 
  
public class JSONparse 
{ 
    public static void main(String[] args) throws Exception  
    { 
        // parsing file "user.json" 
        Object object = new JSONParser().parse(new FileReader("http://localhost/BCA_B2/user.json")); 
        JSONObject skr = (JSONObject) object;  
        String Name = (String) skr.get("name"); 
        long favouriteno = (long) skr.get("favourite no"); 
        String isprogrammer=(String) skr.get("is programmer"); 
        System.out.println(Name); 
        System.out.println(favouriteno); 
        System.out.println(isprogrammer);
        long age = (long) skr.get("age"); 
        System.out.println(age); 
        Map address = ((Map)skr.get("address")); 
        Iterator<Map.Entry> itr1 = address.entrySet().iterator(); 
        while (itr1.hasNext()) { 
            Map.Entry pair = itr1.next(); 
            System.out.println(pair.getKey() + " : " + pair.getValue()); 
    }
}

Below is the output of the code-

Shubham
4
true
19
street:iel colony
city:bokaro
state:jharkhand
postal code:829112

 


Leave a Reply

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