Jason Parser Simple JavaExample

  





While   coding application  we  need to parse  the  Json  file  , this example  give you  step by step coding  in java to parse a  simple Json file.


Maven  dependency   to  run this code .  OR  add  jar   file  file   in the  class path 

json-simple-1.1.1.jar



    com.googlecode.json-simple
    json-simple
    1.1



bhubaba.json



{
  "id": 1,
  "firstname": " Bhikubaba",
  "languages": [
    {
      "lang": "en",
      "knowledge": "proficient"
    },
    {
      "lang": "fr",


      "knowledge": "advanced"
    }
  ],
  "job": {
    "site": "https://javabhirkuti.blogspot.com",
    "name": "Bhirkutisoft"
  }
}



<![endif]-->



import java.io.FileNotFoundException;

import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

/**
 * Json Parsor  Example

    @Mukesh Jha
  **/
public class JsonParseTest {
    private static final String filePath = "/Users/macbook/Documents/mypath/bhubaba.json";
     
    public static void main(String[] args) {
        try {
            // read the json file
            FileReader reader = new FileReader(filePath);
            JSONParser jsonParser = new JSONParser();
            JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
            // get a String from the JSON object
            String firstName = (String) jsonObject.get("firstname");
            System.out.println("The first name is: " + firstName);
            // get a number from the JSON object
            long id =  (long) jsonObject.get("id");
            System.out.println("The id is: " + id);
            // get an array from the JSON object
            JSONArray lang= (JSONArray) jsonObject.get("languages");
             
            // take the elements of the json array
            for(int i=0; i
                System.out.println("The " + i + " element of the array: "+lang.get(i));
            }
            Iterator i = lang.iterator();
            // take each value from the json array separately
            while (i.hasNext()) {
                JSONObject innerObj = (JSONObject) i.next();
                System.out.println("language "+ innerObj.get("lang") + 
                        " with level " + innerObj.get("knowledge"));
            }
            // handle a structure into the json object
            JSONObject structure = (JSONObject) jsonObject.get("job");
            System.out.println("Into job structure, name: " + structure.get("name"));
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (ParseException ex) {
            ex.printStackTrace();
        } catch (NullPointerException ex) {
            ex.printStackTrace();
        }
    }
}

The   main   important   thing to note is  that   you  need Json jar file to  execute the  code ,
after getting   Json   file   object   you  need  to  traverse the  each column of data if think  tabular  formate of same json data file . So  wirte    code   to retrieve each   json format of data. 

Program  Output 




The first name is: Bhikubaba

The id is: 1

The 0 element of the array: {"knowledge":"proficient","lang":"en"}
The 1 element of the array: {"knowledge":"advanced","lang":"fr"}
language en with level proficient
language fr with level advanced
Into job structure, name: Bhirkutisoft


Jason Parser Simple JavaExample Jason   Parser Simple    JavaExample Reviewed by Mukesh Jha on 11:30 PM Rating: 5

No comments:

Add your comment

All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.