How to access this json #2

I have very little experience of json and never before used Newtonsoft json.net.

I have installed json.net and got it to work somewhat following advise from @Brathnann in this post: How to access this json class

This is a pretty large json and I am trying to load it into a List but get the following error:

JsonReaderException: Error parsing comment. Expected: *, got U. Path '', line 1, position 1.

I have the following class structure called YrModel generated via https://json2csharp.com/

[Serializable]
public class YrModel
{
    public class Root
    {
        public string type { get; set; }
        public Geometry geometry { get; set; }
        public Properties properties { get; set; }
    }


    public class Data
    {
        public Instant instant { get; set; }
        public Next12Hours next_12_hours { get; set; }
        public Next1Hours next_1_hours { get; set; }
        public Next6Hours next_6_hours { get; set; }
    }

    public class Details
    {
        public double air_pressure_at_sea_level { get; set; }
        public double air_temperature { get; set; }
        public double cloud_area_fraction { get; set; }
        public double relative_humidity { get; set; }
        public double wind_from_direction { get; set; }
        public double wind_speed { get; set; }
        public double precipitation_amount { get; set; }
    }

    public class Geometry
    {
        public string type { get; set; }
        public List<double> coordinates { get; set; }
    }

    public class Instant
    {
        public Details details { get; set; }
    }

    public class Meta
    {
        public DateTime updated_at { get; set; }
        public Units units { get; set; }
    }

    public class Next12Hours
    {
        public Summary summary { get; set; }
    }

    public class Next1Hours
    {
        public Summary summary { get; set; }
        public Details details { get; set; }
    }

    public class Next6Hours
    {
        public Summary summary { get; set; }
        public Details details { get; set; }
    }

    public class Properties
    {
        public Meta meta { get; set; }
        public List<Timeseries> timeseries { get; set; }
    }


    public class Summary
    {
        public string symbol_code { get; set; }
    }

    public class Timeseries
    {
        public DateTime time { get; set; }
        public Data data { get; set; }
    }

    public class Units
    {
        public string air_pressure_at_sea_level { get; set; }
        public string air_temperature { get; set; }
        public string cloud_area_fraction { get; set; }
        public string precipitation_amount { get; set; }
        public string relative_humidity { get; set; }
        public string wind_from_direction { get; set; }
        public string wind_speed { get; set; }
    }
}

Given results from googling I have tried a more slimmer model with only the properties I need but get the same result.

In my last tests I used the following code to try to read the json into a List:

void Test2()
    {
        path = JSON_FileHandler.GetPath("myjsonBack.json");

        if (File.Exists(path))
        {
            var _my_List = JsonConvert.DeserializeObject<YrModel>(path);
        }
    }

This is an extract of the json file:

{
  "type": "Feature",
  "geometry": {
    "type": "Point",
    "coordinates": [
      9.58,
      60.1,
      496.0
    ]
  },
  "properties": {
    "meta": {
      "updated_at": "2022-09-28T07:36:26Z",
      "units": {
        "air_pressure_at_sea_level": "hPa",
        "air_temperature": "celsius",
        "cloud_area_fraction": "%",
        "precipitation_amount": "mm",
        "relative_humidity": "%",
        "wind_from_direction": "degrees",
        "wind_speed": "m/s"
      }
    },
    "timeseries": [
      {
        "time": "2022-09-28T08:00:00Z",
        "data": {
          "instant": {
            "details": {
              "air_pressure_at_sea_level": 1004.4,
              "air_temperature": 8.0,
              "cloud_area_fraction": 100.0,
              "relative_humidity": 96.2,
              "wind_from_direction": 60.5,
              "wind_speed": 3.8,
              "precipitation_amount": 0.0
            }
          },
          "next_12_hours": {
            "summary": {
              "symbol_code": "lightrain"
            }
          },
          "next_1_hours": {
            "summary": {
              "symbol_code": "lightrain"
            },
            "details": {
              "air_pressure_at_sea_level": 0.0,
              "air_temperature": 0.0,
              "cloud_area_fraction": 0.0,
              "relative_humidity": 0.0,
              "wind_from_direction": 0.0,
              "wind_speed": 0.0,
              "precipitation_amount": 0.3
            }
          },
          "next_6_hours": {
            "summary": {
              "symbol_code": "rain"
            },
            "details": {
              "air_pressure_at_sea_level": 0.0,
              "air_temperature": 0.0,
              "cloud_area_fraction": 0.0,
              "relative_humidity": 0.0,
              "wind_from_direction": 0.0,
              "wind_speed": 0.0,
              "precipitation_amount": 1.7
            }
          }
        }
      }
    ]
  }
}

