我想问一下我的问题,我有html文本输入表,用户可以添加最多10行输入客户地址,我使用asp.net MVP模式设计应用程序只是想问问如何我可以将json对象加到属性方法中,并从json中提取地址,但是将它们提取到getter方法
json
[{"name":"street_01","value":"bfb"},{"name":"city_01","value":"xcbcx"},{"name":"province_01","value":"xcbxcb"},{"name":"postalCode_01","value":"xcbcxb"},{"name":"street_02","value":"xcbcxb"},{"name":"city_02","value":"xcbcxb"},{"name":"province_02","value":"xcbbxc"},{"name":"postalCode_02","value":"cxbbxccxb"}]
IAddresView
public interface IAddressView
{
int AdressId { get; set; }
int PersonId { get; set; }
string Street { get; set; }
string City { get; set; }
string Province { get; set; }
char PostalCode { get; set; }
}
personDetail.aspx.cs
public partial class PersonDetails : System.Web.UI.Page , IPersonView
{
protected void Page_Load(object sender, EventArgs e)
{
}
public int PersonId
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public string FirstName
{
get { return txtbFirstname.Text; }
set { txtbFirstname.Text = value; }
}
public string LastName
{
get { return txtbLastname.Text; }
set { txtbFirstname.Text = value; }
}
public string MiddleName
{
get
{
return txtbMiddleName.Text;
}
set { txtbMiddleName.Text = value; }
}
public DateTime DateOfBirth
{
get { return Convert.ToDateTime(txtbDateOfBirth.Text); }
set { txtbDateOfBirth.Text = value.ToShortDateString(); }
}
public char Gender
{
get { return Convert.ToChar(ddlGender.SelectedValue); }
set { ddlGender.SelectedValue = value.ToString(); }
}
public List<IAddressView> AddressesList
{
get
{
List<IAddressView> addresses = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<IAddressView>(json);
foreach (var item in addresses)
{
}
}
set
{
throw new NotImplementedException();
}
}
protected void btnAddPerson_Click(object sender, EventArgs e)
{
PersonPresenter personPresenter = new PersonPresenter(this);
if (personPresenter.AddPerson())
{
Response.Write("Add person");
}
else
{
Response.Write("error");
}
}
}
答案 0 :(得分:1)
使用nuget manager控制台运行install-package newtonsoft.json
var json = "[{\"name\":\"street_01\",\"value\":\"bfb\"},{\"name\":\"city_01\",\"value\":\"xcbcx\"},{\"name\":\"province_01\",\"value\":\"xcbxcb\"},{\"name\":\"postalCode_01\",\"value\":\"xcbcxb\"},{\"name\":\"street_02\",\"value\":\"xcbcxb\"},{\"name\":\"city_02\",\"value\":\"xcbcxb\"},{\"name\":\"province_02\",\"value\":\"xcbbxc\"},{\"name\":\"postalCode_02\",\"value\":\"cxbbxccxb\"}]";
var pd = JsonConvert.DeserializeObject<PersonDetails>(json);
祝你好运