こちらで作成した JSON を読みます。
.Net Core で JSON の Create
/var/tmp/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"},"t0924":{"name":"足利","population":"65297","date_mod":"2009-12-21"},"t0925":{"name":"日光","population":"14926","date_mod":"2009-11-25"},"t0926":{"name":"下野","population":"28145","date_mod":"2009-1-26"},"t0927":{"name":"さくら","population":"56784","date_mod":"2009-1-21"}}
Program.cs
usingSystem;usingSystem.IO;usingSystem.Collections.Generic;usingSystem.Text.Json;usingSystem.Text.Json.Serialization;namespaceRead01{classProgram{staticvoidMain(string[]args){Console.Error.WriteLine("*** start ***");stringfile_in="/var/tmp/json/tochigi.json";stringstr_json=file_io.file_to_str_proc(file_in);Dictionary<string,Object>dict_aa=JsonSerializer.Deserialize<Dictionary<string,Object>>(str_json);foreach(KeyValuePair<string,Object>kvindict_aa){stringjson_unit=JsonSerializer.Serialize(kv.Value);Dictionary<string,Object>unit_aa=JsonSerializer.Deserialize<Dictionary<string,Object>>(json_unit);stringstr_out=kv.Key+"\t";str_out+=unit_aa["name"]+"\t"+unit_aa["population"];str_out+="\t"+unit_aa["date_mod"];Console.WriteLine(str_out);}Console.Error.WriteLine("*** end ***");}}}
file_io.cs
// --------------------------------------------------------------------/*
file_io.cs
Jan/09/2020
*/// --------------------------------------------------------------------usingSystem;usingSystem.IO;usingSystem.Text;// --------------------------------------------------------------------publicclassfile_io{// --------------------------------------------------------------------publicstaticstringfile_to_str_proc(stringfile_in){StreamReaderfp_in=newStreamReader(file_in);stringbuff;StringBuilderstb=newStringBuilder();while((buff=fp_in.ReadLine())!=null){stb.Append(buff);}fp_in.Close();stringstr_in=stb.ToString();returnstr_in;}}// --------------------------------------------------------------------
コンパイル
$ dotnet build
Microsoft (R) Build Engine version 16.4.0+e901037fe for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
Restore completed in 46.28 ms for /home/uchida/dotnet/Read01/Read01.csproj.
Read01 -> /home/uchida/dotnet/Read01/bin/Debug/netcoreapp3.1/Read01.dll
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:01.46
実行
$ dotnet bin/Debug/netcoreapp3.1/Read01.dll
*** start ***
t2381 名古屋 74125 2009-1-7
t0922 小山 17982 2009-5-19
t0923 佐野 46819 2009-3-28
t0924 足利 65297 2009-12-21
t0925 日光 14926 2009-11-25
t0926 下野 28145 2009-1-26
t0927 さくら 56784 2009-1-21
*** end ***