我想使用 c# 在 asp.net 中解析这个 json 数组这是一个要解析的示例 Json 数据:
[
{
"idvisit":"3",
"iduser":"shoaibshakeel",
"idpage":"1",
"pagetime":"0"
},
{
"idvisit":"4",
"iduser":"shoaibshakeel",
"idpage":"2",
"pagetime":"28"
},
{
"idvisit":"5",
"iduser":"shoaibshakeel",
"idpage":"3",
"pagetime":"0"
}
]
这是我目前在我的代码文件中做的:
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string jsonData = Request.Form[0];
Response.Write(jsonData);
Response.Write("<br/>");
PiwikDbData[] visitlist;
//deserialize json array
using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonData)))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(PiwikDbData));
visitlist = serializer.ReadObject(stream) as PiwikDbData[];
}
Response.Write(visitlist.Length);
}
}
[DataContract]
class PiwikDbData
{
[DataMember(Name = "idvisit", IsRequired = true)]
public int idvisit;
[DataMember(Name = "iduser", IsRequired = true)]
public string iduser;
[DataMember(Name = "idpage", IsRequired = true)]
public int idpage;
[DataMember(Name = "pagetime", IsRequired = true)]
public int pageTime;
}
我在这里做错了什么。visitlist = serializer.ReadObject(stream) as PiwikDbData[];
非常感谢它在帮助下显示“根级别的数据无效” 。谢谢。