0
我是新來的C#和有問題下面就行了Addresses address = (Addresses)serializer.ReadObject(e.Result);
給InvalidCastException的:WP7反序列化JSON
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對象。任何幫助將不勝感激。
謝謝,它仍然在該行上拋出InvalidCastException錯誤。我想我已經跟蹤到 - ''(e.Result).ReadTimeout'拋出了一個'System.InvalidOperationException'類型的異常。「」此流上不支持超時。「}但不知道如何解決這個問題 –
我認爲你必須將序列化程序的初始化更改爲一個列表! –
非常感謝! - 更改行讀取'var serializer = new DataContractJsonSerializer(typeof(List));'現在它解析正確。 什麼是最好的方式來獲得這個數組到列表選擇器的格式 'addressList.line1 addressList.line2 addressList.city addressList.state addressList.rpostcode' –