I have validated the array @ https://jsonlint.com/

https://json2csharp.com/ Throw your string into here and you’ll see your data structure, including the deserialize call that you should be able to do. Looks like your data structure is wrong.

1 Like

Thank you but I have already done that see “I have the following class structure called YrModel generated via https://json2csharp.com/” in text.

Since this is right at the start of the file, this smells like a stray BOM in your input file.

https://en.wikipedia.org/wiki/Byte_order_mark

You can use a hex dumper to look at the start of your JSON payload to make sure there is no BOM at the start.

https://www.cryptosys.net/pki/utf8bom.html

At least eliminate that possibility, because if that’s what it is, NOTHING else you do will work, so at least rule it out.

2 Likes

Checked, no BOM I am afraid.

I took the json you posted and there was no YrModel there. This is what it gave me back.

// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
    public class Data
    {
        public Instant instant { get; set; }
        public Next12Hours next_12_hours { get; set; }
        public Next1Hours next_1_hours { get; set; }
        public Next6Hours next_6_hours { get; set; }
    }

    public class Details
    {
        public double air_pressure_at_sea_level { get; set; }
        public double air_temperature { get; set; }
        public double cloud_area_fraction { get; set; }
        public double relative_humidity { get; set; }
        public double wind_from_direction { get; set; }
        public double wind_speed { get; set; }
        public double precipitation_amount { get; set; }
    }

    public class Geometry
    {
        public string type { get; set; }
        public List<double> coordinates { get; set; }
    }

    public class Instant
    {
        public Details details { get; set; }
    }

    public class Meta
    {
        public DateTime updated_at { get; set; }
        public Units units { get; set; }
    }

    public class Next12Hours
    {
        public Summary summary { get; set; }
    }

    public class Next1Hours
    {
        public Summary summary { get; set; }
        public Details details { get; set; }
    }

    public class Next6Hours
    {
        public Summary summary { get; set; }
        public Details details { get; set; }
    }

    public class Properties
    {
        public Meta meta { get; set; }
        public List<Timeseries> timeseries { get; set; }
    }

    public class Root
    {
        public string type { get; set; }
        public Geometry geometry { get; set; }
        public Properties properties { get; set; }
    }

    public class Summary
    {
        public string symbol_code { get; set; }
    }

    public class Timeseries
    {
        public DateTime time { get; set; }
        public Data data { get; set; }
    }

    public class Units
    {
        public string air_pressure_at_sea_level { get; set; }
        public string air_temperature { get; set; }
        public string cloud_area_fraction { get; set; }
        public string precipitation_amount { get; set; }
        public string relative_humidity { get; set; }
        public string wind_from_direction { get; set; }
        public string wind_speed { get; set; }
    }
1 Like

Hmm you are correct I created the YrModel during my tests to see if that worked, missed that. I did get the same as you.

Reason was that I tried different models. My mistake I am afraid.

The json I am trying to put in a list is: https://api.met.no/weatherapi/locationforecast/2.0/compact?lat=60&lon=11

So, in the app when you get that string back from the API and try to deserialize it, you are getting that above error?
This code seemed to work just fine for me.

IEnumerator Start()
    {
        using (UnityWebRequest www = UnityWebRequest.Get("https://api.met.no/weatherapi/locationforecast/2.0/compact?lat=60&lon=11"))
        {
            yield return www.SendWebRequest();
            Debug.Log(www.result);

            Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(www.downloadHandler.text);
            Debug.Log(myDeserializedClass.geometry.coordinates[0]);
        }
    }
2 Likes

I must say a BIG thank you to you guys as I FINALLY understand how this work. I have spent so much time testing and realise I have been thinking completely wrong how to do this. Really appreciate your help!

2 Likes