Skip to content

Commit b1998d0

Browse files
author
Andreas Göransson
committed
Removed another exception from JSONArray class
1 parent a09e1ee commit b1998d0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+11276
-7
lines changed
Binary file not shown.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* JSON 4 Processing
3+
* Basic example 2: Creating a JSON Array
4+
*
5+
* Good for sending a large set of primitive values, like sensor readings.
6+
*/
7+
8+
import org.json.*;
9+
10+
void setup(){
11+
12+
// 1. Initialize the Array
13+
JSONArray myJsonArray = new JSONArray();
14+
15+
// 2. Add some content to the array
16+
myJsonArray.put( 4 );
17+
myJsonArray.put( 2 );
18+
19+
println( myJsonArray );
20+
}
21+
22+
void draw(){
23+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* JSON 4 Processing
3+
* Basic example 3: Creating a JSON Array of JSON Objects.
4+
*
5+
* Good for sending multiple complex values, such as database tables.
6+
*/
7+
8+
import org.json.*;
9+
10+
void setup(){
11+
12+
// 1. Initialize the Array
13+
JSONArray myJsonUsers = new JSONArray();
14+
15+
// 2. Create the first object & add to array
16+
JSONObject firstUser = new JSONObject();
17+
firstUser.put( "name", "Andreas" );
18+
firstUser.put( "age", 32 );
19+
myJsonUsers.put( firstUser );
20+
21+
// 3. Create the second object
22+
JSONObject secondUser = new JSONObject();
23+
secondUser.put( "name", "Maria" );
24+
secondUser.put( "age", 28 );
25+
myJsonUsers.put( secondUser );
26+
27+
println( myJsonUsers );
28+
}
29+
30+
void draw(){
31+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* JSON 4 Processing
3+
* Basic example 1: Creating a JSON Object
4+
*
5+
* Good for sending values that has a specific meaning (complex values)
6+
*/
7+
8+
import org.json.*;
9+
10+
void setup(){
11+
12+
// 1. Initialize the object
13+
JSONObject myJsonObject = new JSONObject();
14+
15+
// 2. Add some content to the object
16+
myJsonObject.put( "myIntegerValue", 7 );
17+
18+
println( myJsonObject );
19+
}
20+
21+
void draw(){
22+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* JSON 4 Processing
3+
* Basic example 5: Parsing the HTTP header from www.processing.org
4+
*
5+
* This example creates a JSONObject from the HTTP response when
6+
* contacting a website (www.processing.org).
7+
*/
8+
import org.json.*;
9+
import java.net.URL;
10+
import java.net.URLConnection;
11+
12+
URL url;
13+
URLConnection conn;
14+
15+
void setup() {
16+
// Unfortunately there's a problem in the Processing Client implementation
17+
// that forces us to use straight Java.
18+
try {
19+
url = new URL("http://www.processing.org");
20+
conn = url.openConnection();
21+
}
22+
catch( Exception e) {
23+
}
24+
25+
// We will manually add the entire HTTP reponse to this StringBuffer
26+
// and create the JSONObject using it.
27+
StringBuffer sb = new StringBuffer();
28+
29+
// Construct the String object using the URLConnection.
30+
for (int i = 0;; i++) {
31+
String name = conn.getHeaderFieldKey(i);
32+
String value = conn.getHeaderField(i);
33+
if (name == null && value == null) {
34+
break;
35+
}
36+
if (name == null) {
37+
// Add the value, if there is no key-value pair.
38+
sb.append(value).append("\n");
39+
}
40+
else {
41+
// Add the key-value pair.
42+
sb.append(name).append(":").append(value).append("\n");
43+
}
44+
}
45+
46+
// Create the JSON HTTP instance
47+
HTTP http = new HTTP();
48+
49+
// Create the JSONObject using the HTTP instance
50+
JSONObject obj = http.toJSONObject(sb.toString());
51+
52+
// Print the JSONObject
53+
System.out.println(obj);
54+
}
55+
void draw() {
56+
}
57+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* JSON 4 Processing
3+
* Basic example 1: Creating a JSON Object from a json string
4+
*/
5+
6+
import org.json.*;
7+
8+
void setup(){
9+
10+
// 1. Get the json-string (we'll just create one...)
11+
String jsonstring = "{\"myIntegerValue\":7}";
12+
13+
// 2. Initialize the object
14+
JSONObject myJsonObject = new JSONObject(jsonstring);
15+
16+
println( myJsonObject );
17+
}
18+
19+
void draw(){
20+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* JSON 4 Processing
3+
* Basic example 4: Reading data from the web (Yahoo weather)
4+
*
5+
* This example reads the Weather feed from the Yahoo API's. It's using
6+
* the WOEID for Malmo, you can change this by finding the ID you want
7+
* at http://developer.yahoo.com/weather/
8+
*/
9+
10+
import org.json.*;
11+
12+
void setup() {
13+
// Accessing the weather service
14+
String BASE_URL = "http://weather.yahooapis.com/forecastjson?w=";
15+
String WOEID = "898091";
16+
17+
// Get the JSON formatted response
18+
String response = loadStrings( BASE_URL + WOEID )[0];
19+
20+
// Make sure we got a response.
21+
if ( response != null ) {
22+
// Initialize the JSONObject for the response
23+
JSONObject root = new JSONObject( response );
24+
25+
// Get the "condition" JSONObject
26+
JSONObject condition = root.getJSONObject("condition");
27+
28+
// Get the "temperature" value from the condition object
29+
int temperature = condition.getInt("temperature");
30+
31+
// Print the temperature
32+
println( temperature );
33+
}
34+
}
35+
36+
void draw() {
37+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* JSON 4 Processing
3+
* Basic example 7: Creating a JSON Array of JSON Objects and saving it to a file.
4+
*
5+
* Good for saving multiple complex values, such as database tables.
6+
*
7+
* Author: Andreas Göransson, output additions: Tim Pulver
8+
*/
9+
10+
import org.json.*;
11+
import java.io.File;
12+
13+
void setup(){
14+
15+
// Object creation like in example 3
16+
// =================================================
17+
18+
// 1. Initialize the Array
19+
JSONArray myJsonUsers = new JSONArray();
20+
21+
// 2. Create the first object & add to array
22+
JSONObject firstUser = new JSONObject();
23+
firstUser.put( "name", "Andreas" );
24+
firstUser.put( "age", 32 );
25+
myJsonUsers.put( firstUser );
26+
27+
// 3. Create the second object
28+
JSONObject secondUser = new JSONObject();
29+
secondUser.put( "name", "Maria" );
30+
secondUser.put( "age", 28 );
31+
myJsonUsers.put( secondUser );
32+
33+
// Writing the JSON Array / Object to a file
34+
// =================================================
35+
36+
// will store the JSON Array in the file json_output.txt within the data directory
37+
File file = new File(dataPath("") + File.separator + "json_output.txt");
38+
// Create the data directory if it does not exist
39+
file.getParentFile().mkdirs();
40+
try{
41+
// If the file already exists, it will be overwritten
42+
FileWriter fstream = new FileWriter(file, false);
43+
// Use this instead if you want to append the data to the file
44+
//FileWriter fstream = new FileWriter(file, true);
45+
BufferedWriter out = new BufferedWriter(fstream);
46+
// do the actual writing
47+
myJsonUsers.write(out);
48+
// Close the stream
49+
out.close();
50+
}catch (Exception e){
51+
System.err.println("Error writing the JSON file: " + e.getMessage());
52+
}
53+
}
54+
55+
void draw(){
56+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
4+
<head>
5+
<title>json4processing</title>
6+
<meta name="description" content="a library for the programming environment processing" />
7+
<meta name="keywords" content="processing.org, library" />
8+
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
9+
<meta http-equiv="Content-Language" content="en-us" />
10+
<meta name="ROBOTS" content="index,follow,archive" />
11+
<meta http-equiv="imagetoolbar" content="false" />
12+
<meta name="MSSmartTagsPreventParsing" content="true" />
13+
<meta name="author" content="##author##" />
14+
<meta name="Rating" content="General" />
15+
<meta name="revisit-after" content="7 Days" />
16+
<meta name="doc-class" content="Living Document" />
17+
<link rel="stylesheet" type="text/css" href="./stylesheet.css">
18+
</head>
19+
<body>
20+
<div id="container">
21+
22+
<div id="header">
23+
<h1>json4processing</h1>
24+
</div>
25+
26+
<div id="menu" class="clear">
27+
<ul>
28+
<li><a href="#about">About</a> \ </li>
29+
<li><a href="#download">Download</a> \ </li>
30+
<li><a href="#download">Installation</a> \ </li>
31+
<li><a href="#examples">Examples</a> \ </li>
32+
<li><a href="./reference/index.html" target="_blank">Reference</a></li>
33+
<!-- <li><a href="#demos">Demos</a> \ </li> -->
34+
<!-- <li><a href="#misc">Misc</a> \ </li> -->
35+
<!-- <li><a href="#images">Images</a> \ </li> -->
36+
</ul>
37+
</div>
38+
39+
<div id="content" class="clear">
40+
41+
<div id="about">
42+
<h2>json4processing</h2>
43+
<p>
44+
A library by <a href="##yourLink##">Andreas Goransson</a> for the programming environment <a href="http://www.processing.org" target="_blank">processing</a>. Last update, 11/28/2012.
45+
</p>
46+
<p>
47+
Feel free to replace this paragraph with a description of the library. Contributed libraries are developed, documented, and maintained by members of the Processing community. Further directions are included with each library. For feedback and support, please post to the Discourse. We strongly encourage all libraries to be open source, but not all of them are.
48+
</p>
49+
</div>
50+
51+
52+
53+
<div id="download" class="clear">
54+
<h2>Download</h2>
55+
<p>
56+
Download json4processing version 0.1.5 in
57+
<a href="./download/json4processing-0.1.5.zip">.zip format</a>.
58+
</p>
59+
<h2>Installation</h2>
60+
<p>
61+
Unzip and put the extracted json4processing folder into the libraries folder of your processing sketches. Reference and examples are included in the json4processing folder.
62+
</p>
63+
</div>
64+
65+
66+
<div id="resources">
67+
<p><strong>Keywords</strong> ?</p>
68+
<p><strong>Reference</strong>. Have a look at the javadoc reference <a href="./reference/index.html" target="_blank">here</a>. a copy of the reference is included in the .zip as well.</p>
69+
<p><strong>Source</strong>. The source code of json4processing is available at <a href="https://github.com/agoransson/JSON-processing">github</a>, and its repository can be browsed <a href="https://github.com/agoransson/JSON-processing" target="_blank">here</a>.</p>
70+
</div>
71+
72+
<div id="examples" class="clear">
73+
<h2>Examples</h2>
74+
<p>Find a list of examples in the current distribution of json4processing, or have a look at them by following the links below.</p>
75+
<ul>
76+
<li><a href="examples/create_jsonarray/create_jsonarray.pde">create_jsonarray</a></li> <li><a href="examples/create_jsonarray_of_objects/create_jsonarray_of_objects.pde">create_jsonarray_of_objects</a></li> <li><a href="examples/create_jsonobject/create_jsonobject.pde">create_jsonobject</a></li> <li><a href="examples/create_jsonobject_from_httpheader/create_jsonobject_from_httpheader.pde">create_jsonobject_from_httpheader</a></li> <li><a href="examples/create_jsonobject_from_string/create_jsonobject_from_string.pde">create_jsonobject_from_string</a></li> <li><a href="examples/reading_yahoo_weather/reading_yahoo_weather.pde">reading_yahoo_weather</a></li> <li><a href="examples/write_jsonarray_of_objects/write_jsonarray_of_objects.pde">write_jsonarray_of_objects</a></li>
77+
</ul>
78+
</div>
79+
80+
81+
<div id="info">
82+
<h2>Tested</h2>
83+
<p>
84+
<!-- on which platform has the library been tested? -->
85+
<strong>Platform</strong> linux, windows
86+
87+
<!-- which processing version did you use for testing your library? -->
88+
<br /><strong>Processing</strong> 1.5.1
89+
90+
<!-- does your library depend on any other library or framework? -->
91+
<br /><strong>Dependencies</strong> ?
92+
</p>
93+
</div>
94+
95+
96+
<!-- use the demos section for a list of applets run in a browser. -->
97+
<!--
98+
<div id="demos" class="clear">
99+
<h2>demos</h2>
100+
<p>
101+
find a list of online applet demos below.
102+
103+
<ul>
104+
<li><a href="./applets/demo/index.html">demo</a></li>
105+
</ul>
106+
</p>
107+
</div>
108+
-->
109+
110+
<!-- use the misc section for other relevant information. Activate the link to the misc section in the menu above. -->
111+
<!--
112+
<div id="misc" class="clear">
113+
<p></p>
114+
</div>
115+
-->
116+
117+
<!-- use the images/screenshots section. Activate the link to the misc section in the menu above. -->
118+
<!--
119+
<div id="images" class="clear">
120+
</div>
121+
-->
122+
123+
124+
<br class="clear" />
125+
</div>
126+
127+
<div id="footer">
128+
<p>by Andreas Goransson, (c) 2011.</p>
129+
</div>
130+
</div>
131+
</body>
132+
</html>

0 commit comments

Comments
 (0)