Advertisement
MrPolywhirl

Read JSON

May 14th, 2014
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.75 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. import com.google.gson.Gson;
  8.  
  9. public class App {
  10.     public static void main(String[] args) {
  11.         System.out.println(loadJson1("data.json", true));
  12.         System.out.println(loadJson2("data.json", true));
  13.  
  14.         String jsonStr = loadJson2("data.json", true);
  15.         Data data = new Gson().fromJson(jsonStr, Data.class);
  16.  
  17.         // Show it.
  18.         System.out.println(data);
  19.     }
  20.  
  21.     public static String loadJson1(String filename, boolean relative) {
  22.         File file = null;
  23.         FileInputStream fis = null;
  24.         String result = "";
  25.  
  26.         if (relative) {
  27.             filename = "resources/" + filename;
  28.         }
  29.  
  30.         try {
  31.             file = new File(ClassLoader.getSystemResource(filename).toURI());
  32.             fis = new FileInputStream(file);
  33.             byte[] data = new byte[(int) file.length()];
  34.             fis.read(data);
  35.             fis.close();
  36.            
  37.             dumpData(data);
  38.            
  39.             result = new String(data, "UTF-8");
  40.         } catch (Exception e) {
  41.             System.err.println("Error: " + e.getMessage());
  42.         } finally {
  43.             try {
  44.                 fis.close();
  45.             } catch (IOException e) {
  46.                 e.printStackTrace();
  47.             }
  48.         }
  49.  
  50.         return result;
  51.     }
  52.  
  53.     public static String loadJson2(String filename, boolean relative) {
  54.         BufferedReader br = null;
  55.         StringBuffer sb = new StringBuffer();
  56.  
  57.         if (relative) {
  58.             filename = "resources/" + filename;
  59.         }
  60.  
  61.         try {
  62.             br = new BufferedReader(new InputStreamReader(
  63.                     ClassLoader.getSystemResourceAsStream(filename)));
  64.  
  65.             String line = "";
  66.             while ((line = br.readLine()) != null) {
  67.                 sb.append(line.trim());
  68.             }
  69.         } catch (Exception e) {
  70.             System.err.println("Error: " + e.getMessage());
  71.         } finally {
  72.             try {
  73.                 br.close();
  74.             } catch (IOException e) {
  75.                 e.printStackTrace();
  76.             }
  77.         }
  78.  
  79.         return sb.toString();
  80.     }
  81.    
  82.     private static void dumpData(byte[] data) {
  83.         for (int i = 0; i < data.length; i++) {
  84.             System.out.printf("%02X ", data[i]);
  85.            
  86.             if (i % 8 == 7) {
  87.                 System.out.println();
  88.             }
  89.         }
  90.         System.out.println();
  91.     }
  92. }
  93.  
  94. import java.util.List;
  95.  
  96. public class Data {
  97.     private String title;
  98.     private Long id;
  99.     private Boolean children;
  100.     private List<Data> groups;
  101.  
  102.     public String getTitle() {
  103.         return title;
  104.     }
  105.  
  106.     public Long getId() {
  107.         return id;
  108.     }
  109.  
  110.     public Boolean getChildren() {
  111.         return children;
  112.     }
  113.  
  114.     public List<Data> getGroups() {
  115.         return groups;
  116.     }
  117.  
  118.     public void setTitle(String title) {
  119.         this.title = title;
  120.     }
  121.  
  122.     public void setId(Long id) {
  123.         this.id = id;
  124.     }
  125.  
  126.     public void setChildren(Boolean children) {
  127.         this.children = children;
  128.     }
  129.  
  130.     public void setGroups(List<Data> groups) {
  131.         this.groups = groups;
  132.     }
  133.  
  134.     public String toString() {
  135.         return String.format("title='%s',id=%d,children=%s,groups=%s",
  136.             title, id, children, groups);
  137.     }
  138. }
  139.  
  140. /* OUTPUT:
  141.  
  142. 7B 0D 0A 20 20 20 20 27
  143. 74 69 74 6C 65 27 3A 20
  144. 27 43 6F 6D 70 75 74 69
  145. 6E 67 61 6E 64 49 6E 66
  146. 6F 72 6D 61 74 69 6F 6E
  147. 73 79 73 74 65 6D 73 27
  148. 2C 0D 0A 20 20 20 20 27
  149. 69 64 27 3A 20 31 2C 0D
  150. 0A 20 20 20 20 27 63 68
  151. 69 6C 64 72 65 6E 27 3A
  152. 20 27 74 72 75 65 27 2C
  153. 0D 0A 20 20 20 20 27 67
  154. 72 6F 75 70 73 27 3A 20
  155. 5B 7B 0D 0A 20 20 20 20
  156. 20 20 20 20 27 74 69 74
  157. 6C 65 27 3A 20 27 4C 65
  158. 76 65 6C 6F 6E 65 43 49
  159. 53 27 2C 0D 0A 20 20 20
  160. 20 20 20 20 20 27 69 64
  161. 27 3A 20 32 2C 0D 0A 20
  162. 20 20 20 20 20 20 20 27
  163. 63 68 69 6C 64 72 65 6E
  164. 27 3A 20 27 74 72 75 65
  165. 27 2C 0D 0A 20 20 20 20
  166. 20 20 20 20 27 67 72 6F
  167. 75 70 73 27 3A 20 5B 7B
  168. 0D 0A 20 20 20 20 20 20
  169. 20 20 20 20 20 20 27 74
  170. 69 74 6C 65 27 3A 20 27
  171. 49 6E 74 72 6F 54 6F 43
  172. 6F 6D 70 75 74 69 6E 67
  173. 61 6E 64 49 6E 74 65 72
  174. 6E 65 74 27 2C 0D 0A 20
  175. 20 20 20 20 20 20 20 20
  176. 20 20 20 27 69 64 27 3A
  177. 20 33 2C 0D 0A 20 20 20
  178. 20 20 20 20 20 20 20 20
  179. 20 27 63 68 69 6C 64 72
  180. 65 6E 27 3A 20 27 66 61
  181. 6C 73 65 27 2C 0D 0A 20
  182. 20 20 20 20 20 20 20 20
  183. 20 20 20 27 67 72 6F 75
  184. 70 73 27 3A 20 5B 5D 0D
  185. 0A 20 20 20 20 20 20 20
  186. 20 7D 5D 0D 0A 20 20 20
  187. 20 7D 5D 0D 0A 7D
  188. {
  189.     'title': 'ComputingandInformationsystems',
  190.     'id': 1,
  191.     'children': 'true',
  192.     'groups': [{
  193.         'title': 'LeveloneCIS',
  194.         'id': 2,
  195.         'children': 'true',
  196.         'groups': [{
  197.             'title': 'IntroToComputingandInternet',
  198.             'id': 3,
  199.             'children': 'false',
  200.             'groups': []
  201.         }]
  202.     }]
  203. }
  204. {'title': 'ComputingandInformationsystems','id': 1,'children': 'true','groups': [{'title': 'LeveloneCIS','id': 2,'children': 'true','groups': [{'title': 'IntroToComputingandInternet','id': 3,'children': 'false','groups': []}]}]}
  205. title='ComputingandInformationsystems',id=1,children=true,groups=[title='LeveloneCIS',id=2,children=true,groups=[title='IntroToComputingandInternet',id=3,children=false,groups=[]]]
  206.  
  207. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement