How to assign value to variable from a google API Json Response c#

290 Views Asked by At

I am trying to assign the value of a key from an async JSON response to a variable, the JSON key in question is the "threatType" Key. I am querying google's safebrowsing v4 API and I get a good response, the problem is when I try to assign a key to a variable nothing is assigned. Here's my code:

public static async Task<string> CheckUrl( string Api_Key, string MyUrl)
        {
            safeBrowsing_panel i = new safeBrowsing_panel();
            var service = new SafebrowsingService(new BaseClientService.Initializer
            {
                ApplicationName = "Link-checker",
                ApiKey = Api_Key
            });

            var request = service.ThreatMatches.Find(new FindThreatMatchesRequest()
            {
                Client = new ClientInfo
                {
                    ClientId = "Link-checker",
                    ClientVersion = "1.5.2"
                },
                ThreatInfo = new ThreatInfo()
                {
                    ThreatTypes = new List<string> { "SOCIAL_ENGINEERING", "MALWARE" },
                    PlatformTypes = new List<string> { "ANY_PLATFORM" },
                    ThreatEntryTypes = new List<string> { "URL" },
                    ThreatEntries = new List<ThreatEntry>
                    {
                        new ThreatEntry
                        {
                            Url = MyUrl
                        }
                    }
                }
            });

            var response = await request.ExecuteAsync();
            string json = JsonConvert.SerializeObject(await request.ExecuteAsync());

            string jsonFormatted = JToken.Parse(json).ToString(Formatting.Indented);
            Console.WriteLine(jsonFormatted);
            return jsonFormatted;
        }

I created classes to parse the json:

public class ThreatRes
        {
            public string threatType;
        }

        public class RootObjSB
        {
            public List<ThreatRes> matches;
        }

And I call it with:

string SB_Result = await CheckUrl("MyAPIKey", "Bad_URL");
RootObjSB obj = JsonConvert.DeserializeObject<RootObjSB>(SB_Result);

The JSON response from google:

{
  "matches": [
    {
      "cacheDuration": "300s",
      "platformType": "ANY_PLATFORM",
      "threat": {
        "digest": null,
        "hash": null,
        "url": "http://badurl.com/",
        "ETag": null
      },
      "threatEntryMetadata": null,
      "threatEntryType": "URL",
      "threatType": "SOCIAL_ENGINEERING",
      "ETag": null
    }
  ],
  "ETag": null
}

I need help please.

1

There are 1 best solutions below

1
Williams Harold On

So I tried using JavaScriptSerializer and it seemed to work just fine, I also reconstructed my classes to parse all the properties on the JSON response.

Reconstructed Classes:

public class Threat
        {
            public object digest { get; set; }
            public object hash { get; set; }
            public string url { get; set; }
            public object ETag { get; set; }
        }

        public class Match
        {
            public string cacheDuration { get; set; }
            public string platformType { get; set; }
            public Threat threat { get; set; }
            public object threatEntryMetadata { get; set; }
            public string threatEntryType { get; set; }
            public string threatType { get; set; }
            public object ETag { get; set; }
        }

        public class RootObjSB
        {
            public List<Match> matches { get; set; }
            public object ETag { get; set; }
        }

and i deserialize it like this:

string SB_Result = await CheckUrl("MyAPIKey", "Bad_URL");
var obj = new JavaScriptSerializer().Deserialize<RootObjSB>(SB_Result);
string threat = obj.matches[0].threatType;
Console.WriteLine(threat);

I really don't know why the first option I tried didn't parse the data correctly but this was how I overcame that problem. Hope it helps anyone going through the same thing