Created
June 3, 2017 03:21
-
-
Save seansabour/09eed9586b5a60cf9539cdb0f2d58a87 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.gson.*; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.URL; | |
import java.util.Map; | |
/** | |
* Part 1: Create a Java Application to Read and Manipulate Data | |
* ------- | |
* | |
* Requirement: | |
* | |
* - Utilize Java 6 or later | |
* | |
* Features: | |
* | |
* - Retrieve data from the following URL and parse the JSON | |
* | |
* URL: https://gist.githubusercontent.com/jed204/92f90060d0fabf65792d6d479c45f31c/raw/346c44a23762749ede009623db37f4263e94ef2a/java2.json | |
* | |
* - Add all the recv and sent values for totals of each field and present the results | |
* | |
* | |
* Notes: | |
* | |
* - Console Application or Web App (your call!) | |
* - Use appropriate amounts of error handling | |
* - You can use external libraries to support implementing the features (if desired or needed) | |
* - Create a repo/gist on GitHub for your results | |
* | |
* | |
* Dependencies: | |
* gson (https://github.com/google/gson) | |
*/ | |
public class App { | |
public static String getData(String urlString) throws IOException { | |
BufferedReader reader = null; | |
try { | |
URL url = new URL(urlString); | |
reader = new BufferedReader(new InputStreamReader(url.openStream())); | |
StringBuffer buffer = new StringBuffer(); | |
int read; | |
char[] chars = new char[1024]; | |
while ((read = reader.read(chars)) != -1) | |
buffer.append(chars, 0, read); | |
return buffer.toString(); | |
} finally { | |
if (reader != null) | |
reader.close(); | |
} | |
} | |
public static void main(String args[]) throws IOException { | |
final String URL = "https://gist.githubusercontent.com/jed204/92f90060d0fabf65792d6d479c45f31c/raw/346c44a23762749ede009623db37f4263e94ef2a/java2.json"; | |
parseJSON(URL); | |
} | |
public static void parseJSON(String URL) throws IOException { | |
// Get Data from URL | |
String jsonString = getData(URL); | |
// Parse JSON string and conver to JSON Object | |
JsonParser parser = new JsonParser(); | |
JsonObject object = (JsonObject)parser.parse(jsonString); | |
// Iterate through each entry (fields starting with c) | |
for (Map.Entry<String,JsonElement> entry : object.entrySet()) { | |
// Get value returned as a JSON object | |
JsonObject value = entry.getValue().getAsJsonObject(); | |
// Declare/Initalize sum total for sent and receive | |
double sentTotal = 0; | |
double recvTotal = 0; | |
// Print out current key | |
System.out.println("Field --> " + entry.getKey()); | |
// Iterate through the value's objects' | |
for(Map.Entry<String, JsonElement> inner : value.entrySet()) { | |
JsonObject json = inner.getValue().getAsJsonObject(); | |
// Parse the sent and recv and add them to the counter | |
sentTotal += json.get("sent").getAsDouble(); | |
recvTotal += json.get("recv").getAsDouble(); | |
} | |
// Print result for the current field to console | |
System.out.println("Sent --> " + sentTotal); | |
System.out.println("Received --> " + recvTotal); | |
System.out.println(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment