|
| 1 | +package org.json; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.Properties; |
| 6 | + |
| 7 | +//This class' intention is to explain to new-comers the basics of this project |
| 8 | + |
| 9 | + |
| 10 | +public class JSONExamples { |
| 11 | + |
| 12 | + //EXPLAINING THE DIFFERENT WAYS OF CREATING A JSON |
| 13 | + |
| 14 | + private static void JSONExampleArray1() { |
| 15 | + //We create a JSONObject from a String containing an array using JSONArray |
| 16 | + //Firstly, we declare an Array in a String |
| 17 | + String arrayStr = |
| 18 | + "["+"true,"+"false,"+ "\"true\","+ "\"false\","+"\"hello\","+"23.45e-4,"+ |
| 19 | + "\"23.45\","+"42,"+"\"43\","+"["+"\"world\""+"],"+ |
| 20 | + "{"+ |
| 21 | + "\"key1\":\"value1\","+ |
| 22 | + "\"key2\":\"value2\","+ |
| 23 | + "\"key3\":\"value3\","+ |
| 24 | + "\"key4\":\"value4\""+ |
| 25 | + "},"+ |
| 26 | + "0,"+"\"-1\""+ |
| 27 | + "]"; |
| 28 | + //Then, we initializate the JSONArray thanks to its constructor |
| 29 | + JSONArray array = new JSONArray(arrayStr); |
| 30 | + System.out.println("Values array: "+ array); |
| 31 | + //We convert that array into a JSONObject, but first, we need the labels, so we need another JSONArray with the labels Here we will use an auxiliary function to get one for the example. |
| 32 | + JSONArray list = listNumberArray(array.length()); |
| 33 | + System.out.println("Label Array: "+ list.toString()); |
| 34 | + //Now, we construct the JSONObject using both the value array and the label array. |
| 35 | + JSONObject object = array.toJSONObject(list); |
| 36 | + System.out.println("Final JSONOBject: " + object); |
| 37 | + } |
| 38 | + //This method creates an JSONArray of labels in which those are generated by their positions |
| 39 | + private static JSONArray listNumberArray(int max){ |
| 40 | + JSONArray res = new JSONArray(); |
| 41 | + for (int i=0; i<max;i++) { |
| 42 | + //The value of the labels must be an String in order to make it work |
| 43 | + res.put(String.valueOf(i)); |
| 44 | + } |
| 45 | + return res; |
| 46 | + } |
| 47 | + |
| 48 | + private static void JSONExampleArray2() { |
| 49 | + //We can also create an Array without a String by creating an empty array and adding elements to it |
| 50 | + JSONArray array = new JSONArray(); |
| 51 | + //Adding elements with .put() |
| 52 | + array.put("value"); |
| 53 | + array.put(5); |
| 54 | + array.put(-23.45e67); |
| 55 | + array.put(true); |
| 56 | + |
| 57 | + //We convert it to JSONObject providing a label arrray like last time |
| 58 | + JSONArray list = listNumberArray(array.length()); |
| 59 | + JSONObject object = array.toJSONObject(list); |
| 60 | + System.out.println("Final JSONOBject: " + object); |
| 61 | + } |
| 62 | + |
| 63 | + private static void JSONExampleStringer() { |
| 64 | + //We initializate the JSONStringer |
| 65 | + JSONStringer jsonStringer = new JSONStringer(); |
| 66 | + //Now we start the process of adding elements with .object() |
| 67 | + jsonStringer.object(); |
| 68 | + //We can now add elements as keys and values with .values () and .key() |
| 69 | + jsonStringer.key("trueValue").value(true); |
| 70 | + jsonStringer.key("falseValue").value(false); |
| 71 | + jsonStringer.key("nullValue").value(null); |
| 72 | + jsonStringer.key("stringValue").value("hello world!"); |
| 73 | + jsonStringer.key("complexStringValue").value("h\be\tllo w\u1234orld!"); |
| 74 | + jsonStringer.key("intValue").value(42); |
| 75 | + jsonStringer.key("doubleValue").value(-23.45e67); |
| 76 | + //We end this prcedure with .ednObject |
| 77 | + jsonStringer.endObject(); |
| 78 | + //Once we have a JSONStringer, we convert it to JSONObject generating a String and using JSONObject's contructor. |
| 79 | + String str = jsonStringer.toString(); |
| 80 | + JSONObject jsonObject = new JSONObject(str); |
| 81 | + |
| 82 | + System.out.println("Final JSONOBject: " + jsonObject); |
| 83 | + } |
| 84 | + |
| 85 | + private static void JSONExampleObject1() { |
| 86 | + //We can create a JSONObject from a String with the class builder |
| 87 | + String string = "{\"0\":\"value\",\"1\":5,\"2\":-2.345E68,\"3\":true}"; |
| 88 | + JSONObject example = new JSONObject(string); |
| 89 | + System.out.println("Final JSONObject: " + example); |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + private static void JSONExampleObject2() { |
| 94 | + //We can also create a JSONObject directly without messing around with any of the other functions. |
| 95 | + JSONObject example = new JSONObject(); |
| 96 | + //Now we add the keys and values in a similar way as the Stringer method |
| 97 | + example.put("key", "value"); |
| 98 | + //As you can see, the first entry is the key and the second would be its associeted value. |
| 99 | + example.put("key2", 5); |
| 100 | + example.put("key3", -23.45e67); |
| 101 | + example.put("trueValue", true); |
| 102 | + //We can't add null values thougth |
| 103 | + //example.put("nullValue", null); |
| 104 | + |
| 105 | + System.out.println("Final JSONOBject: " + example); |
| 106 | + } |
| 107 | + |
| 108 | + private static void JSONExampleObject3() { |
| 109 | + //We can also create a JSONObject with a Java Map |
| 110 | + //YoU will need a Map whose keys are Strings. The values can be whatever you want |
| 111 | + Map<String,Double> map = new HashMap<String, Double>(); |
| 112 | + |
| 113 | + map.put("key1", 1.0); |
| 114 | + map.put("key2", -23.45e67); |
| 115 | + |
| 116 | + //We create the JSONObject with the map with its class builder |
| 117 | + JSONObject example = new JSONObject(map); |
| 118 | + System.out.println("Final JSONOBject: " + example); |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | + private static void JSONExamplWriter() { |
| 124 | + //This method works in a very similar way to Object and Stringer in the construction of the JSON. |
| 125 | + //The difference is that it needs a Java object called "Appendable" like StringBuilder |
| 126 | + StringBuilder write = new StringBuilder(); |
| 127 | + JSONWriter jsonWriter = new JSONWriter(write); |
| 128 | + //We behave now the same way as Stringer |
| 129 | + jsonWriter.object(); |
| 130 | + |
| 131 | + jsonWriter.key("trueValue").value(true); |
| 132 | + jsonWriter.key("falseValue").value(false); |
| 133 | + jsonWriter.key("nullValue").value(null); |
| 134 | + jsonWriter.key("stringValue").value("hello world!"); |
| 135 | + jsonWriter.key("complexStringValue").value("h\be\tllo w\u1234orld!"); |
| 136 | + jsonWriter.key("intValue").value(42); |
| 137 | + jsonWriter.key("doubleValue").value(-23.45e67); |
| 138 | + |
| 139 | + jsonWriter.endObject(); |
| 140 | + |
| 141 | + //The resoult should be in the "write" object |
| 142 | + |
| 143 | + System.out.println("JSON: " + write.toString()); |
| 144 | + |
| 145 | + //The difference is that we don't get a JSONObject in this one. |
| 146 | + |
| 147 | + |
| 148 | + } |
| 149 | + |
| 150 | + |
| 151 | + private static void JSONExampleTokener() { |
| 152 | + //A partir de una String podemos crear un JSONTokener, que lo podemos usar alternativamente para JSONArray,JSONObject |
| 153 | + String string = "this is not a valid JSON string"; |
| 154 | + JSONTokener token = new JSONTokener(string); |
| 155 | + |
| 156 | + //Now you can use the token in JSONObject and Array the same way as a String |
| 157 | + JSONObject object = new JSONObject(token); |
| 158 | + JSONArray array = new JSONArray(token); |
| 159 | + |
| 160 | + } |
| 161 | + |
| 162 | + //CONVERSIONS |
| 163 | + |
| 164 | + private static void JSONObjectToArray() { |
| 165 | + //We start with a JSONObject |
| 166 | + String string = "{\"0\":\"value\",\"1\":5,\"2\":-2.345E68,\"3\":true}"; |
| 167 | + JSONObject example = new JSONObject(string); |
| 168 | + |
| 169 | + //We need a list of key strings like the reverse operation |
| 170 | + |
| 171 | + JSONArray keyStrings = listNumberArray(example.length()); |
| 172 | + |
| 173 | + //Then we convert to the Array using both elelements |
| 174 | + |
| 175 | + JSONArray array = example.toJSONArray(keyStrings); |
| 176 | + |
| 177 | + System.out.println("Final JSONArray: " + array); |
| 178 | + |
| 179 | + } |
| 180 | + private static void XMLToExampleConversion() { |
| 181 | + //We start with a JSONObject |
| 182 | + |
| 183 | + String string = "{\"0\":\"value\",\"1\":5,\"2\":-2.345E68,\"3\":true}"; |
| 184 | + JSONObject example = new JSONObject(string); |
| 185 | + //We obtain a String with XML format with toString() |
| 186 | + String output = XML.toString(example); |
| 187 | + System.out.println("Final XML: " + output); |
| 188 | + } |
| 189 | + |
| 190 | + private static void XMLFromExampleConversion() { |
| 191 | + //We start with a string with the XML format |
| 192 | + String string = "<0>value</0><1>5</1><2>-2.345E+68</2><3>true</3>"; |
| 193 | + //We obtain a JSONObject with toJSONOBject() |
| 194 | + JSONObject output = XML.toJSONObject(string); |
| 195 | + |
| 196 | + System.out.println("Final JSONObject: " + output); |
| 197 | + } |
| 198 | + |
| 199 | + private static void CookieToExampleConversion() { |
| 200 | + //We start with a JSONObject |
| 201 | + //The JSONOBject needs to entries that gives the cookie a name and gives the field "name" a name too. |
| 202 | + //The Cokkie format doesn't support booleans |
| 203 | + String string = "{\"name\":\"Cookie-Name\",\"value\":\"name\",\"1\":5,\"2\":-2.345E68,\"3\":'true'}"; |
| 204 | + JSONObject example = new JSONObject(string); |
| 205 | + |
| 206 | + //We obtain a String with Cookie format with toString() |
| 207 | + String output = Cookie.toString(example); |
| 208 | + System.out.println("Final Cookie: " + output); |
| 209 | + } |
| 210 | + private static void CookieFromExampleConversion() { |
| 211 | + //We start with a string with the Cookie format |
| 212 | + String string = "Cookie-Name=name;1=5;2=-2.345E%2b68;3=true"; |
| 213 | + //We obtain a JSONObject with toJSONOBject() |
| 214 | + JSONObject output = Cookie.toJSONObject(string); |
| 215 | + System.out.println("Final JSONObject: " + output); |
| 216 | + } |
| 217 | + |
| 218 | + |
| 219 | + private static void HTTPToExampleConversion() { |
| 220 | + //We start with a JSONObject |
| 221 | + //The JSONObject must have the minimun header for a HTTP request or header |
| 222 | + String string = "{\"Method\":\"POST\",\"Request-URI\":'/',\"HTTP-Version\":'HTTP/1.1',\"Value1\":true,\"Value2\":2,\"Value3\":-2.345E68}"; |
| 223 | + JSONObject example = new JSONObject(string); |
| 224 | + //We obtain a String with HTTP format with toString() |
| 225 | + String output = HTTP.toString(example); |
| 226 | + System.out.println("Final HTTP: " + output); |
| 227 | + } |
| 228 | + |
| 229 | + private static void HTTPFromExampleConversion() { |
| 230 | + //We start with a string with the HTTP format |
| 231 | + String string = "Final HTTP: POST '/' HTTP/1.1 Value3: -2.345E+68 Value1: true Value2: 2"; |
| 232 | + //We obtain a JSONObject with toJSONOBject() |
| 233 | + JSONObject output = HTTP.toJSONObject(string); |
| 234 | + System.out.println("Final JSONObject: " + output); |
| 235 | + } |
| 236 | + |
| 237 | +private static String CDLToExampleConversion() { |
| 238 | + //We start with some JSONObjects with the same values in the keys but different values in the "values" |
| 239 | + String string = "{\"0\":\"value\",\"1\":5,\"2\":-2.345E68,\"3\":true}"; |
| 240 | + JSONObject example = new JSONObject(string); |
| 241 | + |
| 242 | + String string2 = "{\"0\":\"value2\",\"1\":6,\"2\":-8.345E68,\"3\":false}"; |
| 243 | + JSONObject example2 = new JSONObject(string2); |
| 244 | + |
| 245 | + //We need now a JSONArray with those JSONObjects |
| 246 | + JSONArray array = new JSONArray(); |
| 247 | + array.put(example); |
| 248 | + array.put(example2); |
| 249 | + //We obtain a String with XML format with toString() |
| 250 | + String output = CDL.toString(array); |
| 251 | + System.out.println("Final CDL: \r\n" + output); |
| 252 | + return output; |
| 253 | + } |
| 254 | + |
| 255 | +private static void CDLFromExampleConversion() { |
| 256 | + //We start wtih a String with the CDL format |
| 257 | + //String string = CDLToExampleConversion(); |
| 258 | + String string = "0,1,2,3\n" |
| 259 | + + "value,5,-2.345E+68,true\n" |
| 260 | + + "value2,6,-8.345E+68,false"; |
| 261 | + //We obtain a JSONArray with toJSONOBject() |
| 262 | + JSONArray output = CDL.toJSONArray(string); |
| 263 | + System.out.println("Final JSONArray: " + output); |
| 264 | +} |
| 265 | + private static Properties PropertyToExampleConversion() { |
| 266 | + //We start with a JSONObject |
| 267 | + String string = "{\"0\":\"value\",\"1\":5,\"2\":-2.345E68,\"3\":true}"; |
| 268 | + JSONObject example = new JSONObject(string); |
| 269 | + //We obtain a String with Properties format with toString() |
| 270 | + Properties output = Property.toProperties(example); |
| 271 | + System.out.println("Final Properties: " + output); |
| 272 | + return output; |
| 273 | + } |
| 274 | + |
| 275 | + private static void PropertyFromExampleConversion() { |
| 276 | + //We start with a Properties object |
| 277 | + Properties input = PropertyToExampleConversion(); |
| 278 | + //We obtain a JSONObject with toJSONOBject() |
| 279 | + JSONObject output = Property.toJSONObject(input); |
| 280 | + System.out.println("Final JSONObject: " + output); |
| 281 | + } |
| 282 | + |
| 283 | + public static void main(String[] args) { |
| 284 | + //JSONObjectToArray(); |
| 285 | + //JSONExampleArray1(); |
| 286 | + //JSONExampleArray2(); |
| 287 | + //JSONExampleStringer(); |
| 288 | + //JSONExampleObject1(); |
| 289 | + //JSONExampleObject2(); |
| 290 | + //JSONExampleObject3(); |
| 291 | + //JSONExamplWriter(); |
| 292 | + //XMLToExampleConversion(); |
| 293 | + //XMLFromExampleConversion(); |
| 294 | + //CookieToExampleConversion(); |
| 295 | + //CookieFromExampleConversion(); |
| 296 | + //HTTPToExampleConversion(); |
| 297 | + //HTTPFromExampleConversion(); |
| 298 | + //CDLToExampleConversion(); |
| 299 | + //CDLFromExampleConversion(); |
| 300 | + //PropertyToExampleConversion(); |
| 301 | + //PropertyFromExampleConversion(); |
| 302 | + } |
| 303 | + |
| 304 | +} |
0 commit comments