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