我创建了一个Web服务应用程序的Windows Phone 7,这是来自敬爱URI JSON数组获得。 ...[{"id":4,"name":"Bangalore"},{"id":1,"name":"Chennai"},{"id":3,"name":"Hyderabad"},{"id":2,"name":"Mumbai"}]...
列表项=(名单)ds.ReadObject(msnew); 在这一行一个错误(它说,同时运行)。 有一个错误在反序列化根级别type.Data的对象是无效的。行1,位置1。
编码:
MainPage的公() { 的InitializeComponent(); }
[DataContract]
公共类项目
{
[数据成员]
公众诠释ID
{
得到;
组;
}
[数据成员]
公共字符串名称
{
得到;
组;
}
}
私人无效PhoneApplicationPage_Loaded(对象发件人,RoutedEventArgs E)
{
Web客户端WC =新的Web客户端();
wc.DownloadStringAsync(新的URI(http://75.101.161.83:8080/CityGuide/Cities?authId=CITY4@$pir*$y$t*m$13GUID*5));
wc.DownloadStringCompleted + =新DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
}
无效wc_DownloadStringCompleted(对象发件人,DownloadStringCompletedEventArgs E)
{
字符串MyJsonString = e.Result;
//的MessageBox.show(e.Result);
DataContractSerializer的DS =新的DataContractSerializer(typeof运算(项目));
MemoryStream的msnew =新的MemoryStream(Encoding.UTF8.GetBytes(MyJsonString));
名单<项目>项目=(名单<项目>)ds.ReadObject(msnew);
}
解决方案
有在你想要做什么,2失误。
您使用的是的DataContractSerializer
而不是 DataContractJsonSerializer
。你要使用的人期待XML,JSON不是。
您正在试图反序列化到一个项目
,然后将其转换为名单,其中;项目>
,而不是一个数组,这是JSON包含的内容。
试试这个来代替:
变种DS =新DataContractJsonSerializer(typeof运算(第[]));
VAR msnew =新的MemoryStream(Encoding.UTF8.GetBytes(MyJsonString));
项目[]项目=(项目[])ds.ReadObject(msnew);
如果你以后想,你可以在阵列转换到一个列表。
I created one web service application in windows phone 7. this is JSON array get from belowed uri. ...[{"id":4,"name":"Bangalore"},{"id":1,"name":"Chennai"},{"id":3,"name":"Hyderabad"},{"id":2,"name":"Mumbai"}]...
List item = (List)ds.ReadObject(msnew); In this line one bug(it says while run). There was an error deserializing the object of type.Data at the root level is invalid. Line 1, position 1.
coding:
public MainPage() { InitializeComponent(); }
[DataContract]
public class Item
{
[DataMember]
public int id
{
get;
set;
}
[DataMember]
public string name
{
get;
set;
}
}
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
WebClient wc = new WebClient();
wc.DownloadStringAsync(new Uri("http://75.101.161.83:8080/CityGuide/Cities?authId=CITY4@$pir*$y$t*m$13GUID*5"));
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
}
void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
string MyJsonString = e.Result;
// MessageBox.Show(e.Result);
DataContractSerializer ds = new DataContractSerializer(typeof(Item));
MemoryStream msnew = new MemoryStream(Encoding.UTF8.GetBytes(MyJsonString));
List<Item> item = (List<Item>)ds.ReadObject(msnew);
}
解决方案
There are 2 mistakes in what you're trying to do.
You're using DataContractSerializer
instead of DataContractJsonSerializer
. The one you're trying to use is expecting XML, not JSON.
You're trying to deserialize to a single Item
and then convert that to a List<Item>
, rather than an array, which is what json contains.
Try this instead:
var ds = new DataContractJsonSerializer(typeof(Item[]));
var msnew = new MemoryStream(Encoding.UTF8.GetBytes(MyJsonString));
Item[] items = (Item[])ds.ReadObject(msnew);
If you later wanted to, you could convert the array to a list.