Skip to content

Instantly share code, notes, and snippets.

@springaki
Created July 10, 2012 01:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save springaki/3080408 to your computer and use it in GitHub Desktop.
Save springaki/3080408 to your computer and use it in GitHub Desktop.
JSON deserialize test (DataContractJsonSerializer)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
namespace ConsoleApplication
{
[DataContract]
public class Cell
{
public Cell(
string row,
string col)
{
Row = int.Parse(row);
Col = int.Parse(col);
}
[DataMember]
public int Row { get; set; }
[DataMember]
public int Col { get; set; }
}
class Program
{
static void Main(string[] args)
{
// JSONデータ
var json = @"[{""Row"":""1"",""Col"":""3""},{""Row"":""1"",""Col"":""11""}]";
// シリアライズしてくれる人
var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(List<Cell>));
// String->Bytes->MemoryStreamに変換
var bytes= System.Text.Encoding.Unicode.GetBytes(json);
var stream = new System.IO.MemoryStream(bytes);
// CSharp オブジェクトに変換
var obj = (List<Cell>)serializer.ReadObject(stream);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment