Use Jackson to read the following JSON file.
tochigi.json
{
"t2381": {
"name": "Nagoya",
"population": "74125",
"date_mod": "2009-1-7"
},
"t0922": {
"name": "Oyama",
"population": "17982",
"date_mod": "2009-5-19"
},
"t0923": {
"name": "Sano",
"population": "46819",
"date_mod": "2009-3-28"
}
}
Download the Jackson jar file
SRC="http://repo1.maven.org/maven2/com/fasterxml/jackson/core"
#
wget $SRC/jackson-annotations/2.9.9/jackson-annotations-2.9.9.jar
wget $SRC/jackson-core/2.9.9/jackson-core-2.9.9.jar
wget $SRC/jackson-databind/2.9.9/jackson-databind-2.9.9.jar
#
json_read.java
// -----------------------------------------------------------------------
/*
json_read.java
Jan/10/2020
*/
// -----------------------------------------------------------------------
import java.util.HashMap;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
public class json_read {
public static void main(String[] args) throws Exception {
System.err.println ("***start***");
TypeReference<HashMap <String, HashMap<String,String>>> reference
= new TypeReference<HashMap <String, HashMap<String,String>>> (){};
String file_in = "tochigi.json";
String json = file_io.file_to_str_proc (file_in);
ObjectMapper mapper = new ObjectMapper();
HashMap <String, HashMap<String,String>> dict_aa = mapper.readValue(json, reference);
text_manipulate.dict_display_proc (dict_aa);
System.err.println ("***start***");
}
}
// ----------------------------------------------------------------------
file_io.java
// -----------------------------------------------------------------------
/*
file_io.java
Jan/10/2020
*/
// -----------------------------------------------------------------------
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
// -----------------------------------------------------------------------
public class file_io
{
// -----------------------------------------------------------------------
static String file_to_str_proc (String file_in)
{
String str_in = "";
try
{
FileReader in_file = new FileReader (file_in);
BufferedReader buff = new BufferedReader (in_file);
String line;
while ((line = buff.readLine ()) != null)
{
str_in += line;
}
}
catch (Exception ee)
{
ee.printStackTrace ();
}
return str_in;
}
// -----------------------------------------------------------------------
}
// ----------------------------------------------------------------------
text_manipulate.java
// -----------------------------------------------------------------------
/*
text_manipulate.java
Jan/10/2020
*/
// -----------------------------------------------------------------------
import java.util.HashMap;
import java.util.Set;
import java.util.TreeSet;
// -----------------------------------------------------------------------
public class text_manipulate
{
// -----------------------------------------------------------------------
static void dict_display_proc
(HashMap <String, HashMap <String,String>> dict_aa)
{
HashMap <String,String> dict_unit
= new HashMap <String,String> ();
System.out.println (dict_aa.size ());
Set <String> set_aaa = dict_aa.keySet ();
Set <String> ss = new TreeSet <String> (set_aaa);
for (Object key_aa: ss)
{
String key = (String)key_aa;
dict_unit = dict_aa.get (key_aa);
System.out.print (key_aa + "\t");
System.out.print (dict_unit.get ("name") + "\t");
Object ppx = dict_unit.get ("population");
System.out.print (ppx + "\t");
System.out.println (dict_unit.get ("date_mod"));
}
}
// -----------------------------------------------------------------------
}
// -----------------------------------------------------------------------
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_read.class: json_read.java
javac json_read.java
clean:
rm -f *.class
compile
make
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_read
Recommended Posts