Return to Snippet

Revision: 67306
at September 6, 2014 07:51 by prgrmmraben


Updated Code
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.net.URI;
import java.net.URISyntaxException;
import org.json.*;


/**
 * @author prgrmmr.aben [at] gmail (dot) com
 * http://fivesnippets.blogspot.com/2014/09/jsonobject-jsonwriter-and-jsontokener.html
 * please give back a small donation if you find
 * this little educational snippet of code useful 
 */
public class test {
 
 int intValue=0;
 public int getIntValue() {
  return intValue;
 }

 public void setIntValue(int intValue) {
  this.intValue = intValue;
 }

 public String getStringValue() {
  return stringValue;
 }

 public void setStringValue(String stringValue) {
  this.stringValue = stringValue;
 }

 public Double getDoubleValue() {
  return doubleValue;
 }

 public void setDoubleValue(Double doubleValue) {
  this.doubleValue = doubleValue;
 }
 
 String stringValue="azerty0123";
 Double doubleValue = new Double(100.01);
 static URI uri =null;
 static { //is there a way to create an object which constructor can throw
    //an exception without using static block?, please answer me in comments
  try {
  uri = new URI("https://raw.githubusercontent.com/originals974/symfony/master/composer.json");
 } catch (URISyntaxException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 }

 public static void main(String[] args) throws IOException, URISyntaxException  {
  // TODO Auto-generated method stub
  /*
   *  creation of an URI object consisted of parsing a Json file in this address
   *  creation of two Json objects one by default construct,
   *  the other with a bean as an argument, a bean is an object 
   *  with public getters and setters
  */
  JSONObject jsonObj = new JSONObject();
  JSONObject jsonObj2 = new JSONObject(new test());
  //put key/value pairs and experimenting the toString() method
  //on Json objects
  jsonObj.put("value1", "qwerty");
  jsonObj.accumulate("value1", "azerty");
  System.out.println(jsonObj.toString());
  System.out.println(jsonObj2.toString());
  //we can put objects of type JSONObject, like we did with beans,
  //we do with JSONObject objects
  jsonObj.put("innerJson", jsonObj2);
  System.out.println(jsonObj.toString());
  jsonObj.remove("innerJson");
  System.out.println(jsonObj.toString());
  //JSONObject.doubleToString() returns null
  //when NaN (not finite number) value is passed as its argument
  System.out.println(JSONObject.doubleToString(new Double(0.0/0.0)));
  System.out.println(JSONObject.doubleToString(new Double(0.0/2)));
  //getting Names (keys) of jsonObj2
  for (String string : JSONObject.getNames(jsonObj2)) {
   System.out.print(string+", ");
  }
  System.out.println();
  //you need to activate assertions, pass -ea as an argument to the JVM
  assert !jsonObj.isNull("[email protected]") : "key not found";
  /*
   *  making a JSONWriter object to put key/value pairs in "quick and convenient way"
   *  as stated in the documentation of JSONWriter
  */
  StringWriter writer = new StringWriter();
  JSONWriter JsonWriter = new JSONWriter(writer);
  JsonWriter.object().key("key1").value(new String("value1")).key("key2").value("value2").key("key3").value("value3").endObject();
  System.out.println(writer.toString());
  /*
   *  experiencing parsing methods in JSONTokener objects
   *  we pass an inputStream after opening a connection to an URL 
  */
  InputStream jsonStreamExample = uri.toURL().openStream();
  BufferedReader reader = new BufferedReader(new InputStreamReader(jsonStreamExample));
  StringBuilder builder = new StringBuilder();
  String aux = "";
  while ((aux = reader.readLine()) != null) {
      builder.append(aux);
  }
  String text = builder.toString();
  System.out.println(text);

  jsonStreamExample = uri.toURL().openStream();
  JSONTokener tokener = new JSONTokener(jsonStreamExample);
  System.out.println("1-->"+tokener.next()+"<-- is a bracket");
  System.out.println("2-->"+tokener.next()+"<-- is a back-space");
  System.out.println("3-->"+tokener.next()+"<-- is a space");
  System.out.println("4-->"+tokener.next()+"<-- is a space");
  System.out.println("5-->"+tokener.next()+"<-- is a space");
  System.out.println("6-->"+tokener.next()+"<-- is a space");
  System.out.println("7-->"+tokener.next()+"<-- is a quote");
  System.out.println("8-->"+tokener.next()+"<-- is first character of \"name\"  ");
  System.out.println("9-->"+tokener.nextValue()+"<-- is the value that comes next");
  System.out.print(tokener.next()+","+tokener.next());
  System.out.println("\nthis is the value that comes next to the space after ':' --> "+tokener.nextValue());

}
}

Revision: 67305
at September 6, 2014 07:49 by prgrmmraben


Updated Code
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.net.URI;
import java.net.URISyntaxException;
import org.json.*;


/**
 * @author prgrmmr.aben [at] gmail (dot) com
 * http://fivesnippets.blogspot.com/2014/08/servlet-filter-for-ddos-spam-etc.html
 * please give back a small donation if you find
 * this little educational snippet of code useful 
 */
public class test {
 
