-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathJsonJavaHelloWorld.java
More file actions
28 lines (24 loc) · 803 Bytes
/
JsonJavaHelloWorld.java
File metadata and controls
28 lines (24 loc) · 803 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package json_java;
import org.json.JSONObject;
/**
* Json-Java Hello World
*
*/
public class JsonJavaHelloWorld {
public static void main(String[] args) {
// convert Java to json
JSONObject root = new JSONObject();
root.put("message", "Hi");
JSONObject place = new JSONObject();
place.put("name", "World!");
root.put("place", place);
String json = root.toString();
System.out.println(json);
System.out.println();
// convert json to Java
JSONObject jsonObject = new JSONObject(json);
String message = jsonObject.getString("message");
String name = jsonObject.getJSONObject("place").getString("name");
System.out.println(message + " " + name);
}
}