3

json内のサブ配列からデータを取得する必要がありますが、リストに変換されません。以下はjson文字列です

{"responseCode":"0","re​​sponseObject":{"TotalRecords":25,"TotalDisplayRecords":25,"aaData":[{"InvoiceId":16573,"somedata..}," appCrmAccount(some title,合計 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 のインスタンスを作成するパラメータで、Student の List を渡します。

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 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 に答える
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 に答える