Announcement

Collapse
No announcement yet.

Need help reading in json with arrays - vb.net

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Need help reading in json with arrays - vb.net

    Hi there

    This might be a cheeky ask but I know there are some clever programmers on this forum so thought I would ask here before I pull all my remaining hair out.

    I have done some work with json, deserializing with Newtonsoft in vb.net so I'm not completely new to this. But I'm definitely an amateur. Please forgive me if my use of terminology is wrong.

    I have a little project on at the momentat home and the json that is returned has me stumped. Basically there are nested arrays which can vary in size and I need to read the data from items in these arrays. I don't know how to determine the size of the arrays and besides, I dont know how to loop through the arrays to get to what I want.

    An example of the url returning the json is:
    https://chargepoints.dft.gov.uk/api/...t/json/limit/8

    You can see that the ChargeDevice array contains 8 items (this can vary in size) Within each of these 8 there is another array called Connector which can also vary in size.

    As an example, I want to be able to read in the ConnectorType items in each othe arrays: eg ChargeDevice►3►Connector►1►ConnectorType

    Thanks for your help.

    #2
    May be easier using Regex:

    Output using Jon00DataScraper in Regex mode:

    0=JEVS G105 (CHAdeMO) DC
    1=CCS Type 2 Combo (IEC62196)
    2=Type 2 Mennekes (IEC62196)
    3=Type 2 Mennekes (IEC62196)
    4=Type 2 Mennekes (IEC62196)
    5=Type 2 Mennekes (IEC62196)
    6=Type 2 Mennekes (IEC62196)
    7=Type 2 Mennekes (IEC62196)
    8=Type 2 Mennekes (IEC62196)
    9=Type 2 Mennekes (IEC62196)
    10=Type 2 Mennekes (IEC62196)
    11=Type 2 Mennekes (IEC62196)
    12=Type 2 Mennekes (IEC62196)
    13=Type 2 Mennekes (IEC62196)
    14=Type 2 Mennekes (IEC62196)
    15=Type 2 Mennekes (IEC62196)
    Pattern1Count=16
    Jon

    Comment


      #3
      I usually use https://json2csharp.com/ to generate C# classes from json. For your example above it generates the attached code.

      chargepoints.txt

      Then you just go myDeserializedClass.ChargeDevice[3].Connector[1].ConnectorType

      Obviously you need to have extra checks for nulls and array indexes, or at least exception handler.

      And if for example you want to find Connector by ConnectorType - use LINQ, i.e.

      myDeserializedClass.ChargeDevice[3].Connector.Where(con => con.ConnectorType == "some type").FirstOrDefault();

      Comment


        #4
        Just noticed you are using VB.NET - https://jsonutils.com/

        Comment


          #5
          Originally posted by jon00 View Post
          May be easier using Regex:

          Output using Jon00DataScraper in Regex mode:

          0=JEVS G105 (CHAdeMO) DC
          1=CCS Type 2 Combo (IEC62196)
          2=Type 2 Mennekes (IEC62196)
          3=Type 2 Mennekes (IEC62196)
          4=Type 2 Mennekes (IEC62196)
          5=Type 2 Mennekes (IEC62196)
          6=Type 2 Mennekes (IEC62196)
          7=Type 2 Mennekes (IEC62196)
          8=Type 2 Mennekes (IEC62196)
          9=Type 2 Mennekes (IEC62196)
          10=Type 2 Mennekes (IEC62196)
          11=Type 2 Mennekes (IEC62196)
          12=Type 2 Mennekes (IEC62196)
          13=Type 2 Mennekes (IEC62196)
          14=Type 2 Mennekes (IEC62196)
          15=Type 2 Mennekes (IEC62196)
          Pattern1Count=16
          Thank you Jon. It could well be time for me to use Jon00DataScraper. I'm going to investigate Alex's answer because I want to learn a bit more about json but if I struggle I'll turn to the DataScraper.

          Comment


            #6
            Originally posted by jon00 View Post
            May be easier using Regex:

            Output using Jon00DataScraper in Regex mode:

            0=JEVS G105 (CHAdeMO) DC
            1=CCS Type 2 Combo (IEC62196)
            2=Type 2 Mennekes (IEC62196)
            3=Type 2 Mennekes (IEC62196)
            4=Type 2 Mennekes (IEC62196)
            5=Type 2 Mennekes (IEC62196)
            6=Type 2 Mennekes (IEC62196)
            7=Type 2 Mennekes (IEC62196)
            8=Type 2 Mennekes (IEC62196)
            9=Type 2 Mennekes (IEC62196)
            10=Type 2 Mennekes (IEC62196)
            11=Type 2 Mennekes (IEC62196)
            12=Type 2 Mennekes (IEC62196)
            13=Type 2 Mennekes (IEC62196)
            14=Type 2 Mennekes (IEC62196)
            15=Type 2 Mennekes (IEC62196)
            Pattern1Count=16
            OK Jon, it looks like I'm going to use your DataScraper. I'm getting my head round it now and starting to make some progress. Thanks again for another useful utility.

            Comment

            Working...
            X