BAEL 1269 Intro to JSON-JAVA (#3493)
* Final commit * Made changes as per last review * Moved from core-java to json module
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
3e4b6df194
commit
15f18bbb83
59
json/src/main/java/com/baeldung/jsonjava/CDLDemo.java
Normal file
59
json/src/main/java/com/baeldung/jsonjava/CDLDemo.java
Normal file
@@ -0,0 +1,59 @@
|
||||
package com.baeldung.jsonjava;
|
||||
|
||||
import org.json.CDL;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONTokener;
|
||||
|
||||
public class CDLDemo {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("7.1. Producing JSONArray Directly from Comma Delimited Text: ");
|
||||
jsonArrayFromCDT();
|
||||
|
||||
System.out.println("\n7.2. Producing Comma Delimited Text from JSONArray: ");
|
||||
cDTfromJSONArray();
|
||||
|
||||
System.out.println("\n7.3.1. Producing JSONArray of JSONObjects Using Comma Delimited Text: ");
|
||||
jaOfJOFromCDT2();
|
||||
|
||||
System.out.println("\n7.3.2. Producing JSONArray of JSONObjects Using Comma Delimited Text: ");
|
||||
jaOfJOFromCDT2();
|
||||
}
|
||||
|
||||
public static void jsonArrayFromCDT() {
|
||||
JSONArray ja = CDL.rowToJSONArray(new JSONTokener("England, USA, Canada"));
|
||||
System.out.println(ja);
|
||||
}
|
||||
|
||||
public static void cDTfromJSONArray() {
|
||||
JSONArray ja = new JSONArray("[\"England\",\"USA\",\"Canada\"]");
|
||||
String cdt = CDL.rowToString(ja);
|
||||
System.out.println(cdt);
|
||||
}
|
||||
|
||||
public static void jaOfJOFromCDT() {
|
||||
String string =
|
||||
"name, city, age \n" +
|
||||
"john, chicago, 22 \n" +
|
||||
"gary, florida, 35 \n" +
|
||||
"sal, vegas, 18";
|
||||
|
||||
JSONArray result = CDL.toJSONArray(string);
|
||||
System.out.println(result.toString());
|
||||
}
|
||||
|
||||
public static void jaOfJOFromCDT2() {
|
||||
JSONArray ja = new JSONArray();
|
||||
ja.put("name");
|
||||
ja.put("city");
|
||||
ja.put("age");
|
||||
|
||||
String string =
|
||||
"john, chicago, 22 \n" +
|
||||
"gary, florida, 35 \n" +
|
||||
"sal, vegas, 18";
|
||||
|
||||
JSONArray result = CDL.toJSONArray(ja, string);
|
||||
System.out.println(result.toString());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user