From Evernote: |
讀取Json檔案到Object |
需要using的Path
using System. IO ;
using System. Xml ;
using System. Xml .Linq ;
using System. Runtime .Serialization . Json;
Test.json:
{"First":true,"AddDrop":false,"Abandon":false}
主要的程式碼:
string path = "../Test.json";
byte [] buffer = System . IO. File .ReadAllBytes (path);
using ( XmlReader reader = JsonReaderWriterFactory .CreateJsonReader ( buffer, XmlDictionaryReaderQuotas . Max))
{
using ( XmlDictionaryReader xml = XmlDictionaryReader. CreateDictionaryReader ( reader))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer ( typeof( CourseSelectJsonModel )); //重點在這行
CourseSelectJsonModel courseSelectJsonModel = serializer .ReadObject ( xml);
}
}
Model:
public class CourseSelectJsonModel
{
public bool First { get ; set ; }
public bool AddDrop { get ; set ; }
public bool Abandon { get ; set ; }
}
沒有留言:
張貼留言