Created
July 10, 2012 01:21
-
-
Save springaki/3080408 to your computer and use it in GitHub Desktop.
JSON deserialize test (DataContractJsonSerializer)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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