Skip to main content

Java API call Example using GSON, org.json.json and Jackson [ Simple Get Call] and parsing result as JSON


import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.*;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class APICALL {

public static void main(String[] args) throws IOException {
// String url="https://mocki.io/v1/19a50724-c2e5-46a1-b457-543462cdfde2";
String url="https://jsonplaceholder.typicode.com/users";
String line;
StringBuilder resp=new StringBuilder();
System.out.println(url);
HttpURLConnection con= (HttpURLConnection) new URL(url).openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Accept","application/json");
System.out.println(con.getResponseMessage());
System.out.println(con.getContentType());
InputStream inputStream=con.getInputStream();
BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));

while ((line=reader.readLine())!=null)
{
resp.append(line);
}

System.out.println(resp.toString());
System.out.println("Outputsss");
FileOutputStream fileOutputStream=new FileOutputStream("resp.txt");
fileOutputStream.write(resp.toString().getBytes());


System.out.println("==================Using org.json.json ================== ");
JSONArray jsonArray=new JSONArray(resp.toString());



for (int i=0;i<jsonArray.length();i++)
{
JSONObject jsonObject=jsonArray.getJSONObject(i);
System.out.println(jsonObject.get("name")+"--"+jsonObject.get("email"));
}
System.out.println("Printed total "+jsonArray.length());


System.out.println("==================Using Jackson methods==================");

JsonNode jsonNode=new ObjectMapper().readTree(resp.toString());

jsonNode.forEach(x-> System.out.println(x.get("name")+"--"+x.get("email")));

// using GSOn
System.out.println("==================Using Gson==================");

JsonElement jsonElement=JsonParser.parseString(resp.toString());
JsonArray jar=jsonElement.getAsJsonArray();
for (int i=0;i<jar.size();i++)
{
JsonObject jsonObject=jar.get(i).getAsJsonObject();
System.out.println(jsonObject.get("name")+"--"+jsonObject.get("email"));
}
System.out.println("Printed total "+jar.size());


}
}

Comments

Popular posts from this blog

How to Put Google Adsense Below Post Title in Blogger?

Adsense is used by majority of expert bloggers for their website monetization because it is a cookie based contextual advertising system that shows targeted ads relevant to the content and reader. As bloggers are paid on per click basis, they try various ad placements on the blog to  increase the revenue  and get maximum clicks on the ad units. Well, on some blogs, you might have seen Adsense ad units placed below the post title. Do you know why? It is because the area just below the post title gets the most exposure and is the best place to put AdSense ad units to increase  Click Through Rate (CTR). Even though ads below post title work like a charm but this doesn’t mean that it will work for you as well. If you want to find out the best AdSense ads placement for your blog, try experimenting by placing ads at various locations such as header, sidebar, footer, etc. You can try other  blog monetization methods  as well to effectively monetize y...

30 Terrific Twitter Facts And Figures

Twitter has always suffered an image problem and is not usually taken very seriously by the general public. Its name doesn’t help with some people even saying that ‘Twitter is for twits’. Despite this glamor and brand problem this has not held back its growth after its humble origins and launch in 2006. Since then Twitter has gained popularity worldwide and is estimated to have 225 million users, generating 65 million tweets a day and handling over 800,000 search queries per day. It is sometimes described as the “SMS of the Internet” and its 140 character limit keeps the messages short and simple. Its attraction as a social web media platform is maybe in its simplicity and real time messaging that enables breaking news and information to hit the web instantly without filt30 Terrific Twitter Facts and Figures:- Twitter was created in March 2006 by Jack Dorsey and launched in July of that year. Twitter’s origins lie in a “day long brainstorming session” that was held by board...

python program to Print Starting Series OF Indian Mobile Number for a State or operator or both

import requests import urllib.request import time from bs4 import BeautifulSoup as bs import re url = ' https://en.wikipedia.org/wiki/Mobile_telephone_numbering_in_India' state_to_extract = "UE" #if set to None all state is considered telecom_to_extracted = None #if set to none all operator from particular city is extracted response = requests . get(url) print (response) soup = bs(response . text, "html.parser" ) one_a_tag = soup . findAll( 'tr' )[ 35 :] lst = [] for k in one_a_tag: s = k . findAll( 'td' ) limit = len (s) i = 0 while True : if i == limit: break no = s[i] . text i += 1 if i == limit: break operator = s[i] . text i += 1 if i == limit: break state = s[i] . text i += 1 if i == limit: break res = f "{no} {operator} {state}" if state_to_extract is None : if telecom_to_extracted is None : lst . append(no) elif telecom_to_e...

