Resolved JsonConvert.DeserializeObject System.NullReferenceException

Snoopy

New member
Joined
Jun 18, 2020
Messages
3
Programming Experience
Beginner
Hi all.
I am trying to download a dynamic json from a site to the var Vjson .
deseralize it and show some content to the console.
I don't know what i am doing wrong. i receive a NullReferenceException error however i am sure there is data.

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using Newtonsoft.Json;
//using ReadJSONusingCsharp.Models;
using System.Data.Common;
using System.Threading;

namespace jsonCmd0003
{
    class Program
    {
        static void Main(string[] args)
        {

          
                var client = new WebClient();
                var Vjson = client.DownloadString(Some URL);
                dynamic Post = JsonConvert.DeserializeObject( Vjson);
                foreach (var item in Post.Match)  //loop through class Post Match error System.NullReferenceException
            {

                Console.WriteLine("{0} {1} \n", item.ip_str, item.port);
            }
          

               Console.ReadKey();

            }
    }
}


the class is

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace jsonCmd0003
{


    class Post
 
    {

        public class Location
        {

            public string city { get; set; }
            public string region_code { get; set; }
            public int? area_code { get; set; }
            public double longitude { get; set; }
            public string country_code3 { get; set; }
            public double latitude { get; set; }
            public string postal_code { get; set; }
            public int? dma_code { get; set; }
            public string country_code { get; set; }
            public string country_name { get; set; }

        }

        public class Options
        {

        }

        public class Shodan
        {

            public string crawler { get; set; }
            public bool ptr { get; set; }
            public string id { get; set; }
            public string module { get; set; }
            public Options options { get; set; }

        }

        public class AngularJS
        {

            public IList<string> categories { get; set; }

        }

        public class Components
        {

            public AngularJS AngularJS { get; set; }

        }

        public class Http
        {

            public int? robots_hash { get; set; }
            public IList<object> redirects { get; set; }
            public object securitytxt { get; set; }
            public string title { get; set; }
            public int? sitemap_hash { get; set; }
            public string robots { get; set; }
            public object favicon { get; set; }
            public string host { get; set; }
            public string html { get; set; }
            public string location { get; set; }
            public Components components { get; set; }
            public object securitytxt_hash { get; set; }
            public string server { get; set; }
            public string sitemap { get; set; }
            public int html_hash { get; set; }
            public string waf { get; set; }

        }

        public class Match
        {

            public int hash { get; set; }
            public object ip { get; set; }
            public string isp { get; set; }
            public string transport { get; set; }
            public string data { get; set; }
            public string asn { get; set; }
            public int port { get; set; }
            public IList<string> hostnames { get; set; }
            public Location location { get; set; }
            public DateTime timestamp { get; set; }
            public IList<string> domains { get; set; }
            public string org { get; set; }
            public object os { get; set; }
            public Shodan _shodan { get; set; }
            public string ip_str { get; set; }
            public string product { get; set; }
            public string devicetype { get; set; }
            public Http http { get; set; }
            public string title { get; set; }
            public IList<string> tags { get; set; }
            public IList<string> cpe { get; set; }

        }

        public IList<Match> matches { get; set; }
        public int total { get; set; }

    }

    

}

What am i doing wrong ?
any help would be appreciated
 
Use your debugger. Is Post null? Is Post.Match null?
 
Also, looking at your class, Post has a matches member, not Match.
 
Any particular reason why you are using a dynamic type from the JSON converter instead of a definite typed Post?
 
Back
Top Bottom