Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import com.google.gson.Gson;
- public class App {
- public static void main(String[] args) {
- System.out.println(loadJson1("data.json", true));
- System.out.println(loadJson2("data.json", true));
- String jsonStr = loadJson2("data.json", true);
- Data data = new Gson().fromJson(jsonStr, Data.class);
- // Show it.
- System.out.println(data);
- }
- public static String loadJson1(String filename, boolean relative) {
- File file = null;
- FileInputStream fis = null;
- String result = "";
- if (relative) {
- filename = "resources/" + filename;
- }
- try {
- file = new File(ClassLoader.getSystemResource(filename).toURI());
- fis = new FileInputStream(file);
- byte[] data = new byte[(int) file.length()];
- fis.read(data);
- fis.close();
- dumpData(data);
- result = new String(data, "UTF-8");
- } catch (Exception e) {
- System.err.println("Error: " + e.getMessage());
- } finally {
- try {
- fis.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return result;
- }
- public static String loadJson2(String filename, boolean relative) {
- BufferedReader br = null;
- StringBuffer sb = new StringBuffer();
- if (relative) {
- filename = "resources/" + filename;
- }
- try {
- br = new BufferedReader(new InputStreamReader(
- ClassLoader.getSystemResourceAsStream(filename)));
- String line = "";
- while ((line = br.readLine()) != null) {
- sb.append(line.trim());
- }
- } catch (Exception e) {
- System.err.println("Error: " + e.getMessage());
- } finally {
- try {
- br.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return sb.toString();
- }
- private static void dumpData(byte[] data) {
- for (int i = 0; i < data.length; i++) {
- System.out.printf("%02X ", data[i]);
- if (i % 8 == 7) {
- System.out.println();
- }
- }
- System.out.println();
- }
- }
- import java.util.List;
- public class Data {
- private String title;
- private Long id;
- private Boolean children;
- private List<Data> groups;
- public String getTitle() {
- return title;
- }
- public Long getId() {
- return id;
- }
- public Boolean getChildren() {
- return children;
- }
- public List<Data> getGroups() {
- return groups;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public void setChildren(Boolean children) {
- this.children = children;
- }
- public void setGroups(List<Data> groups) {
- this.groups = groups;
- }
- public String toString() {
- return String.format("title='%s',id=%d,children=%s,groups=%s",
- title, id, children, groups);
- }
- }
- /* OUTPUT:
- 7B 0D 0A 20 20 20 20 27
- 74 69 74 6C 65 27 3A 20
- 27 43 6F 6D 70 75 74 69
- 6E 67 61 6E 64 49 6E 66
- 6F 72 6D 61 74 69 6F 6E
- 73 79 73 74 65 6D 73 27
- 2C 0D 0A 20 20 20 20 27
- 69 64 27 3A 20 31 2C 0D
- 0A 20 20 20 20 27 63 68
- 69 6C 64 72 65 6E 27 3A
- 20 27 74 72 75 65 27 2C
- 0D 0A 20 20 20 20 27 67
- 72 6F 75 70 73 27 3A 20
- 5B 7B 0D 0A 20 20 20 20
- 20 20 20 20 27 74 69 74
- 6C 65 27 3A 20 27 4C 65
- 76 65 6C 6F 6E 65 43 49
- 53 27 2C 0D 0A 20 20 20
- 20 20 20 20 20 27 69 64
- 27 3A 20 32 2C 0D 0A 20
- 20 20 20 20 20 20 20 27
- 63 68 69 6C 64 72 65 6E
- 27 3A 20 27 74 72 75 65
- 27 2C 0D 0A 20 20 20 20
- 20 20 20 20 27 67 72 6F
- 75 70 73 27 3A 20 5B 7B
- 0D 0A 20 20 20 20 20 20
- 20 20 20 20 20 20 27 74
- 69 74 6C 65 27 3A 20 27
- 49 6E 74 72 6F 54 6F 43
- 6F 6D 70 75 74 69 6E 67
- 61 6E 64 49 6E 74 65 72
- 6E 65 74 27 2C 0D 0A 20
- 20 20 20 20 20 20 20 20
- 20 20 20 27 69 64 27 3A
- 20 33 2C 0D 0A 20 20 20
- 20 20 20 20 20 20 20 20
- 20 27 63 68 69 6C 64 72
- 65 6E 27 3A 20 27 66 61
- 6C 73 65 27 2C 0D 0A 20
- 20 20 20 20 20 20 20 20
- 20 20 20 27 67 72 6F 75
- 70 73 27 3A 20 5B 5D 0D
- 0A 20 20 20 20 20 20 20
- 20 7D 5D 0D 0A 20 20 20
- 20 7D 5D 0D 0A 7D
- {
- 'title': 'ComputingandInformationsystems',
- 'id': 1,
- 'children': 'true',
- 'groups': [{
- 'title': 'LeveloneCIS',
- 'id': 2,
- 'children': 'true',
- 'groups': [{
- 'title': 'IntroToComputingandInternet',
- 'id': 3,
- 'children': 'false',
- 'groups': []
- }]
- }]
- }
- {'title': 'ComputingandInformationsystems','id': 1,'children': 'true','groups': [{'title': 'LeveloneCIS','id': 2,'children': 'true','groups': [{'title': 'IntroToComputingandInternet','id': 3,'children': 'false','groups': []}]}]}
- title='ComputingandInformationsystems',id=1,children=true,groups=[title='LeveloneCIS',id=2,children=true,groups=[title='IntroToComputingandInternet',id=3,children=false,groups=[]]]
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement