3

i need to get data from sub-array inside json,but its not getting converted into list, below is my json string

{"responseCode":"0","responseObject":{"TotalRecords":25,"TotalDisplayRecords":25,"aaData":[{"InvoiceId":16573,"somedata..}," appCrmAccount(some title,total 100 such titles) amount":40086.00,"invoiceNumber":"12,accountName":"dfgAsfsadf"," dueDateStr":"04/24/2012"(data to be get into list)

here is my code:

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

kindly help.. Thanks in Advance.

2
  • What do you mean not getting execute? an error? exception? Commented Apr 25, 2012 at 5:10
  • Could you post code of class "dataList" here? Do you using DataContract and DataMember attributes? Commented Apr 25, 2012 at 7:52

3 Answers 3

1

Try this

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

And need to parse the returned JSON as below. In parameter to create instance of DataContractJsonSrrializer we are passing List of Student.

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

You should mark all classes and properties DataContract and DataMember attributes. Using you code snippet i created something like this:

[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
    }

3 Comments

Mark it as answer, pls, if it is.
I'm having a similar problem but this is not working for arrays... though I don't understand what is this: Name="aaData"?
Name="aaData" is just property for attribute that indicate name of field in source. See msdn.microsoft.com/en-us/library/…
0

what exactly you need to do is take array element return as a DataContract and its sub member as DataMember as

[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);  

that it...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.