Create JSON in Java

Create a JSON file using Jackson.

json_create.java


// -----------------------------------------------------------------------
/*
        json_create.java

                                        Jan/10/2020
*/
// -----------------------------------------------------------------------
import  java.util.HashMap;
import  java.io.File;
import  java.io.FileWriter;
import  java.io.BufferedWriter;
import  java.io.PrintWriter;

// -----------------------------------------------------------------------
public class json_create
{

// -----------------------------------------------------------------------
public  static void main (String [] args)
{
        System.err.println ("***start***");

        String json_file = args[0];

        HashMap <String, HashMap <String,String>>
                dict_aa = data_prepare_proc ();

        String json_str = json_manipulate.dict_to_json_proc (dict_aa);

        file_write_proc (json_file,json_str);

        System.err.println ("***End***");
}

// -----------------------------------------------------------------------
static HashMap <String, HashMap <String,String>> data_prepare_proc ()
{
        HashMap <String, HashMap<String,String>> dict_aa
                = new HashMap <String, HashMap<String,String>> ();

        dict_aa = dict_append_proc
                (dict_aa,"t0921","Utsunomiya",19825,"2008-2-12");
        dict_aa = dict_append_proc
                (dict_aa,"t0922","Oyama",35749,"2008-10-14");
        dict_aa = dict_append_proc
                (dict_aa,"t0923","Sano",49128,"2008-4-9");
        dict_aa = dict_append_proc
                (dict_aa,"t0924","Ashikaga",85972,"2008-3-21");
        dict_aa = dict_append_proc
                (dict_aa,"t0925","sunlight",64152,"2008-7-25");
        dict_aa = dict_append_proc
                (dict_aa,"t0926","Shimono",85793,"2008-10-9");
        dict_aa = dict_append_proc
                (dict_aa,"t0927","Cherry Blossoms",73164,"2008-2-11");
        dict_aa = dict_append_proc
                (dict_aa,"t0928","Sheet pile",65237,"2008-5-27");
        dict_aa = dict_append_proc
                (dict_aa,"t0929","Moka",28754,"2008-1-5");
        dict_aa = dict_append_proc
                (dict_aa,"t0930","Tochigi",79571,"2008-7-4");
        dict_aa = dict_append_proc
                (dict_aa,"t0931","Otawara",36952,"2008-9-27");
        dict_aa = dict_append_proc
                (dict_aa,"t0932","Kanuma",42316,"2008-10-24");
        dict_aa = dict_append_proc
                (dict_aa,"t0933","Nasushiobara",64753,"2008-12-17");
        dict_aa = dict_append_proc
                (dict_aa,"t0934","Nasukarasuyama",71298,"2008-11-15");


        return  dict_aa;
}


// -----------------------------------------------------------------------
static HashMap <String, HashMap<String,String>>
dict_append_proc (HashMap <String, HashMap <String,String>> dict_aa,
        String key_aa,String name,int population,String date_mod)
{
        String str_population = Integer.toString (population);

        HashMap <String,String> dict_unit
                                = new HashMap <String,String> ();

        dict_unit.put ("name",name);
        dict_unit.put ("population",str_population);
        dict_unit.put ("date_mod",date_mod);

        dict_aa.put (key_aa,dict_unit);

        return  dict_aa;
}

// ----------------------------------------------------------------
static void file_write_proc (String file_out,String str_out)
{
        try
        {
        File file = new File (file_out);
        FileWriter filewriter = new FileWriter (file);
        BufferedWriter bw = new BufferedWriter (filewriter);

        PrintWriter pw = new PrintWriter (bw);
        pw.println (str_out);
        pw.close ();

        }
        catch (Exception ee)
                {
                ee.printStackTrace ();
                }

}

// -----------------------------------------------------------------------
}
// -----------------------------------------------------------------------

json_maninpulate.java


// -----------------------------------------------------------------------
/*
        json_manipulate.java


                                Jan/10/2020
*/
// -----------------------------------------------------------------------
import  java.util.HashMap;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
// -----------------------------------------------------------------------
public class json_manipulate
{


// -----------------------------------------------------------------------
static String   dict_to_json_proc
        (HashMap <String, HashMap<String,String>> dict_aa)
{
        String str_json = "";

        ObjectMapper mapper = new ObjectMapper();
        try
                {
                str_json = mapper.writeValueAsString(dict_aa);
                }
        catch   (Exception ee)
                {
                ee.printStackTrace ();
                }

        return  str_json;
}

// -----------------------------------------------------------------------
}
// -----------------------------------------------------------------------

Makefile


export JAR=../jar
#
export CLASSPATH=.:$(JAR)/jackson-annotations-2.9.9.jar:$(JAR)/jackson-core-2.9.9.jar:$(JAR)/jackson-databind-2.9.9.jar
json_create.class: json_create.java
        javac json_create.java
clean:
        rm -f *.class

Run

export JAR=../jar
#
export CLASSPATH=.:$JAR/jackson-annotations-2.9.9.jar:$JAR/jackson-core-2.9.9.jar:$JAR/jackson-databind-2.9.9.jar
#
java json_create out01.json

Recommended Posts

Create JSON in Java
Read JSON in Java
POST JSON in Java
Create hyperlinks in Java PowerPoint
Create Azure Functions in Java
POST Json in Java ~ HttpURLConnection ~
Json serialization / deserialization in Java 1.4
Changes in Java 11
Rock-paper-scissors in Java
Pi in Java
FizzBuzz in Java
Create variable length binary data in Java
Try using JSON format API in Java
Create Java Spring Boot project in IntelliJ
Create a TODO app in Java 7 Create Header
Working with huge JSON in Java Lambda
[java] sort in list
Make Blackjack in Java
Create barcodes and QR codes in Java PDF
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Create a CSR with extended information in Java
Combine arrays in Java
"Hello World" in Java
Callable Interface in Java
[Java] Create a filter
Comments in Java source
Code to escape a JSON string in Java
Azure functions in java
Try to create a bulletin board in Java
Parse Json in Scala
Format XML in Java
Simple htmlspecialchars in Java
Let's create a super-simple web framework in Java
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
Date manipulation in Java 8
What's new in Java 8
I tried to create Alexa skill in Java
Use PreparedStatement in Java
What's new in Java 9,10,11
Parallel execution in Java
Initializing HashMap in Java
Convert JSON and YAML in Java (using Jackson and SnakeYAML)
How to create a Java environment in just 3 seconds
I tried to create a Clova skill in Java
How to create a data URI (base64) in Java
Try using RocksDB in Java
Avoid Yubaba's error in Java
[Java] JSON communication with jackson
Get EXIF information in Java
Save Java PDF in Excel
Edit ini in Java: ini4j
Java history in this world