0

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.

  • Is **RootObjSB** always null? Looks like the response from Google has 2 properties: _matches_ and _ETag_. So your **RootObjSB** will need to match that signature. – Stinky Towel Jan 24 '20 at 20:21
  • I am a bit confused, where exactly are you trying to assign the value? All I see is you serializing your object. I created a quick dotnetfiddle and i see your class does get deserialized correctly. – SomeStudent Jan 24 '20 at 20:23
  • @StinkyTowel So do i have to create a variable for every single property in the response for it to work? – Williams Harold Jan 24 '20 at 21:23
  • @SomeStudent I am trying to assign the 'threatType' property's value to a variable. So i created the **RootObjSB** Class to assign the property i need – Williams Harold Jan 24 '20 at 21:24
  • Seems to work fine when i ran it, you have your RootObjSB, which as an array of matches. Said matches has a threat type property that is set to Social Engineering; is that was you were trying to accomplish? – SomeStudent Jan 24 '20 at 21:33
  • @SomeStudent so why is it that when i try to write it to console I don't get a value – Williams Harold Jan 24 '20 at 21:37
  • I am not sure what JToken is, or how it does its parsing. Though if you were to do foreach(var p in obj.matches) {Console.WriteLine(p.threatType):} where you deserialize it to your root object then it will print that field fine – SomeStudent Jan 24 '20 at 22:32
  • @SomeStudent `JToken` is a class from the `Newton.Json` Library. Thank you for the help. your clarification of my working class made my try another method of parsing my data – Williams Harold Jan 24 '20 at 22:46

1 Answers1

1

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