/ Published in: Java
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
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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; } return stringValue; } this.stringValue = stringValue; } return doubleValue; } this.doubleValue = doubleValue; } 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(); } } // 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"); //we can put objects of type JSONObject, like we did with beans, //we do with JSONObject objects jsonObj.put("innerJson", jsonObj2); jsonObj.remove("innerJson"); //JSONObject.doubleToString() returns null //when NaN (not finite number) value is passed as its argument //getting Names (keys) of jsonObj2 } //you need to activate assertions, pass -ea as an argument to the JVM assert !jsonObj.isNull("f5fe@al") : "key not found"; /* * making a JSONWriter object to put key/value pairs in "quick and convenient way" * as stated in the documentation of JSONWriter */ JSONWriter JsonWriter = new JSONWriter(writer); JsonWriter.object().key("key1").value(new String("value1")).key("key2").value("value2").key("key3").value("value3").endObject(); /* * experiencing parsing methods in JSONTokener objects * we pass an inputStream after opening a connection to an URL */ StringBuilder builder = new StringBuilder(); while ((aux = reader.readLine()) != null) { builder.append(aux); } jsonStreamExample = uri.toURL().openStream(); JSONTokener tokener = new JSONTokener(jsonStreamExample); System.out.println("\nthis is the value that comes next to the space after ':' --> "+tokener.nextValue()); } }
URL: http://fivesnippets.blogspot.com/2014/09/jsonobject-jsonwriter-and-jsontokener.html