Created
May 12, 2014 20:57
-
-
Save jamieshepherd/e012772f6a9eedc95091 to your computer and use it in GitHub Desktop.
json deserialize into object list
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
/* -- 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