[solution] Motorola moto G5S plus | xt1804|Sanders Magisk error 1 : cannot mount /vendor

Error: mainly all error 1 errors. can not mount /vendors... Cause : Since your current TWRP is not treble supported ,You need a Treble supported Recovery. Solution : Simpally download this file and then flash it .  this recovery is treble supported 1. VIA TWRP boot to twrp recovery  goto install .  touch on install image and then select this downloaded file (.img)  select recovery  then flash it 2. Using fastboot fastboot flash recovery <(downloaded.img)> NOW YOU CAN FLASH MAGISK zip file via recovery LINK : TWRP_SANDERS_r22_BY_GENETIC ENGINEER

0 Add A Stylish Subscription Box With Social Media Buttons Below Every Posts

Adding a Stylish Subscription Box Below Bloggers Post is a great techniques to capture your blog post readers never to leave your bloggers blog. Giving them the opportunity to subscribe to your bloggers blog will definitely increase the count of your blog readers. By adding a Subscription box to your blog post or below your blog post will also create an opportunity for your blog readers never to leave you website or blog.I included stylish social media follow button with the subscription widget. ☻  HOW TO ADD SUBSCRIPTION BOX WITH SOCIAL MEDIA BUTTONS BELOW EVERY POSTS 1) Go to  Blogger Dashboard   →   Template   →   Edit HTML . 2) Now Find the code shown below using [ ctrl+F ] ( Use In HTML Box ) 3) Now Paste the Code Shown Below just  After  it. Get free daily email updates! Subscribe via Email Follow us! [Get this widget] 4) Customize your setting.Find any word click  F3  or  Ctrl+F . ♥ Repl...

customize-the-windows-7-logon-screen

Customize the Windows 7 Logon Screen Do you like to customize the Windows interface, and tired of the standard log on screen in Windows 7? Today we take a look at Windows 7 Logon Background Changer which is a free and Open Source app that lets you easily customize the logon screen. Windows 7 Logon Background Changer This cool app is free, Open Source, and lets you change the wallpaper on the Windows 7 Logon screen. It doesn’t require installation, and won’t change any of the system files. The download is a small zip file that contains an executable you can run from a flash drive, and also an installer in the Setup folder if you choose to install it. After launching Background changer, you can browse through the standard Windows backgrounds to get an idea of how it will look.If you don’t want to use the standard Windows backgrounds, you can add your own folder of images. After you’ve selected the background you want, click on the Apply button in the upper right hand corner....

Windows 10 English x64.iso 4.03 GB download direct (google drive) creator update 1703 ..latest windows 10

updated ON 1/01/2018 Windows 10 English x64.iso 4.03 GB download direct (google drive) creator update 1703 ..latest windows 10 x64  .. NEW LINK latest version fall creator 1709 update : click here Updated : 5 july 3 PM (IST) Link to the file  click here link: https://www.multcloud.com/share/7977c732-8fa3-4cb7-ae58-a6d99d66bb5b   Just goto to the following link and enter your email address and get the password delivered to you.. Password to the link is :  HERE (FOR PASSWORD OF ABOVE LINK)  OR USE THE FORM BELOW.... Loading... size : 4.03 GB  Language : ENGLISH INTERNATIONAL SYSTEM :WINDOWS 10 x64 BASED.. TYPE : DIRECT DOWNLOAD LINK ......ISO FILE> TORRENT  windows 10 creator update 1703/ 1709 iso FOR OTHER mirror 2 link goto here and enter your email address you will get a email with link to the file....

download Code blocks 13.12 mingw.setup .exe 97 mb

NOTE: A newer version is been updated on the site ... visit here  http://vastgk.blogspot.com/2017/07/download-code-blocks-1601-mingwsetup.html File Date Download from codeblocks-13.12-setup.exe 27 Dec 2013 BerliOS  or  Sourceforge.net codeblocks-13.12mingw-setup.exe 27 Dec 2013 BerliOS  or  Sourceforge.net codeblocks-13.12mingw-setup-TDM-GCC-481.exe 27 Dec 2013 BerliOS  or  Sourceforge.net NOTE : The codeblocks-13.12mingw-setup.exe file  includes  the GCC compiler and GDB debugger from  TDM-GCC  (version 4.7.1, 32 bit). The codeblocks-13.12mingw-setup-TDM-GCC-481.exe file includes the TDM-GCC compiler, version 4.8.1, 32 bit. While v4.7.1 is rock-solid (we use it to compile C::B), v4.8.1 is provided for convenience, there are some known bugs with this version related to the compilation of Code::Blocks itself. IF UNSURE, USE "codeblocks-13.12mingw-setup.exe"!  \ Linux 32-bit: Dis...