 int intValue=0;
 public int getIntValue() {
  return intValue;
 }

 public void setIntValue(int intValue) {
  this.intValue = intValue;
 }

 public String getStringValue() {
  return stringValue;
 }

 public void setStringValue(String stringValue) {
  this.stringValue = stringValue;
 }

 public Double getDoubleValue() {
  return doubleValue;
 }

 public void setDoubleValue(Double doubleValue) {
  this.doubleValue = doubleValue;
 }
 
 String stringValue="azerty0123";
 Double doubleValue = new Double(100.01);
 static URI uri =null;
 static { //is there a way to create an object which constructor can throw
    //an exception without using static block?, please answer me in comments
  try {
  uri = new URI("https://raw.githubusercontent.com/originals974/symfony/master/composer.json");
 } catch (URISyntaxException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 }

 public static void main(String[] args) throws IOException, URISyntaxException  {
  // TODO Auto-generated method stub
  /*
   *  creation of an URI object consisted of parsing a Json file in this address
   *  creation of two Json objects one by default construct,
   *  the other with a bean as an argument, a bean is an object 
   *  with public getters and setters
  */
  JSONObject jsonObj = new JSONObject();
  JSONObject jsonObj2 = new JSONObject(new test());
  //put key/value pairs and experimenting the toString() method
  //on Json objects
  jsonObj.put("value1", "qwerty");
  jsonObj.accumulate("value1", "azerty");
  System.out.println(jsonObj.toString());
  System.out.println(jsonObj2.toString());
  //we can put objects of type JSONObject, like we did with beans,
  //we do with JSONObject objects
  jsonObj.put("innerJson", jsonObj2);
  System.out.println(jsonObj.toString());
  jsonObj.remove("innerJson");
  System.out.println(jsonObj.toString());
  //JSONObject.doubleToString() returns null
  //when NaN (not finite number) value is passed as its argument
  System.out.println(JSONObject.doubleToString(new Double(0.0/0.0)));
  System.out.println(JSONObject.doubleToString(new Double(0.0/2)));
  //getting Names (keys) of jsonObj2
  for (String string : JSONObject.getNames(jsonObj2)) {
   System.out.print(string+", ");
  }
  System.out.println();
  //you need to activate assertions, pass -ea as an argument to the JVM
  assert !jsonObj.isNull("[email protected]") : "key not found";
  /*
   *  making a JSONWriter object to put key/value pairs in "quick and convenient way"
   *  as stated in the documentation of JSONWriter
  */
  StringWriter writer = new StringWriter();
  JSONWriter JsonWriter = new JSONWriter(writer);
  JsonWriter.object().key("key1").value(new String("value1")).key("key2").value("value2").key("key3").value("value3").endObject();
  System.out.println(writer.toString());
  /*
   *  experiencing parsing methods in JSONTokener objects
   *  we pass an inputStream after opening a connection to an URL 
  */
  InputStream jsonStreamExample = uri.toURL().openStream();
  BufferedReader reader = new BufferedReader(new InputStreamReader(jsonStreamExample));
  StringBuilder builder = new StringBuilder();
  String aux = "";
  while ((aux = reader.readLine()) != null) {
      builder.append(aux);
  }
  String text = builder.toString();
  System.out.println(text);

  jsonStreamExample = uri.toURL().openStream();
  JSONTokener tokener = new JSONTokener(jsonStreamExample);
  System.out.println("1-->"+tokener.next()+"<-- is a bracket");
  System.out.println("2-->"+tokener.next()+"<-- is a back-space");
  System.out.println("3-->"+tokener.next()+"<-- is a space");
  System.out.println("4-->"+tokener.next()+"<-- is a space");
  System.out.println("5-->"+tokener.next()+"<-- is a space");
  System.out.println("6-->"+tokener.next()+"<-- is a space");
  System.out.println("7-->"+tokener.next()+"<-- is a quote");
  System.out.println("8-->"+tokener.next()+"<-- is first character of \"name\"  ");
  System.out.println("9-->"+tokener.nextValue()+"<-- is the value that comes next");
  System.out.print(tokener.next()+","+tokener.next());
  System.out.println("\nthis is the value that comes next to the space after ':' --> "+tokener.nextValue());

}
}

Revision: 67304
at September 6, 2014 07:29 by prgrmmraben


Initial Code
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.net.URI;
import java.net.URISyntaxException;
import org.json.*;


/**
 * @author prgrmmr.aben [at] gmail (dot) com
 * please give back a small donation if you find
 * this little educational snippet of code useful 
 */
public class test {
 
