3

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

{"responseCode":"0","re​​sponseObject":{"TotalRecords":25,"TotalDisplayRecords":25,"aaData":[{"InvoiceId":16573,"somedata..},"appCrmAccount(一些标题,总共 100 个这样的标题) amount":40086.00,"invoiceNumber":"12,accountName":"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

请帮助..在此先感谢。

4

3 回答 3

1

试试这个

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;
}
于 2012-04-25T05:53:05.313 回答
0

您应该标记所有类和属性 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
    }
于 2012-04-25T07:58:29.630 回答
0

您究竟需要做的是将数组元素返回作为 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);  

它...

于 2012-05-12T13:39:01.673 回答