Insert into class if criteria is met RRS feed

  • Question

  • User333333839 posted


    I'm fairly new to C# and I'm stuck on the following issue.

    I have the following classes:

    public class Journeys
    public string tripGUID { get; set; }
    public Int32 scheduledJourneyNotificationMins { get; set; }
    public string distance { get; set; }
    public string duration { get; set; }
    public List<Legs> legs { get; set; }

    public class Legs
    public string legGUID { get; set; }
    public string timeMinute { get; set; }
    public string path { get; set; }
    public List<Disruptions> disruptions { get; set; }
    public Int32 stopsInSequence { get; set; }
    public List<Points> points { get; set; }
    public Mode mode { get; set; }

    public class Disruptions
    public string reference { get; set; }
    public string latitude { get; set; }
    public string longitude { get; set; }
    public System.DateTime start_date_time { get; set; }
    public System.DateTime end_date_time { get; set; }
    public string source { get; set; }
    public string name { get; set; }
    public string short_description { get; set; }

    public class Points
    public string name { get; set; }
    public string nameWO { get; set; }
    public string platformName { get; set; }
    public PointsDateTime dateTime { get; set; }
    public Ref reference { get; set; }

    As you can see, they are related, with Journeys being the top level.

    I'm already inserting data into these classes successfully (except "Disruptions") and then using JsonConvert.SerializeObject to output json.

    After I've loaded data into the classes, I then call another service which gives me the Disruption data in Json format (I can't load this at the same time as the initial data).

    I then want to say something like (as I loop through the Json):

    "If Journeys.tripGUID = "xx" and Legs.legGUID = "yy", then insert data into the Disruptions class"
    - xx is the tripGUID from the Disruption data
    - yy is the legGUID from the Disruption data

    Obviously, the insert method would have to occur against the correct tripGUID and legGUID data.

    Any help/advise would be appreciated.

    Thanks James

    Thursday, July 19, 2018 12:50 PM

All replies

  • User753101303 posted


    Not sure to get the full context. Json is a serialization format. My first move would be to deserialize data and work on .NET objects (possibly using Linq). My understanding is that you are trying to work directly on the JSON string or suing a JSONReader ?

    Also ASP.NET does provide serialization/deserialization support so you should rarely need to, explicitely do that in your code. For now my understanding is that steps are :

    • you have data
      - you transmit to a service, HttpClient could serialize this for you and return deserialized data for you
      - you then process the data you sent (that you kept as .NET objects) all along with the deserialized data you got from  the service
      - and proceed further as needed

    In short don't be overobsessed about trying to skip a deserialization step. Do something simple that works and could fit your needs rather than trying to optimize with a much more complex code without being 100% sure you'll get any real benefit.

    Also when not done properly it happens to see a situation where the json string is serialized again by ASP.NET (and sometimes it is unnoticed until a 3rd party tries to consume your service).

    Thursday, July 19, 2018 9:50 PM