「[[マイクロソフト系技術情報 Wiki>http://techinfoofmicrosofttech.osscons.jp/]]」は、「[[Open棟梁Project>https://github.com/OpenTouryoProject/]]」,「[[OSSコンソーシアム .NET開発基盤部会>https://www.osscons.jp/dotNetDevelopmentInfrastructure/]]」によって運営されています。 -[[戻る>JSON]] * 目次 [#pe665aa8] #contents *概要 [#o65cd171] とある案件の要件で、 >非構造化データを粘土細工のように~ ゴリゴリと処理したいが、そういったことは、可能なのだろうか? ということを調査した。 結論としては、[[コチラ>#ud85b2ed]]のLINQ to JSONを使用することで可能。 *JSON フォーマットとクラスの定義 [#vf3529ba] **JSON フォーマットの確認 [#dbc5c33d] まず、どのようなフォーマットの JSON を出力したいのかを確認する。 **クラスの定義 [#e10b9cbb] 次に、出力したい JSON に合わせて、クラスを定義する。 ***基本 [#i16a4292] 基本的には、以下のルールに従って、クラス・プロパティを定義する。 -JSON オブジェクト ({ キー名 : 値 }) の場合 --「キー名」を名前に持つプロパティを定義する -JSON 配列 ([ 値1, 値2, ... ]) の場合 --リストまたは配列のプロパティを定義する -キー名が不定の場合 --キー名が不定 (実行時に決まる) の場合、事前のクラス/プロパティ定義ができない。~ --このようなときは、Dictionary<TKey, TValue> を、Dictionary<string, object> として定義する。 ***ルール [#t6d4fac1] 基本的に、以下のようにJSON出力を想定し、クラスを定義する。 |#|出力したいJSON|定義するクラス・プロパティ|h |1|{ "key1" : "value1", "key2" : "value2" }|public class Sample&br;{&br; ''string key1'' { get; set; }&br; ''string key2'' { get; set; }&br;}&br;&br;若しくは、&br;''Dictionary<string, string>'' dic;| |2|[ "1", "2", "3" ]|''List<string>'' listData { get; set; }| |3|{ "key1" : [ "1", "2", "3" ] }|public class Sample&br;{&br; ''List<string> key1'' { get; set; }&br;}| |4|{ "key" : { "key1" : "value1", "key2" : "value2" } }|public class Sample&br;{&br; ''string key1'' { get; set; }&br; ''string key2'' { get; set; }&br;}&br;public class Sample2&br;{&br; ''Sample key'' { get; set; }&br;}&br;&br;若しくは、&br;''Dictionary<string, Sample>'' dic;&br;&br;若しくは、&br;''Dictionary<string, Dictionary<string, string>>'' dic;| |5|[ { "key1" : "value1", "key2" : "value2" }, &br;{ "key1" : "value3", "key2" : "value4" } ]|public class Sample&br;{&br; ''string key1'' { get; set; }&br; ''string key2'' { get; set; }&br;}&br;&br;''List<Sample>'' list;| ただし、DictionaryをDataContractJsonSerializerでシリアライズ・デシリアライズする場合、[[下記の様に>#w5b8ea38]]、相互運用性に問題があるので注意する。 ***Web Essentialsを使用 [#y3ecc544] Visual Studio の拡張機能である、Web Essentials を使うと、JSON からクラスを自動生成できる。~ Web Essentials は、Visual Studio のメニューから、[ツール] - [拡張機能と更新プログラム] で追加できる。~ &ref(WebEssentials.png,nolink,80%); JSON 文字列をコピーし、Visual Studio のエディターで、右クリックまたは [編集] メニューから、~ [形式を選択して貼り付け] - [JSON をクラスとして貼り付ける] を選択すると、~ その JSON フォーマットに合ったクラスが生成される。 *JSON.NET [#tf2395f2] ココが参考になる。 -C#でJSONを扱うライブラリ「Json.NET」を使ってみました - Qiita~ http://qiita.com/ta-yamaoka/items/a7ff1d9651310ade4e76 **基本(Bean, POCO) [#tf0108ab] ***シリアライズ・デシリアライズ [#df8d3cd2] [JsonObject("aaa")] public class AAA { [JsonProperty("prop1")] public int Property1 { get; set; } [JsonProperty("prop2")] public string Property2 { get; set; } } -シリアライズ --コード AAA aaa = new AAA(); aaa.Property1 = 100; aaa.Property2 = "xxx"; string json = JsonConvert.SerializeObject(aaa); --文字列 {"prop1":100,"prop2":"xxx"} -デシリアライズ --型を指定してデシリアライズ ---コード~ AAA型にデシリアライズされる。 AAA aaa = JsonConvert.DeserializeObject<AAA>(json); --型を指定せずにデシリアライズ ---コード~ JObject型にデシリアライズされる。 JObject jobj = (JObject)JsonConvert.DeserializeObject(json); -参考 --JsonConvert.SerializeObject Method~ http://www.newtonsoft.com/json/help/html/Overload_Newtonsoft_Json_JsonConvert_SerializeObject.htm --JsonConvert.DeserializeObject Method~ http://www.newtonsoft.com/json/help/html/Overload_Newtonsoft_Json_JsonConvert_DeserializeObject.htm ***プロパティの既定値 [#adf4b3f4] -プロパティの既定値の設定方法~ プロパティの属性にSystem.ComponentModel.DefaultValue属性を指定する。 [JsonObject("aaa")] public class AAA { [JsonProperty("prop1")] public int Property1 { get; set; } [JsonProperty("prop2")] [DefaultValue("hogehoge")] public string Property2 { get; set; } } -シリアライズ(デシリアライズ時)の既定値の取り扱い~ DefaultValueHandling 列挙体でシリアライズ(デシリアライズ時)の既定値の取り扱い方法を制御できる。 --DefaultValueHandling |#|設定値|動作|h |1|Include|シリアライズ時に既定値の項目をJSONに含める(既定)。| |2|Ignore|シリアライズ時に既定値の項目をJSONに含めない。| |3|Populate|デシリアライズ時にJSON文字列中に要素が存在しない場合でも既定値を設定。| |4|IgnoreAndPopulate|IgnoreとPopulateの同時指定。| --指定方法 ---プロパティの属性として指定 [JsonProperty("prop2", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)] [DefaultValue("hogehoge")] public string Property2 { get; set; } ---シリアライズ時に指定 string json = JsonConvert.SerializeObject(aaa, Formatting.Indented, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate }); --デシリアライズ時に指定 AAA aaa = JsonConvert.DeserializeObject<UserModel>(jsonstring, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate }); **応用 [#j19bc906] 上記のリンク先の記事に >「階層構造を持ったJSONでも問題なくデシリアライズ可能。~ JSON配列もListなどにパースしてくれる。」 とあるが、何処までやってくれるか? ・・・検証の結果、 -型が特定できる構造のJSONはシリアライズできる。 -[[型が特定できない構造のJSON>#ud85b2ed]]は、シリアライズは可能だが、~ デシリアライズ結果は(インデクサが使用可能な)JObjectに格納されるので、~ これをそのまま使用するか、[[手動で任意の型にデシリアライズする>#vd7fdcd8]]必要がある。 ということが解った。 以下は、型が特定できる構造のJSONをparseする例。 ***階層構造を持ったBean, POCOを使用する [#q9cc3f69] -型を明示 [JsonObject("aaa")] public class AAA { [JsonProperty("prop1")] public int Property1 { get; set; } [JsonProperty("prop2")] public string Property2 { get; set; } [JsonProperty("prop3")] public AAA Property3 { get; set; } } static void Main(string[] args) { AAA aaa = new AAA(); aaa.Property1 = 100; aaa.Property2 = "xxx"; aaa.Property3 = new AAA(); aaa.Property3.Property1 = 200; aaa.Property3.Property2 = "yyy"; string json = JsonConvert.SerializeObject(aaa); Console.WriteLine(json); aaa = JsonConvert.DeserializeObject<AAA>(json); -型を明示しない~ オブジェクト型を使用する(JObject型にデシリアライズされる)。 [JsonObject("aaa")] public class AAA { [JsonProperty("prop1")] public int Property1 { get; set; } [JsonProperty("prop2")] public string Property2 { get; set; } [JsonProperty("prop3")] public object Property3 { get; set; } } static void Main(string[] args) { AAA aaa1 = new AAA(); AAA aaa2 = new AAA(); aaa1.Property1 = 100; aaa1.Property2 = "xxx"; aaa2.Property1 = 200; aaa2.Property2 = "yyy"; aaa1.Property3 = aaa2; string json = JsonConvert.SerializeObject(aaa1); Console.WriteLine(json); JObject jobj= (JObject)JsonConvert.DeserializeObject(json); ***配列(List<AAA>)を使用する [#c749f4e3] 配列(Generic)も問題なく処理できる。 [JsonObject("aaa")] public class AAA { [JsonProperty("prop1")] public int Property1 { get; set; } [JsonProperty("prop2")] public string Property2 { get; set; } [JsonProperty("prop3")] public AAA Property3 { get; set; } } static void Main(string[] args) { AAA aaa1 = new AAA(); AAA aaa2 = new AAA(); aaa1.Property1 = 100; aaa1.Property2 = "xxx"; aaa2.Property1 = 200; aaa2.Property2 = "yyy"; aaa1.Property3 = aaa2; List<AAA> lstaaa = new List<AAA>(); lstaaa.Add(aaa1); lstaaa.Add(aaa2); string json = JsonConvert.SerializeObject(lstaaa); Console.WriteLine(json); lstaaa = JsonConvert.DeserializeObject<List<AAA>>(json); ***Primitive型とGeneric型を使用する [#p067db84] -Primitive型とGeneric型の範囲であれば、JSONの型が特定できるので、parseできる。 -このため、Primitive型とGeneric型の範囲で自由な構造を組むことができる。 -しかし、Genericでは、Bean, POCOを使用するため(フィールド名・フィールド型の組み合わせが)自由な構造を組めない。 static void Main(string[] args) { List<Dictionary<string, string>> parent = new List<Dictionary<string, string>>(); Dictionary<string, string> child; child = new Dictionary<string, string>(); child["aaa"] = "AAA"; child["bbb"] = "BBB"; child["ccc"] = "CCC"; parent.Add(child); child = new Dictionary<string, string>(); child["xxx"] = "XXX"; child["yyy"] = "YYY"; child["zzz"] = "ZZZ"; parent.Add(child); string json = JsonConvert.SerializeObject(parent); Console.WriteLine(json); parent = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(json); ***継承を使用する [#q73512eb] -派生型をシリアライズできるが、問題は派生型にデシリアライズできないこと。 -バイナリ・シリアライズではこういうことができたが、それは型情報を持たせているから。 -型情報を持たないJSONのシリアライズでは、こういうことはできない。 -DataContractJsonSerializerでは型情報の出力などもサポートされていたが、~ 昨今の相互運用性が重視されるJSON利用方法を考えると、もう使われない機能になった。 [JsonObject("aaa")] public class AAA { [JsonProperty("prop1")] public int Property1 { get; set; } [JsonProperty("prop2")] public string Property2 { get; set; } [JsonProperty("prop3")] public AAA Property3 { get; set; } } [JsonObject("bbb")] public class BBB : AAA { [JsonProperty("prop4")] public int Property4 { get; set; } } [JsonObject("ccc")] public class CCC : AAA { [JsonProperty("prop4")] public string Property4 { get; set; } } static void Main(string[] args) { AAA aaa1 = new AAA(); AAA aaa2 = new AAA(); aaa1.Property1 = 100; aaa1.Property2 = "xxx"; aaa1.Property3 = new BBB(); // AAAの派生のBBB aaa2.Property1 = 200; aaa2.Property2 = "yyy"; aaa2.Property3 = new CCC(); // AAAの派生のCCC List<AAA> lstaaa = new List<AAA>(); lstaaa.Add(aaa1); lstaaa.Add(aaa2); string json = JsonConvert.SerializeObject(lstaaa); Console.WriteLine(json); // BBBとCCCがAAA型のProperty3フィールドにデシリアライズされない。 lstaaa = JsonConvert.DeserializeObject<List<AAA>>(json); **型が特定できない構造のJSON [#ud85b2ed] ***型が特定できない構造のJSONの例 [#sdf7fbe0] シリアライズの対象にobject型を使用しており、 シリアライズの際、 -任意の型が指定される。 -出力されるされるJSONの構造が一定ではない。 ***object型を使用せざるを得ないケース [#ea76ada5] 以下の様な構造が一定ではないケースでは、~ シリアライズの対象にobject型を使用せざるを得ない。 -子要素JSONの構造が一定ではない(Fieldにobject型を使用する) -配列内のJSONの構造が一定ではない(object[], Hashtableを使用する) ***LINQ to JSONで手動のデシリアライズ [#vd7fdcd8] 型が特定できない構造の(object型をシリアライズした)JSONを~ デシリアライズするとJSONデータはJObject型に格納される。 この場合、LINQ to JSONでJObject型の手動のデシリアライズを行う。 -C# + LINQ でJSONのパースをする - Qiita~ http://qiita.com/numa08/items/475ab3e8aa9e10441a26 -Json.NET (Newtonsoft.Json) の基本的な使い方 | ITメモ~ https://netweblog.wordpress.com/2016/10/24/json-net-newtonsoft-json-usage/ -LINQ to JSON~ http://www.newtonsoft.com/json/help/html/LINQtoJSON.htm --Querying JSON with LINQ~ http://www.newtonsoft.com/json/help/html/QueryingLINQtoJSON.htm LINQ to JSONといっても、単純にインデクサで処理できる模様。 ***型が特定できない構造のJSONをparseする例 [#eadea96f] -memo: C#でJSON配列のパースに苦労した話~ https://kazyx.blogspot.com/2013/09/cjson.html -JSON.NETで変なJSONを読み込む方法 - かずきのBlog@hatena~ http://blog.okazuki.jp/entry/2014/04/19/235806 ***参考 [#w963879c] -Json.NET - Newtonsoft~ http://www.newtonsoft.com/json *DataContractJsonSerializer [#t365ab51] [[WCF]]の既定のシリアライザ。 流行ってないので基本スルーだが、以下の点に注意する。 **JavaやJSON.NETのシリアライザとの違い。 [#w5b8ea38] .NETのDataContractJsonSerializerはJavaやJSON.NETと、シリアライザの挙動が違う。~ そのため、同じ形式の POJO または POCO であっても、シリアライズ後の JSON フォーマットが異なる。 ***Dictionaryのシリアライズ結果 [#oe9db4c8] 特に、以下のように Dictionaryの JSON シリアライズ結果が異なる。 |#|技術|POJO / POCO サンプル|シリアライズされた JSON|h |1|Java, JSON.NET|public class Sample {&br(); public String _key1;&br(); public String _key2;&br(); public void setKey1(String key1) {&br(); this._key1 = key1;&br(); }&br(); public String getKey1() {&br(); return this._key1;&br(); }&br(); public void setKey2(String key2) {&br(); this._key2 = key2;&br(); }&br(); public String getKey2() {&br(); return this._key2;&br(); }&br();}|{ "key1" : "value1", "key2" : "value2" }| |2|~|public class Sample {&br(); public HashMap<String, String> _key;&br(); public void setKey(HashMap<String, String> key) {&br(); this._key = key;&br(); }&br(); public void getKey() {&br(); return this._key;&br(); }&br();}|~| |3|.NETのDataContractJsonSerializer|public class Sample {&br(); public string key1 { get; set; }&br(); public string key2 { get; set; }&br();}|~| |4|~|public class Sample {&br(); public Dictionary<string, string> key { get; set; }&br();}|[&br(); {&br(); "Key": "Prop1", &br(); "Value": "Value1"&br(); }, &br(); {&br(); "Key": "Prop2", &br(); "Value": "Value2"&br(); }&br();]&br();&br();''同じ形式のジェネリック型であっても、&br();使用するSerializer次第でシリアライズ結果が異なる''| ***Efficient dictionary serialization, Nonsensical dictionary serialization [#q3d85516] この違いは、以下の機能のサポート状況によるものらしい。 -Efficient dictionary serialization~ http://stackoverflow.com/questions/1207731/how-can-i-deserialize-json-to-a-simple-dictionarystring-string-in-asp-net -Nonsensical dictionary serialization~ http://stackoverflow.com/questions/4559991/any-way-to-make-datacontractjsonserializer-serialize-dictionaries-properly -参考 --Json.NET - Home~ http://json.codeplex.com/ --Json.NET - Newtonsoft~ http://www.newtonsoft.com/json *JSON.stringify、JSON.parse [#e56c0f75] JavaScriptでは、JSON.stringify(シリアライズ)、JSON.parse(デシリアライズ)が使用できる。 **JSON.parse [#a6e99c68] JSON 文字列を解析して JavaScript のオブジェクトに変換する。 **JSON.stringify [#t99dd427] JavaScript の値を JSON 文字列に変換する。 **参考 [#w271bb4b] -【JavaScript】JSONのparseとstringifyメソッドの使い方 - TASK NOTES~ http://www.task-notes.com/entry/20160719/1468858991 -JSON.stringifyを改めて調べる。 - Qiita~ http://qiita.com/qoAop/items/57d35a41ef9629351c3c ---- Tags: [[:.NET開発]], [[:.NET Core]], [[:ASP.NET]], [[:ASP.NET Web API]]