Advertisement
ridjis

JSONDemo

Nov 3rd, 2015
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.27 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.PrintWriter;
  4.  
  5. import org.json.simple.JSONArray;
  6. import org.json.simple.JSONObject;
  7. import org.json.simple.parser.JSONParser;
  8. import org.json.simple.parser.ParseException;
  9.  
  10. public class JSONDemo {
  11.     public final static String FILE_NAME = "JSONDemo2.json";
  12.     public static void main(String[] args) {
  13.         jsonToFile(FILE_NAME);
  14.        
  15.         String jsonString = jsonFromFile(FILE_NAME);
  16.        
  17.         jsonParse(jsonString);
  18.     }
  19.    
  20.     @SuppressWarnings("unchecked")
  21.     public static void jsonToFile(String fileName) {
  22.         String name = Svetovid.in.readLine("Enter a name: ");
  23.         JSONObject root = new JSONObject();
  24.         root.put("name", name);
  25.        
  26.         JSONArray courses = new JSONArray();
  27.  
  28.         while(true) {
  29.             String course = Svetovid.in.readLine("Enter a course name (Enter to exit): ");
  30.             if (course.length() == 0)
  31.                 break;
  32.            
  33.             int grade = Svetovid.in.readInt("Enter a course grade: ");
  34.            
  35.             JSONObject courseObject = new JSONObject();
  36.             courseObject.put("grade", grade);
  37.             courseObject.put("course", course);
  38.             courses.add(courseObject);
  39.         }
  40.         root.put("courses", courses);
  41.        
  42.         File file = new File(fileName);
  43.        
  44.         try (PrintWriter writer = new PrintWriter(file)) {
  45.             writer.print(root.toJSONString());
  46.         } catch (FileNotFoundException e) {
  47.             System.err.println(e.toString());
  48.         }
  49.        
  50.         System.out.println("File created successfully.\n");
  51.     }
  52.    
  53.     public static String jsonFromFile(String fileName) {
  54.         StringBuilder jsonIn = new StringBuilder();
  55.        
  56.         while (Svetovid.in(fileName).hasMore()) {
  57.             jsonIn.append(Svetovid.in(fileName).readLine());
  58.         }
  59.  
  60.         return jsonIn.toString();
  61.     }
  62.    
  63.     public static void jsonParse(String jsonString) {
  64.         JSONParser parser = new JSONParser();
  65.        
  66.         try {
  67.             JSONObject root = (JSONObject) parser.parse(jsonString);
  68.             System.out.printf("Course info for %s", root.get("name") + "\n");
  69.            
  70.             JSONArray courses = (JSONArray) root.get("courses");
  71.            
  72.             for (int i = 0; i < courses.size(); i++) {
  73.                 JSONObject course = (JSONObject) courses.get(i);
  74.                
  75.                 System.out.printf("course: %s", course.get("course") + ", ");
  76.                 System.out.printf("grade: %d", course.get("grade"));
  77.                 System.out.println();
  78.             }
  79.         } catch (ParseException e) { System.err.println(e); }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement