Skip to content

Instantly share code, notes, and snippets.

@jamieshepherd
Created May 12, 2014 20:57
Show Gist options
  • Save jamieshepherd/e012772f6a9eedc95091 to your computer and use it in GitHub Desktop.
Save jamieshepherd/e012772f6a9eedc95091 to your computer and use it in GitHub Desktop.
json deserialize into object list
/* -- EXAMPLE JSON
[
{"boxID":1,"boxName":"testname","boxPath":"D:\\Web\\jamie\\Vagrantfile","boxStatus":false},
{"boxID":2,"boxName":"anotherbox","boxPath":"D:\\Web\\jamie\\Vagrantfile","boxStatus":false}
]
*/
// My current method
public static void loadSettings()
{
var json = Properties.Settings.Default.Boxes;
var jsonSerialiser = new JavaScriptSerializer();
// Tried this which did not work, threw InvalidOperationException
// MainWindow.vagrantBoxList = jsonSerialiser.Deserialize<BoxList>(json);
}
// BoxList.cs
[Serializable()]
public class BoxList
{
public int id = 0;
public List<Box> list { get; set; }
public void AddBox(Box Box)
{
list.Add(Box);
Program.mainWindow.RefreshMenu();
}
public IEnumerator<Box> GetEnumerator()
{
return list.GetEnumerator();
}
}
// Box.cs
public class Box
{
public int boxID;
public String boxName, boxPath;
public Boolean boxStatus
{
get;
set;
}
public Box(int id, String name, String path, Boolean status)
{
boxID = id;
boxName = name;
boxPath = path;
boxStatus = status;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment