0

This is my first time using a library outside of .NET let-alone JSON.Net so excuse my beginner question. I am trying to deserialize some AWS data into an object using the JSON.Net library but keep getting this error specifically with the IP address in the json file.

Example AWS Data

    {
  "ip_prefix": "205.251.254.0/24",
  "region": "GLOBAL",
  "service": "CLOUDFRONT"
},
{
  "ip_prefix": "216.137.32.0/19",
  "region": "GLOBAL",
  "service": "CLOUDFRONT"
}

Here is an example of my C#:

class AWS
{
    public string ipprefix;
    public string region;
    public string service;
}


   class DataGathering
{
    public List<AWS> GetIPData(string filename)
    {
        List<AWS> ipdata = new List<AWS>();
        using (StreamReader reader = new StreamReader(filename))
        {
            string json = reader.ReadToEnd();
            ipdata = JsonConvert.DeserializeObject<List<AWS>>(json);
        }

        return ipdata;
    }

Any help is much appreciated

1
  • The issue that I am having is that the region and service values are being stored, but the IP addresses are not. Commented Sep 5, 2017 at 1:59

1 Answer 1

1

Since the property name in the data has an underscore it is likely that it is not translating that to the class. This topic shows how you can define what the json property names are Json.Net: JsonSerializer-Attribute for custom naming

Sign up to request clarification or add additional context in comments.

4 Comments

The issue that I am having is that the region and service values are being stored, but the IP addresses are not.
yep - since the ip_prefix property in the json object has an underscore and the property on your AWS object does not, it is not able to match that data to the property. Use tha JsonProperty attribute with the PropertyName to tell the serializer that "ip_prefix" is expected to map to the ipprefix property in your class
thanks for that, I understand what you mean now. However it doesn't seem that it is attaching to the Json object. This is what I changed it to: [JsonProperty(PropertyName = "IPPrefix")] public string IPPrefix { get; set; }
Close, since the property name in the json data is "ip_prefix" the attribute declaration should look like this [JsonProperty(PropertyName = "ip_prefix")]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.