反序列化 JSON 数组以列出 WP7

本文关键字:WP7 JSON 数组 反序列化 | 更新日期: 2023-09-27 18:32:14

>我需要从 JSON 内部的子数组中获取数据,但它没有转换为列表,下面是我的 JSON 字符串

{"responseCode":"0","responseObject":{"TotalRecords":25,"

TotalDisplayRecords":25,"aaData":[{"InvoiceId":16573,"somedata..}," appCrmAccount(一些标题,总共 100 个这样的标题)金额":40086.00,"发票编号":"12,帐户名称":"dfgAsfsadf","dueDateStr":"04/24/2012"(数据待进入列表)

这是我的代码:

var djson = new DataContractJsonSerializer(typeof(dataList));
var stream = new MemoryStream(Encoding.UTF8.GetBytes(json));
dataList result = (dataList)djson.ReadObject(stream);//not getting execute

请帮忙..提前谢谢。

反序列化 JSON 数组以列出 WP7

试试这个

private void btnAdd_Click(object sender, RoutedEventArgs e)
{
    WebClient proxy = new WebClient();
    proxy.DownloadStringCompleted += new DownloadStringCompletedEventHandler(proxy_DownloadStringCompleted);
    proxy.DownloadStringAsync(new Uri(""));
}

并且需要解析返回的 JSON,如下所示。在创建DataContractJsonSrrializer实例的参数中,我们正在传递学生列表。

void proxy_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    Stream stream = new MemoryStream(Encoding.Unicode.GetBytes(e.Result));
    DataContractJsonSerializer obj = new DataContractJsonSerializer(typeof(List<Student>));
    List<Student> result = obj.ReadObject(stream) as List<Student>;
    lstStudents.ItemsSource = result;
}

应标记所有类和属性 DataContract 和 DataMember 属性。使用你的代码片段,我创建了这样的东西:

[DataContract]
    public class Result
    {
        [DataMember(Name="responseCode")]
        public int Code { get; set; }
        [DataMember(Name="responseObject")]
        public ResponseObject Result { get; set; }
    }
    [DataContract]
    public class ResponseObject
    {
        [DataMember]
        public int TotalRecords { get; set; }
        [DataMember]
        public int TotalDisplayRecords { get; set; }
        [DataMember(Name="aaData")]
        public DataItem[] Data { get; set; }
    }
    [DataContract]
    public class DataItem
    {
        [DataMember(Name = "InvoiceId")]
        public int InvoiceId { get; set; }
        // Others properties
    }

您真正需要做的是将数组元素返回作为DataContract,将其子成员作为DataMember作为

[DataContract] 
public class mainresponse
 {
 [DataMember]
 public resultmap arrayelement { get; set; }
 }  
 [DataContract]
 public class resultmap 
{
 [DataMember] 
 public string substringhere { get; set; } 
 }     
 var djson = new DataContractJsonSerializer(typeof(Mainresponse));
 var stream = new MemoryStream(Encoding.UTF8.GetBytes(responsestring));
 mainresponse result = (mainresponse)djson.ReadObject(stream);  

它...