我是ASP.NET世界中的新成員,我正試圖實現以下功能。在ASP.NET中序列化JSON
我需要序列化格式
{
"results": {
"profile": {
"firstname": "John,
"lastname": "Newman",
},
"credit": {
"amount": 30
}
}
}
的API的問題是,我不知道怎麼我的數據模型。我需要一個結果對象,其中包含2個其他對象(配置文件和信用)。您可以在下面看到一些示例代碼。 。?
public class Results
{
public class Data {
public Profile profile { get; set; }
public Credit credit {get; set; }
}
public class Profile {
public String firstname {get; set; }
public String lastname { get; set; }
}
public class Credit {
public int amount { get; set; }
}
}
static void Main(string[] args)
{
Results results= new Results
{
Data = new Data{
Profile = new Profile {
firstname = "John",
lastname = "Newman"
},
Credit = new Credit {
balance = "30"
}
}
};
string json = JsonConvert.SerializeObject(results);
Console.WriteLine(json);
}
我得到的錯誤是「成員數據不能被初始化它不是一個字段或屬性有什麼我做錯了
偉大的:-)它正是我想要的!非常感謝 – mgiota
歡迎您,不要忘記接受這個答案 –