目的
テスト用に使えるjsonファイルは何が良いかなと思ったら、天気予報JSONファイルが使えるようなのでためしてみる
System.Text.Json はNet5、NetCore3.0, 3.1で今のところ使用可
※今のところの問題点
encoderSettings.AllowRange(UnicodeRanges.All);
UnicodeRanges.All 指定なのに '\u002B', '\u3000'(+と全角空白)がUTF8のコードで出力されてしまう
※今のところよくわかってない(検索しても見つからない?)
VS2019のFormで作成すると
Console.WriteLine:出力先が不明??
Debug.WriteLine:出力(デバック)に出力される
市町村コードについて
※区はどうなんだと突っ込みはさておき(都道府県コードというのだろうか?)
市町村コードは 13(県コード2桁) + 116(3桁コード)または 13(県コード2桁) + 1164(4桁コード)
【東日本】市町村コード(5桁・6桁)一覧(更新:2021.1.1)より
13116 :豊島区
131164:豊島区
問い合わせは 130000(県コード2桁+0000)の6桁の様だ
サンプルコード
気象庁の天気予報JSONファイルをWebAPI的に利用したサンプルアプリ
の一部をC#で修正しながら書き直してみた(つもり)
using System.Text.Json;
using System.Text.Unicode;
using System.Text.Encodings.Web;
//
// 天気概況(明後日まで)https://www.jma.go.jp/bosai/forecast/data/overview_forecast/130000.json
//
private void button1_Click(object sender, EventArgs e)
{
WebClient wc = new WebClient();
string JsonStr = wc.DownloadString(@"https://www.jma.go.jp/bosai/forecast/data/overview_forecast/130000.json");
jsonweather jsontex = JsonSerializer.Deserialize<jsonweather>(JsonStr);
//Debug.WriteLine(JsonStr); // 取込データ全てを表示
Debug.WriteLine("発表者");
Debug.WriteLine(" " + jsontex.publishingOffice);
Debug.WriteLine("報告日時");
Debug.WriteLine(" " + jsontex.reportDatetime);
Debug.WriteLine("対象地域");
Debug.WriteLine(" " + jsontex.targetArea);
Debug.WriteLine("ヘッドライン");
Debug.WriteLine(" " + jsontex.headlineText);
string delimiter = "\n\n";
string[] arry = jsontex.text.Split(delimiter);
Debug.WriteLine("概況");
Debug.WriteLine(arry[0]);
Debug.WriteLine("今日の天気");
Debug.WriteLine(arry[1]);
Debug.WriteLine("明日の天気");
Debug.WriteLine(arry[2]);
Debug.WriteLine("明後日の天気");
Debug.WriteLine(arry[3]);
// Serializeをやってみる
var encoderSettings = new TextEncoderSettings();
//
// TODO
// All 指定なのに '\u002B', '\u3000'
// +と全角空白がUTF8のコードで出力されてしまう
//
encoderSettings.AllowRange(UnicodeRanges.All);
var options = new JsonSerializerOptions
{
Encoder = JavaScriptEncoder.Create(encoderSettings),
WriteIndented = true
};
string SerializeStr = JsonSerializer.Serialize(jsontex, options);
Debug.WriteLine(SerializeStr);
}
//
// 天気概況(週間)https://www.jma.go.jp/bosai/forecast/data/overview_week/130000.json
//
private void button3_Click(object sender, EventArgs e)
{
WebClient wc = new WebClient();
string JsonStr = wc.DownloadString(@"https://www.jma.go.jp/bosai/forecast/data/overview_week/130000.json");
jsonweek jsonweek = JsonSerializer.Deserialize<jsonweek>(JsonStr);
//Debug.WriteLine(JsonStr);
Debug.WriteLine("発表者");
Debug.WriteLine(" " + jsonweek.publishingOffice);
Debug.WriteLine("報告日時");
Debug.WriteLine(" " + jsonweek.reportDatetime);
Debug.WriteLine("対象地域");
Debug.WriteLine(" " + jsonweek.headTitle);
Debug.WriteLine("天気概況(週間)");
Debug.WriteLine(" " + jsonweek.text);
}
// 天気概況(明後日まで)用のクラス
class jsonweather
{
public string publishingOffice { get; set; }
public string reportDatetime { get; set; }
public string targetArea { get; set; }
public string headlineText { get; set; }
public string text { get; set; }
}
// 天気概況(週間)用のクラス
class jsonweek
{
public string publishingOffice { get; set; }
public string reportDatetime { get; set; }
public string headTitle { get; set; }
public string text { get; set; }
}
参考にしたのは以下のサイト
気象庁の天気予報JSONファイルをWebAPI的に利用したサンプルアプリ
【超簡単】c#でJSONを読み書きする方法
System.Text.Json で文字エンコードをカスタマイズする方法
.NET 内で JSON のシリアル化と逆シリアル化 (マーシャリングとマーシャリングの解除) を行う方法
気象庁JSONデータ
気象庁|天気予報
都道府県コード〈 ファイル名称:PrefCode 〉
総務省|電子自治体|全国地方公共団体コード
【東日本】市町村コード(5桁・6桁)一覧(更新:2021.1.1)
【西日本】市町村コード(5桁・6桁)一覧(更新:2021.1.1)