Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6878743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:45:40+00:00 2026-05-27T04:45:40+00:00

I am trying to read some JSON with DataContractJsonSerializer , as I normally do.

  • 0

I am trying to read some JSON with DataContractJsonSerializer, as I normally do.

Valid JSON as follows:

  {
"error": "false",
"errorMsg": "Error message",
"errorField": "",
"time": "19:24",
"times": [
    [
        "blank",
        "19:35",
        "London",
        "direct",
        "1",
        "new"
    ],
    [
        "blank",
        "20:07",
        "Manchester",
        "direct",
        "1",
        "old"
    ]
],
"buses": [],
"ferries": [],
"updates": []
 }

C#

 DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(NRResult));

                using (var stream = new MemoryStream(Encoding.Unicode.GetBytes(result1)))
                {
                    var item = (NRResult)serializer.ReadObject(stream);
                   foreach (var item in routes.Timings)
                    {
                        NRData _item = new NRData();

                        _item.Route = route.unknown1;
                        _item.Direction = route.time;
                        _item.Time = route.destination;
                        _item.AimedDepart = route.ontime;
                        _item.ExpectedDepart = route.unknown2;
                        _item.OpRef = route.link;

                        listBox1.Items.Add(_item);
                    } 

Data class:

 [DataContract]
public class NRResult
{
    [DataMember(Name = "times")]
    public IEnumerable<Timings> Timings{ get; set; }
}
[DataContract]

public class Timings    {
    [DataMember]
    public string unknown1 { get; set; }
    [DataMember]
    public string time { get; set; }
    [DataMember]
    public string destination { get; set; }
    [DataMember]
    public string ontime { get; set; }
    [DataMember]
    public string unknown2 { get; set; }
    [DataMember]
    public string link { get; set; }
}

I get an InvalidCastException at the line
var item = (NRResult)serializer.ReadObject(stream);

And I am not sure why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:45:41+00:00Added an answer on May 27, 2026 at 4:45 am

    I think the declaration of NRResult should be something like this.

    [DataContract]
    public class NRResult
    {
        [DataMember(Name = "times")]
        public IEnumerable<string[]> Timings { get; set; }
    }
    
    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(NRResult));
    using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(result1)))
    {
        var item = (NRResult)serializer.ReadObject(stream);
        foreach (var route in item.Timings)
        {
            var _item = new{
                    Route = route[0],
                    Time = route[1],
                    Destination = route[2],
                    AimedDepart = route[3],
                    ExpectedDepart = route[4],
                    OpRef = route[5]
                };
    
            Console.WriteLine(_item);
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to read some JSON data from the Tumblr API. I'm using the
Im trying to use JSON.NET framework in a windows Form to read some information
I've been experiencing some problems for quite some time trying to load a JSON
Hi everyone I am trying to read some json and do 2 things. Insert
I am trying to read some settings from php.ini using zend. The API that
I'm trying to read some OpenGL tutorials on the net. the problem is that
I am trying to read some smps files with python, and found a fortran
I am trying to read some info from a text file by using windows
I'm getting something pretty strange going on when trying to read some data using
I'm trying to read in some sample data from an XML file in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.