Advertisement
andrzejiwaniuk

JSON reading values from file

Jan 18th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.23 KB | None | 0 0
  1. package co.uk.andrzejiwaniuk;
  2.  
  3.  
  4. import org.json.simple.*;
  5. import org.json.simple.parser.JSONParser;
  6. import java.io.FileReader;
  7. import java.util.Iterator;
  8.  
  9. /**
  10.  * JSON
  11.  *
  12.  */
  13. public class App
  14. {
  15.     public static void main( String[] args )
  16.     {
  17.        
  18.            
  19.        
  20.          /*System.out.println("=======decode=======");
  21.          
  22.           String s="[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
  23.           Object obj=JSONValue.parse(s);
  24.           JSONArray array=(JSONArray)obj;
  25.           System.out.println("======the 2nd element of array======");
  26.           System.out.println(array.get(1));
  27.           System.out.println();
  28.                        
  29.           JSONObject obj2=(JSONObject)array.get(1);
  30.           System.out.println("======field \"1\"==========");
  31.           System.out.println(obj2.get("1"));    
  32.  
  33.                        
  34.           s="{}";
  35.           obj=JSONValue.parse(s);
  36.           System.out.println(obj);
  37.                        
  38.           s="[5,]";
  39.           obj=JSONValue.parse(s);
  40.           System.out.println(obj);
  41.                        
  42.           s="[5,,2]";
  43.           obj=JSONValue.parse(s);
  44.           System.out.println(obj);*/
  45.        
  46.           //System.out.println( "Witaj Andrzej2" );
  47.        
  48.         JSONParser parser = new JSONParser();
  49.          
  50.         try {
  51.  
  52.             Object obj = parser.parse(new FileReader(
  53.                     "C:/JSON/file1.txt"));
  54.  
  55.             JSONObject jsonObject = (JSONObject) obj;
  56.  
  57.             String name = (String) jsonObject.get("Name");
  58.             String author = (String) jsonObject.get("Author");
  59.             JSONArray companyList = (JSONArray) jsonObject.get("Company List");
  60.  
  61.             System.out.println("Name: " + name);
  62.             System.out.println("Author: " + author);
  63.             System.out.println("\nCompany List:");
  64.             Iterator<String> iterator = companyList.iterator();
  65.             while (iterator.hasNext()) {
  66.                 System.out.println(iterator.next());
  67.             }
  68.  
  69.         } catch (Exception e) {
  70.             e.printStackTrace();
  71.         }
  72.    
  73.     }
  74. }
  75.  
  76. /*
  77. *
  78. *   JSON file
  79. *
  80. */
  81. {
  82.     "Name": "tedo-tech.co.uk",
  83.     "Author": "Andrzej Iwaniuk",
  84.     "Company List": [
  85.         "Compnay: eBay",
  86.         "Compnay: Paypal",
  87.         "Compnay: Google"
  88.     ]
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement