0
如何讀取我的json對象。我有以下的JSON JSON文件用C讀取Json文件#
{
"DBEnvironment": [
{
"dbID": 1,
"dbName": "Develop"
},
{
"dbID": 2,
"dbName": "Test"
},
{
"dbID": 3,
"dbName": "Production"
}
]
}
我有一個叫DBEnvironment作爲類遵循
class DBEnvironment : INotifyPropertyChanged
{
#region Data
private int _dbID;
private string _dbName;
#endregion
#region Contructor
public DBEnvironment(int id, string name)
{
_dbID = id;
_dbName = name;
}
#endregion
public int dbId
{
get { return _dbID; }
set
{
_dbID = value;
OnPropertyChanged("ID changed");
}
}
public string dbName
{
get { return _dbName; }
set
{
_dbName = value;
OnPropertyChanged("Database Name");
}
}
}
我閱讀下列代碼
string json = File.ReadAllText(path);
DBEnvironment dblist = JsonConvert.DeserializeObject<DBEnvironment>(json);
MessageBox.Show(dblist.dbName);
文件,但DBLIST是空的。如何解決這個問題?
這就是當我做你的代碼時得到的錯誤。其他信息:將值「DBEnvironment」轉換爲鍵入「Baja.Model.DBEnvironment []」時出錯。路徑'',第1行,第19位。 – Jseb
您需要一個空白的構造函數。看看上面的變化。 – Laith