0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Java で JSON の Create

Last updated at Posted at 2020-01-10

Jackson を使って JSON ファイルを作成します。

プログラム

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 ("*** 開始 ***");

        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 ("*** 終了 ***");
}

// -----------------------------------------------------------------------
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","宇都宮",19825,"2008-2-12");
        dict_aa = dict_append_proc
                (dict_aa,"t0922","小山",35749,"2008-10-14");
        dict_aa = dict_append_proc
                (dict_aa,"t0923","佐野",49128,"2008-4-9");
        dict_aa = dict_append_proc
                (dict_aa,"t0924","足利",85972,"2008-3-21");
        dict_aa = dict_append_proc
                (dict_aa,"t0925","日光",64152,"2008-7-25");
        dict_aa = dict_append_proc
                (dict_aa,"t0926","下野",85793,"2008-10-9");
        dict_aa = dict_append_proc
                (dict_aa,"t0927","さくら",73164,"2008-2-11");
        dict_aa = dict_append_proc
                (dict_aa,"t0928","矢板",65237,"2008-5-27");
        dict_aa = dict_append_proc
                (dict_aa,"t0929","真岡",28754,"2008-1-5");
        dict_aa = dict_append_proc
                (dict_aa,"t0930","栃木",79571,"2008-7-4");
        dict_aa = dict_append_proc
                (dict_aa,"t0931","大田原",36952,"2008-9-27");
        dict_aa = dict_append_proc
                (dict_aa,"t0932","鹿沼",42316,"2008-10-24");
        dict_aa = dict_append_proc
                (dict_aa,"t0933","那須塩原",64753,"2008-12-17");
        dict_aa = dict_append_proc
                (dict_aa,"t0934","那須烏山",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.14.2.jar:$(JAR)/jackson-core-2.14.2.jar:$(JAR)/jackson-databind-2.14.2.jar
json_create.class: json_create.java
	javac json_create.java

clean:
        rm -f *.class

実行

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

確認したバージョン

$ javac --version
javac 18.0.2-ea

$ java --version
openjdk 18.0.2-ea 2022-07-19
OpenJDK Runtime Environment (build 18.0.2-ea+9-Ubuntu-2)
OpenJDK 64-Bit Server VM (build 18.0.2-ea+9-Ubuntu-2, mixed mode, sharing)

関連情報

Java で JSON の Read

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?