Bael 1269 Intro to JSON Java (#3662)
* Final commit * Made changes as per last review * Moved from core-java to json module * Interrupted thread before logging * Separated out JSON serialization to Java Object * Reverting access modifier
This commit is contained in:
parent
f17816d5e1
commit
d70fbdc273
|
@ -6,16 +6,16 @@ import org.json.JSONTokener;
|
||||||
|
|
||||||
public class CDLDemo {
|
public class CDLDemo {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println("7.1. Producing JSONArray Directly from Comma Delimited Text: ");
|
System.out.println("8.1. Producing JSONArray Directly from Comma Delimited Text: ");
|
||||||
jsonArrayFromCDT();
|
jsonArrayFromCDT();
|
||||||
|
|
||||||
System.out.println("\n7.2. Producing Comma Delimited Text from JSONArray: ");
|
System.out.println("\n8.2. Producing Comma Delimited Text from JSONArray: ");
|
||||||
cDTfromJSONArray();
|
cDTfromJSONArray();
|
||||||
|
|
||||||
System.out.println("\n7.3.1. Producing JSONArray of JSONObjects Using Comma Delimited Text: ");
|
System.out.println("\n8.3.1. Producing JSONArray of JSONObjects Using Comma Delimited Text: ");
|
||||||
jaOfJOFromCDT2();
|
jaOfJOFromCDT2();
|
||||||
|
|
||||||
System.out.println("\n7.3.2. Producing JSONArray of JSONObjects Using Comma Delimited Text: ");
|
System.out.println("\n8.3.2. Producing JSONArray of JSONObjects Using Comma Delimited Text: ");
|
||||||
jaOfJOFromCDT2();
|
jaOfJOFromCDT2();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,10 +5,10 @@ import org.json.JSONObject;
|
||||||
|
|
||||||
public class CookieDemo {
|
public class CookieDemo {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println("8.1. Converting a Cookie String into a JSONObject");
|
System.out.println("9.1. Converting a Cookie String into a JSONObject");
|
||||||
cookieStringToJSONObject();
|
cookieStringToJSONObject();
|
||||||
|
|
||||||
System.out.println("\n8.2. Converting a JSONObject into Cookie String");
|
System.out.println("\n9.2. Converting a JSONObject into Cookie String");
|
||||||
jSONObjectToCookieString();
|
jSONObjectToCookieString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,10 +5,10 @@ import org.json.JSONObject;
|
||||||
|
|
||||||
public class HTTPDemo {
|
public class HTTPDemo {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println("9.1. Converting JSONObject to HTTP Header: ");
|
System.out.println("10.1. Converting JSONObject to HTTP Header: ");
|
||||||
jSONObjectToHTTPHeader();
|
jSONObjectToHTTPHeader();
|
||||||
|
|
||||||
System.out.println("\n9.2. Converting HTTP Header String Back to JSONObject: ");
|
System.out.println("\n10.2. Converting HTTP Header String Back to JSONObject: ");
|
||||||
hTTPHeaderToJSONObject();
|
hTTPHeaderToJSONObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,13 +8,13 @@ import org.json.JSONObject;
|
||||||
|
|
||||||
public class JSONArrayDemo {
|
public class JSONArrayDemo {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println("5.1. Creating JSON Array: ");
|
System.out.println("6.1. Creating JSON Array: ");
|
||||||
creatingJSONArray();
|
creatingJSONArray();
|
||||||
|
|
||||||
System.out.println("\n5.2. Creating JSON Array from JSON string: ");
|
System.out.println("\n6.2. Creating JSON Array from JSON string: ");
|
||||||
jsonArrayFromJSONString();
|
jsonArrayFromJSONString();
|
||||||
|
|
||||||
System.out.println("\n5.3. Creating JSON Array from Collection Object: ");
|
System.out.println("\n6.3. Creating JSON Array from Collection Object: ");
|
||||||
jsonArrayFromCollectionObj();
|
jsonArrayFromCollectionObj();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,9 +15,6 @@ public class JSONObjectDemo {
|
||||||
|
|
||||||
System.out.println("\n4.3. Creating JSONObject from JSON string: ");
|
System.out.println("\n4.3. Creating JSONObject from JSON string: ");
|
||||||
jsonFromJSONString();
|
jsonFromJSONString();
|
||||||
|
|
||||||
System.out.println("\n4.4. Creating JSONObject from Java Bean: ");
|
|
||||||
jsonFromDemoBean();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void jsonFromJSONObject() {
|
public static void jsonFromJSONObject() {
|
||||||
|
@ -46,14 +43,4 @@ public class JSONObjectDemo {
|
||||||
|
|
||||||
System.out.println(jo.toString());
|
System.out.println(jo.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void jsonFromDemoBean() {
|
|
||||||
DemoBean demo = new DemoBean();
|
|
||||||
demo.setId(1);
|
|
||||||
demo.setName("lorem ipsum");
|
|
||||||
demo.setActive(true);
|
|
||||||
|
|
||||||
JSONObject jo = new JSONObject(demo);
|
|
||||||
System.out.println(jo);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.baeldung.jsonjava;
|
||||||
|
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
public class ObjectToFromJSON {
|
||||||
|
|
||||||
|
public static void main(String args[]) throws Exception {
|
||||||
|
System.out.println("\n5.1. Creating JSONObject from Java Bean: ");
|
||||||
|
jsonFromDemoBean();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void jsonFromDemoBean() {
|
||||||
|
DemoBean demo = new DemoBean();
|
||||||
|
demo.setId(1);
|
||||||
|
demo.setName("lorem ipsum");
|
||||||
|
demo.setActive(true);
|
||||||
|
|
||||||
|
JSONObject jo = new JSONObject(demo);
|
||||||
|
System.out.println(jo);
|
||||||
|
}
|
||||||
|
}
|
|
@ -39,15 +39,4 @@ public class JSONObjectIntegrationTest {
|
||||||
|
|
||||||
assertEquals("{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}", jo.toString());
|
assertEquals("{\"city\":\"chicago\",\"name\":\"jon doe\",\"age\":\"22\"}", jo.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenDemoBean_thenCreateJSONObject() {
|
|
||||||
DemoBean demo = new DemoBean();
|
|
||||||
demo.setId(1);
|
|
||||||
demo.setName("lorem ipsum");
|
|
||||||
demo.setActive(true);
|
|
||||||
|
|
||||||
JSONObject jo = new JSONObject(demo);
|
|
||||||
assertEquals("{\"name\":\"lorem ipsum\",\"active\":true,\"id\":1}", jo.toString());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
package com.baeldung.jsonjava;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import org.json.JSONObject;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class ObjectToFromJSONIntegrationTest {
|
||||||
|
@Test
|
||||||
|
public void givenDemoBean_thenCreateJSONObject() {
|
||||||
|
DemoBean demo = new DemoBean();
|
||||||
|
demo.setId(1);
|
||||||
|
demo.setName("lorem ipsum");
|
||||||
|
demo.setActive(true);
|
||||||
|
|
||||||
|
JSONObject jo = new JSONObject(demo);
|
||||||
|
assertEquals("{\"name\":\"lorem ipsum\",\"active\":true,\"id\":1}", jo.toString());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue