Posted By

prgrmmraben on 09/06/14


Tagged

parse json beans JSONObject JSONWriter JSONTokener pojos


Versions (?)

Some uses of JSONObject, JSONWriter and JSONTokener classes of org.json


 / Published in: Java
 

URL: http://fivesnippets.blogspot.com/2014/09/jsonobject-jsonwriter-and-jsontokener.html

This snippet of code, illustrates how to put, remove or accumulate values in a JSONObject object, use of JSONWriter for putting key/value pairs in "quick and convenient way" as stated by official documentation, and more practical uses for org.json beginners

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import java.io.StringWriter;
  6. import java.net.URI;
  7. import java.net.URISyntaxException;
  8. import org.json.*;
  9.  
  10.  
  11. /**
  12.  * @author prgrmmr.aben [at] gmail (dot) com
  13.  * http://fivesnippets.blogspot.com/2014/09/jsonobject-jsonwriter-and-jsontokener.html
  14.  * please give back a small donation if you find
  15.  * this little educational snippet of code useful
  16.  */
  17. public class test {
  18.  
  19. int intValue=0;
  20. public int getIntValue() {
  21. return intValue;
  22. }
  23.  
  24. public void setIntValue(int intValue) {
  25. this.intValue = intValue;
  26. }
  27.  
  28. public String getStringValue() {
  29. return stringValue;
  30. }
  31.  
  32. public void setStringValue(String stringValue) {
  33. this.stringValue = stringValue;
  34. }
  35.  
  36. public Double getDoubleValue() {
  37. return doubleValue;
  38. }
  39.  
  40. public void setDoubleValue(Double doubleValue) {
  41. this.doubleValue = doubleValue;
  42. }
  43.  
  44. String stringValue="azerty0123";
  45. Double doubleValue = new Double(100.01);
  46. static URI uri =null;
  47. static { //is there a way to create an object which constructor can throw
  48. //an exception without using static block?, please answer me in comments
  49. try {
  50. uri = new URI("https://raw.githubusercontent.com/originals974/symfony/master/composer.json");
  51. } catch (URISyntaxException e) {
  52. // TODO Auto-generated catch block
  53. e.printStackTrace();
  54. }
  55. }
  56.  
  57. public static void main(String[] args) throws IOException, URISyntaxException {
  58. // TODO Auto-generated method stub
  59. /*
  60.   * creation of an URI object consisted of parsing a Json file in this address
  61.   * creation of two Json objects one by default construct,
  62.   * the other with a bean as an argument, a bean is an object
  63.   * with public getters and setters
  64.   */
  65. JSONObject jsonObj = new JSONObject();
  66. JSONObject jsonObj2 = new JSONObject(new test());
  67. //put key/value pairs and experimenting the toString() method
  68. //on Json objects
  69. jsonObj.put("value1", "qwerty");
  70. jsonObj.accumulate("value1", "azerty");
  71. System.out.println(jsonObj.toString());
  72. System.out.println(jsonObj2.toString());
  73. //we can put objects of type JSONObject, like we did with beans,
  74. //we do with JSONObject objects
  75. jsonObj.put("innerJson", jsonObj2);
  76. System.out.println(jsonObj.toString());
  77. jsonObj.remove("innerJson");
  78. System.out.println(jsonObj.toString());
  79. //JSONObject.doubleToString() returns null
  80. //when NaN (not finite number) value is passed as its argument
  81. System.out.println(JSONObject.doubleToString(new Double(0.0/0.0)));
  82. System.out.println(JSONObject.doubleToString(new Double(0.0/2)));
  83. //getting Names (keys) of jsonObj2
  84. for (String string : JSONObject.getNames(jsonObj2)) {
  85. System.out.print(string+", ");
  86. }
  87. System.out.println();
  88. //you need to activate assertions, pass -ea as an argument to the JVM
  89. assert !jsonObj.isNull("[email protected]") : "key not found";
  90. /*
  91.   * making a JSONWriter object to put key/value pairs in "quick and convenient way"
  92.   * as stated in the documentation of JSONWriter
  93.   */
  94. StringWriter writer = new StringWriter();
  95. JSONWriter JsonWriter = new JSONWriter(writer);
  96. JsonWriter.object().key("key1").value(new String("value1")).key("key2").value("value2").key("key3").value("value3").endObject();
  97. System.out.println(writer.toString());
  98. /*
  99.   * experiencing parsing methods in JSONTokener objects
  100.   * we pass an inputStream after opening a connection to an URL
  101.   */
  102. InputStream jsonStreamExample = uri.toURL().openStream();
  103. BufferedReader reader = new BufferedReader(new InputStreamReader(jsonStreamExample));
  104. StringBuilder builder = new StringBuilder();
  105. String aux = "";
  106. while ((aux = reader.readLine()) != null) {
  107. builder.append(aux);
  108. }
  109. String text = builder.toString();
  110. System.out.println(text);
  111.  
  112. jsonStreamExample = uri.toURL().openStream();
  113. JSONTokener tokener = new JSONTokener(jsonStreamExample);
  114. System.out.println("1-->"+tokener.next()+"<-- is a bracket");
  115. System.out.println("2-->"+tokener.next()+"<-- is a back-space");
  116. System.out.println("3-->"+tokener.next()+"<-- is a space");
  117. System.out.println("4-->"+tokener.next()+"<-- is a space");
  118. System.out.println("5-->"+tokener.next()+"<-- is a space");
  119. System.out.println("6-->"+tokener.next()+"<-- is a space");
  120. System.out.println("7-->"+tokener.next()+"<-- is a quote");
  121. System.out.println("8-->"+tokener.next()+"<-- is first character of \"name\" ");
  122. System.out.println("9-->"+tokener.nextValue()+"<-- is the value that comes next");
  123. System.out.print(tokener.next()+","+tokener.next());
  124. System.out.println("\nthis is the value that comes next to the space after ':' --> "+tokener.nextValue());
  125.  
  126. }
  127. }

Report this snippet  

You need to login to post a comment.