Open棟梁Project - マイクロソフト系技術情報 Wiki

目次

概要

とある案件の要件で、

非構造化データを粘土細工のように
ゴリゴリと処理したいが、そういったことは、可能なのだろうか?

ということを調査した。

結論としては、コチラのLINQ to JSONを使用することで可能。

基本

基本的には、ここに記載されたルールに従って、parseする。

JSON.NET

ココが参考になる。

基本(Bean, POCO)

シリアライズ・デシリアライズ

[JsonObject("aaa")]
public class AAA
{
  [JsonProperty("prop1")]
  public int Property1 { get; set; }
  [JsonProperty("prop2")]
  public string Property2  { get; set; }
}

プロパティの既定値

応用

上記のリンク先の記事に

「階層構造を持ったJSONでも問題なくデシリアライズ可能。
JSON配列もListなどにパースしてくれる。」

とあるが、何処までやってくれるか?

・・・検証の結果、

ということが解った。

以下は、型が特定できる構造のJSONをparseする例。

階層構造を持ったBean, POCOを使用する

配列(List<AAA>)を使用する

配列(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型を使用する

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);

継承を使用する

派生型をシリアライズできるが、問題は派生型にデシリアライズできないこと。

[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

型が特定できない構造のJSONの例

object型を使用しており、シリアライズの際、

object型を使用せざるを得ないケース

LINQ to JSONで手動のデシリアライズ

型が特定できない構造のJSONをデシリアライズするとJSONデータはJObject型に格納される。

この場合、LINQ to JSONでJObject型の手動のデシリアライズを行う。 LINQ to JSONといっても、単純にインデクサで処理できる模様。

型が特定できない構造のJSONをparseする例

参考

DataContractJsonSerializer?


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS