How to parse JSON object in Java

0
28
Spread the love

Hello again, today i’ll explain to you how to parse JSON object in java.

Let’s create a JSON file yourtechpage.json

{
  "name": "John Pit",
  "website": "www.yourtechpage.com",
  "technology": {
    "php": 5   },
  "compose": {
    "total": 1,   
    "soundex": [
      {  
        "info": {
          "date_of_birth": "1-1-2013",
          "name_id": "pxpx345"                   
        }
      }
    ]
  }
}


Right now we will create a Java class to parse our JSON file

package com.test.yourtechpage;

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;

public class JsonParseTest {

	public static void main(String[] args) {

		JSONParser jsonParser = new JSONParser();
		Object object;
		try {
			object = jsonParser.parse(new FileReader("yourtechpage.json"));
			JSONObject jsonObject = (JSONObject) object;

			String name = (String) jsonObject.get("name");
			System.out.println("Name: " + name);
			String website = (String) jsonObject.get("website");
			System.out.println("Website: " + website);

			JSONObject technology = (JSONObject) jsonObject.get("technology");
			System.out.println("Technology: " + technology);
			long php = (Long) technology.get("php");
			System.out.println("\tphp: " + php);

			JSONObject compose = (JSONObject) jsonObject.get("compose");
			System.out.println("compose: " + compose);
			long total = (Long) compose.get("total");
			System.out.println("\ttotal: " + total);
			JSONArray soundex = (JSONArray) compose.get("soundex");
			System.out.println("\tsoundex: " + soundex);

			Object composeObj = jsonObject.get("compose");
			JSONObject jsonObject1 = (JSONObject) composeObj;
			Iterator itr = soundex.iterator();

			while (itr.hasNext()) {

				Object slide = itr.next();
				JSONObject jsonObject2 = (JSONObject) slide;
				JSONObject info = (JSONObject) jsonObject2.get("info");

				String date_of_birth = (String) info.get("date_of_birth");
				String name_id = (String) info.get("name_id");

				System.out.println("\t\tDate of Birth: " + date_of_birth);
				System.out.println("\t\tName Id: " + name_id);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
}

our output is like this : 

Name: John Pit Website: www.yourtechpage.com Technology: {"php":5} php: 5 compose: {"total":1,"soundex":[{"info":
{"date_of_birth":"1-1-2013","name_id":"pxpx345"}}]} total: 1 soundex: [{"info":{"date_of_birth":"1-1-2013","name_id":"pxpx345"}}] Date of Birth: 1-1-2013 Name Id: pxpx345



LEAVE A REPLY

Please enter your comment!
Please enter your name here