Reading Json Data From Server and Saving File.

1 min read

Problem : Server is having a JSON file. This file contains some data that is needed to be parsed. After parsing data and processing it, information is to be saved in a file.

Solution : Let break requirement into manageable tasks.

In our Java File we re going to write methods related to

  1. Reading File data from Server
  2. Parsing File data to get Information
  3. Saving Information to File System.
1. Reading File Data from Server
public String readFileFromServer(String url) {
HttpURLConnection conn;
BufferedReader rd;
String line = null;
StringBuilder result = new StringBuilder();
try {
url = new URL(url);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = rd.readLine()) != null) {
result.append(line);
}
rd.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return result.toString();
}

2. Parsing File data to get Information

public void prelimInteraction(String str) {
JSONObject obj = new JSONObject(str);
String pageName = obj.getJSONObject("pageInfo").getString("pageName");
JSONArray arr = obj.getJSONArray("posts");
System.out.println("Page Name: "+pageName);
for (int i = 0; i < arr.length(); i++)
{
String postId = arr.getJSONObject(i).getString("post_id");
String actorId = arr.getJSONObject(i).getString("actor_id");
String picOfPersonWhoPosted = arr.getJSONObject(i).getString("picOfPersonWhoPosted");
String nameOfPersonWhoPosted = arr.getJSONObject(i).getString("nameOfPersonWhoPosted");
String message = arr.getJSONObject(i).getString("message");
String likesCount = arr.getJSONObject(i).getString("likesCount");
String comments = arr.getJSONObject(i).getString("comments");
String timeOfPost = arr.getJSONObject(i).getString("timeOfPost");
System.out.println("Post Id: "+postId);
System.out.println("actor Id: "+actorId);
System.out.println("picOfPersonWhoPosted: "+picOfPersonWhoPosted);
System.out.println("nameOfThePersonWhoPosted: "+nameOfPersonWhoPosted);
System.out.println("message: "+message);
System.out.println("likesCount: "+likesCount);
System.out.println("comments: "+comments);
System.out.println("timeOfPost: "+timeOfPost);
}
}

3. Saving Information to File System.

public void writeFileToMemory(String fileName,String content) {
try {
File file = new File(fileName);
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}

Now I left it to you people to integrate these 3 methods to solve the actual problem.

Solution given in this post assumes Json.jar has been added to Class Path.


Note : Dear Copy-Paste Community :).
Hope you are doing good.
If you are a software engineer study the material before copy pasting.

Else knowledge gets increased when it is shared.

Powerful Tool : Java Extensions : Part 2

In continuation to Part 1 (Java Extensions) Let us have a java class that does a very cool thing does some sort of animation...
user
1 min read

Powerful Tool : Java Extensions : Part 1

Starting a Software Engineering career is very easy :). But the career road becomes bumpy as you travel. Few days back one of my...
user
1 min read