Reading a Json File using Json Simple Jar

Reading a Json File using Json Simple Jar:
JSON is Java Script Object Notation mainly used for information exchange and it is best alternative for XML. We are going to read a json file using simple json jar 1.1 in java.

If you do not have this jar please download from the below mentioned link and put into your classpath.

Required Jar:

Json-simple-1.1.jar

 

 

Java Program:

[java]package in.javadomain;

import java.io.FileReader;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class ReadJsonFile {

public static void main(String[] args) {

JSONParser parser = new JSONParser();

try {
JSONArray feedJSONArray = (JSONArray) parser.parse(newFileReader("D:\\test.json"));
for (Object outerObj : feedJSONArray) {
JSONObject couponz = (JSONObject) outerObj;

JSONArray couponArray = (JSONArray) couponz.get("item");
if (couponArray != null) {
for (Object ob : couponArray) {
JSONObject indivCoupon = (JSONObject) ob;
System.out.println("1===>" +indivCoupon.get("LastVerified"));

System.out.println("2===>" +indivCoupon.get("DealType"));

System.out.println("3===>" +indivCoupon.get("DealCode"));

System.out.println("4===>" +indivCoupon.get("StoreName"));

System.out.println("5===>" +indivCoupon.get("DealLink"));

System.out.println("6===>" +indivCoupon.get("Exclusive"));

System.out.println("7===>" +indivCoupon.get("CampaignLogo"));

System.out.println("8===>" +indivCoupon.get("Expired"));

System.out.println("9===>" +indivCoupon.get("Thumbnail"));

System.out.println("10===>" +indivCoupon.get("DealDescription"));

System.out.println("11===>" +indivCoupon.get("DealTitle"));

System.out.println("12===>" +indivCoupon.get("CouponCategory"));

System.out.println("13===>" +indivCoupon.get("StoreId"));

System.out.println("14===>" + indivCoupon.get("Id"));

System.out.println("15===>" +indivCoupon.get("CouponCategoryId"));

System.out.println("————————Javadomain.in———————–");
}
}

}
} catch (Exception e) {
e.printStackTrace();
}

}

}
[/java]

 

Output:

[plain]
1===>2015-12-04 13:55:15
2===>Offer
3===>
4===>Rediff.com India Ltd – All Products
5===>http://www.s2d6.com/x/?x=c&z=s&v=6388513&k=[NETWORKID]&t=http%3A%2F%2Fshopping.rediff.com%2Fstores%2F6844%3Fsc_cid%3DDGMShoppingAffiliate
6===>0
7===>http://dzh2xn6ml8v5t.cloudfront.net/logo/store/54.jpg
8===>2015-12-31
9===>http://dzh2xn6ml8v5t.cloudfront.net/thumb/store/54.jpg
10===>Buy 1 & Get 1 Free On Selected Products At Rediff Shopping. No Coupon Code Required. Hurry!!! Offer Valid For Limited Period.
11===>Buy 1 & Get 1 Free On Selected Products
12===>All
13===>54
14===>799a8c29085fe3f533a66d31eb68a9be
15===>0
————————Javadomain.in———————–
1===>2015-12-04 15:02:33
2===>Offer
3===>
4===>Rediff.com India Ltd – All Products
5===>http://www.s2d6.com/x/?x=c&z=s&v=6388513&k=[NETWORKID]&t=http%3A%2F%2Fshopping.rediff.com%2Fproduct%2Fpuma-shoes%3Fsc_cid%3DDGMShoppingAffiliate
6===>0
7===>http://dzh2xn6ml8v5t.cloudfront.net/logo/store/54.jpg
8===>2015-12-31
9===>http://dzh2xn6ml8v5t.cloudfront.net/thumb/store/54.jpg
10===>Buy Puma Shoes And Get Upto 50% Off At Rediff Shopping. No Coupon Code Required. Hurry!!! Offer Valid For Limited Period.
11===>Upto 50% Off On Puma Shoes
12===>Shoes And Bags
13===>54
14===>a0d39a2e9765f9deb4e11be16ddfce4a
15===>5
————————Javadomain.in———————–
[/plain]

 

 

Test.json file:

[plain]
[{"lastBuildDate":"2015-12-23 10:48:03"},{"item":[{"Id":"799a8c29085fe3f533a66d31eb68a9be","DealTitle":"Buy 1 & Get 1 Free On Selected Products","DealType":"Offer","DealCode":"","DealDescription":"Buy 1 & Get 1 Free On Selected Products AtRediff Shopping. No Coupon Code Required. Hurry!!! Offer Valid For Limited Period.","DealLink":"http:\/\/www.s2d6.com\/x\/?x=c&z=s&v=6388513&k=[NETWORKID]&t=http%3A%2F%2Fshopping.rediff.com%2Fstores%2F6844%3Fsc_cid%3DDGMShoppingAffiliate","CouponCategory":"All","CouponCategoryId":"0","StoreName":"Rediff.com India Ltd – All Products","StoreId":"54","CampaignLogo":"http:\/\/dzh2xn6ml8v5t.cloudfront.net\/logo\/store\/54.jpg","LastVerified":"2015-12-04 13:55:15","Expired":"2015-12-31","Exclusive":"0","Thumbnail":"http:\/\/dzh2xn6ml8v5t.cloudfront.net\/thumb\/store\/54.jpg"},{"Id":"a0d39a2e9765f9deb4e11be16ddfce4a","DealTitle":"Upto 50% Off On Puma Shoes","DealType":"Offer","DealCode":"","DealDescription":"Buy Puma Shoes And Get Upto 50% Off At RediffShopping. No Coupon Code Required. Hurry!!! Offer Valid For Limited Period.","DealLink":"http:\/\/www.s2d6.com\/x\/?x=c&z=s&v=6388513&k=[NETWORKID]&t=http%3A%2F%2Fshopping.rediff.com%2Fproduct%2Fpuma-shoes%3Fsc_cid%3DDGMShoppingAffiliate","CouponCategory":"Shoes And Bags","CouponCategoryId":"5","StoreName":"Rediff.com India Ltd – All Products","StoreId":"54","CampaignLogo":"http:\/\/dzh2xn6ml8v5t.cloudfront.net\/logo\/store\/54.jpg","LastVerified":"2015-12-04 15:02:33","Expired":"2015-12-31","Exclusive":"0","Thumbnail":"http:\/\/dzh2xn6ml8v5t.cloudfront.net\/thumb\/store\/54.jpg"}]}]
[/plain]

Leave a Reply