-
-
Save anonymous/0f4e60ebc58a443f6800 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
/* | |
* To change this template, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package be.heb.esi.alg3ir.g32666.tools.json; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.io.Reader; | |
import java.io.StringWriter; | |
import java.io.Writer; | |
import net.sf.json.JSONObject; | |
import net.sf.json.JSONSerializer; | |
/** | |
* | |
* @author Neth | |
*/ | |
public class JsonParsing { | |
public static void main(String[] args) throws Exception { | |
InputStream is = | |
JsonParsing.class.getResourceAsStream( "test"); | |
String jsonTxt = JsonParsing.convertStreamToString(is); | |
JSONObject json = new JSONObject(jsonTxt); | |
JSONObject ahri = json.getJSONObject("Ahri"); | |
/*double coolness = json.getDouble( "coolness" ); | |
int altitude = json.getInt( "altitude" ); | |
JSONObject pilot = json.getJSONObject("pilot"); | |
String firstName = pilot.getString("firstName"); | |
String lastName = pilot.getString("lastName");*/ | |
/*System.out.println( "Coolness: " + coolness ); | |
System.out.println( "Altitude: " + altitude ); | |
System.out.println( "Pilot: " + lastName );*/ | |
} | |
public static String convertStreamToString(InputStream is) throws IOException{ | |
if (is != null) { | |
Writer writer = new StringWriter(); | |
char[] buffer = new char[1024]; | |
try { | |
Reader reader = new BufferedReader( | |
new InputStreamReader(is, "UTF-8")); | |
int n; | |
while ((n = reader.read(buffer)) != -1) { | |
writer.write(buffer, 0, n); | |
} | |
} finally { | |
is.close(); | |
} | |
return writer.toString(); | |
} else { | |
return ""; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment