Read JSON in Java

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

Read JSON in Java
POST JSON in Java
Create JSON in Java
Read binary files in Java 1
Read standard input in Java
Read binary files in Java 2
POST Json in Java ~ HttpURLConnection ~
Json serialization / deserialization in Java 1.4
Easily read text files in Java (Java 11 & Java 7)
Read Java properties file in C #
Read CSV in Java (Super CSV Annotation)
Partization in Java
Changes in Java 11
Rock-paper-scissors in Java
Pi in Java
FizzBuzz in Java
Try using JSON format API in Java
Read Felica using RC-S380 (PaSoRi) in Java
Read xlsx file in Java with Selenium
Working with huge JSON in Java Lambda
Interpreter implementation in Java
Make Blackjack in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
Read a string in a PDF file with Java
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
Read Java Property file
Callable Interface in Java
Comments in Java source
Code to escape a JSON string in Java
Azure functions in java
Parse Json in Scala
Format XML in Java
Simple htmlspecialchars in Java
Boyer-Moore implementation in Java
Read Java HashMap source
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Add, read, and delete Excel comments in Java
Ping commands in Java
Various threads in java
[Java] Read the file in src / main / resources
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
Express failure in Java
Date manipulation in Java 8
What's new in Java 8
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)
Read the packet capture obtained by tcpdump in Java
Try using RocksDB in Java