 int intValue=0;
 public int getIntValue() {
  return intValue;
 }

 public void setIntValue(int intValue) {
  this.intValue = intValue;
 }

 public String getStringValue() {
  return stringValue;
 }

 public void setStringValue(String stringValue) {
  this.stringValue = stringValue;
 }

 public Double getDoubleValue() {
  return doubleValue;
 }

 public void setDoubleValue(Double doubleValue) {
  this.doubleValue = doubleValue;
 }
 
 String stringValue="azerty0123";
 Double doubleValue = new Double(100.01);
 static URI uri =null;
 static { //is there a way to create an object which constructor can throw
    //an exception without using static block?, please answer me in comments
  try {
  uri = new URI("https://raw.githubusercontent.com/originals974/symfony/master/composer.json");
 } catch (URISyntaxException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 }

 public static void main(String[] args) throws IOException, URISyntaxException  {
  // TODO Auto-generated method stub
  /*
   *  creation of an URI object consisted of parsing a Json file in this address
   *  creation of two Json objects one by default construct,
   *  the other with a bean as an argument, a bean is an object 
   *  with public getters and setters
  */
  JSONObject jsonObj = new JSONObject();
  JSONObject jsonObj2 = new JSONObject(new test());
  //put key/value pairs and experimenting the toString() method
  //on Json objects
  jsonObj.put("value1", "qwerty");
  jsonObj.accumulate("value1", "azerty");
  System.out.println(jsonObj.toString());
  System.out.println(jsonObj2.toString());
  //we can put objects of type JSONObject, like we did with beans,
  //we do with JSONObject objects
  jsonObj.put("innerJson", jsonObj2);
  System.out.println(jsonObj.toString());
  jsonObj.remove("innerJson");
  System.out.println(jsonObj.toString());
  //JSONObject.doubleToString() returns null
  //when NaN (not finite number) value is passed as its argument
  System.out.println(JSONObject.doubleToString(new Double(0.0/0.0)));
  System.out.println(JSONObject.doubleToString(new Double(0.0/2)));
  //getting Names (keys) of jsonObj2
  for (String string : JSONObject.getNames(jsonObj2)) {
   System.out.print(string+", ");
  }
  System.out.println();
  //you need to activate assertions, pass -ea as an argument to the JVM
  assert !jsonObj.isNull("[email protected]") : "key not found";
  /*
   *  making a JSONWriter object to put key/value pairs in "quick and convenient way"
   *  as stated in the documentation of JSONWriter
  */
  StringWriter writer = new StringWriter();
  JSONWriter JsonWriter = new JSONWriter(writer);
  JsonWriter.object().key("key1").value(new String("value1")).key("key2").value("value2").key("key3").value("value3").endObject();
  System.out.println(writer.toString());
  /*
   *  experiencing parsing methods in JSONTokener objects
   *  we pass an inputStream after opening a connection to an URL 
  */
  InputStream jsonStreamExample = uri.toURL().openStream();
  BufferedReader reader = new BufferedReader(new InputStreamReader(jsonStreamExample));
  StringBuilder builder = new StringBuilder();
  String aux = "";
  while ((aux = reader.readLine()) != null) {
      builder.append(aux);
  }
  String text = builder.toString();
  System.out.println(text);

  jsonStreamExample = uri.toURL().openStream();
  JSONTokener tokener = new JSONTokener(jsonStreamExample);
  System.out.println("1-->"+tokener.next()+"<-- is a bracket");
  System.out.println("2-->"+tokener.next()+"<-- is a back-space");
  System.out.println("3-->"+tokener.next()+"<-- is a space");
  System.out.println("4-->"+tokener.next()+"<-- is a space");
  System.out.println("5-->"+tokener.next()+"<-- is a space");
  System.out.println("6-->"+tokener.next()+"<-- is a space");
  System.out.println("7-->"+tokener.next()+"<-- is a quote");
  System.out.println("8-->"+tokener.next()+"<-- is first character of \"name\"  ");
  System.out.println("9-->"+tokener.nextValue()+"<-- is the value that comes next");
  System.out.print(tokener.next()+","+tokener.next());
  System.out.println("\nthis is the value that comes next to the space after ':' --> "+tokener.nextValue());

}
}

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

Initial Description
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

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

Initial Tags
json

Initial Language
Java