我是 c# 的新手,并且在下面给出 InvalidCastException 时遇到问题Addresses address = (Addresses)serializer.ReadObject(e.Result);
:
namespace My_App
{
[DataContract]
public class Addresses
{
[DataMember(Name = "line1")]
public string line1
{
get;
set;
}
[DataMember(Name = "line2")]
public string line2
{
get;
set;
}
[DataMember(Name = "postcode")]
public string rpostcode
{
get;
set;
}
[DataMember(Name = "city")]
public string city
{
get;
set;
}
[DataMember(Name = "state")]
public string state
{
get;
set;
}
}
public partial class sim : PhoneApplicationPage
{
public sim()
{
InitializeComponent();
}
private void Button_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
var webClient = new WebClient();
webClient.OpenReadCompleted += OnOpenReadCompleted;
webClient.OpenReadAsync(new Uri("http://www.myurl" UriKind.Absolute));
}
}
private void OnOpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
var serializer = new DataContractJsonSerializer(typeof(Addresses));
Addresses address = (Addresses)serializer.ReadObject(e.Result);
}
}
}
JSON:
[
{
"@type": "accountAddress",
"line1": " 1",
"line2": "NORWICH ROAD",
"postcode": "NR1 1AU",
"city": "NORWICH",
"state": "NORFOLK"
},
{
"@type": "accountAddress",
"line1": " 2",
"line2": "NORWICH ROAD",
"postcode": "NR1 1AU",
"city": "NORWICH",
"state": "NORFOLK"
},
{
"@type": "accountAddress",
"line1": " 3",
"line2": "NORWICH ROAD",
"postcode": "NR1 1AU",
"city": "NORWICH",
"state": "NORFOLK"
}
]
我需要它采用合适的格式才能在 listpicker 对象中使用。任何帮助将不胜感激。