Java で JSON の Read

Jackson を使って　次のJSONファイルを読みます。

```json:tochigi.json
{
  "t2381": {
    "name": "名古屋",
    "population": "74125",
    "date_mod": "2009-1-7"
  },
  "t0922": {
    "name": "小山",
    "population": "17982",
    "date_mod": "2009-5-19"
  },
  "t0923": {
    "name": "佐野",
    "population": "46819",
    "date_mod": "2009-3-28"
  }
}
```

## Jackson  の jar ファイルのダウンロード

```bash
SRC="https://repo1.maven.org/maven2/com/fasterxml/jackson/core"
#
wget $SRC/jackson-annotations/2.14.2/jackson-annotations-2.14.2.jar
wget $SRC/jackson-core/2.14.2/jackson-core-2.14.2.jar
wget $SRC/jackson-databind/2.14.2/jackson-databind-2.14.2.jar
#
```

## プログラム

```java:json_read.java
// -----------------------------------------------------------------------
/*
	json_read.java


							Feb/10/2023
*/
// -----------------------------------------------------------------------
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 ("*** 開始 ***");
		String file_in = args[0];
		TypeReference<HashMap <String, HashMap<String,String>>> reference
					= new TypeReference<HashMap <String, HashMap<String,String>>> (){};
		  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 ("*** 開始 ***");
		    }
}


// ----------------------------------------------------------------------
```

```java: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;
}

// -----------------------------------------------------------------------
}
// ----------------------------------------------------------------------
```

```java: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"));
                }
}

// -----------------------------------------------------------------------
}
// -----------------------------------------------------------------------
```

```text: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_read.class: json_read.java
        javac json_read.java
clean:
        rm -f *.class
```

## コンパイル

```bash
make
```

## 実行

```bash
#
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_read tochigi.json
```

## 確認したバージョン

```text
$ 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 の Create](https://qiita.com/ekzemplaro/items/becd27848e0f1563a653)