0

I'm trying to use a JSON stream using the following code(console example):

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Net.Http;
using System.Data;
using System.Net;

namespace jsontest
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var webClient = new WebClient())
            {
                string jsonString = webClient.DownloadString("https://services.nvd.nist.gov/rest/json/cves/1.0?cvssV3Severity=CRITICAL&resultsPerPage=30"); 
                deserialiseJSON(jsonString);

            }

            void deserialiseJSON(string strJSON)
            {
                    var jPerson = JsonConvert.DeserializeObject<Rootobject>(strJSON);
                    foreach(var num in jPerson.result.CVE_Items)
                    {
                        //Get the ID number and basescore -> Succes
                        Console.WriteLine("\nCVE ID           : " + num.cve.CVE_data_meta.ID + "          CVSSv3 baseScore    : " + num.impact.baseMetricV3.cvssV3.baseScore);
                        //Get the ID number and basescore -> Succes
                        Console.WriteLine("AndOr : " + num.configurations.nodes[0]._operator);                            
                    }               
            }
        }
    }
}

This works perfect with almost all the information within the Json stream. BUT not for one item and that is the operator item witch is a string and always containing AND or OR. It's not possible to read that as a string but clearly is also defined as one.

I use a class generated by VisualStudio2019 to store the data. Here is a part of that class where the operator string is defined:

public class Configurations
    {
        public string CVE_data_version { get; set; }
        public Node[] nodes { get; set; }
    }

    public class Node
    {
        public string _operator { get; set; }
        public Cpe_Match[] cpe_match { get; set; }
        public Child[] children { get; set; }

If I look at the Json data using http://jsoneditoronline.org/ it looks like this(again just a part):

"configurations": {
                    "CVE_data_version": "4.0",
                    "nodes": [
                        {
                            "operator": "OR",
                            "cpe_match": [

I need the information because if it OR or AND the data changes and I need to change my reading logic to read the extra information.

So how should I handle OR and AND strings in Json and use it so that I can make a decision in my program.

Thanks in advance

provell
  • 1
  • 1
  • I read your question twice, and... I'm still very unclear as to what your actual problem is. What does "*It's not possible to read that as a string*" actually mean, in practice? – canton7 Apr 21 '21 at 14:33
  • Why does the `_operator` property in the `Node` class start with an underscore? (right, reserved keyword) It doesn't have an underscore in the JSON property. – gunr2171 Apr 21 '21 at 14:35
  • 3
    You probably need `[JsonProperty("operator")] string _operator` or `string @operator`. VS saw that `operator` is a reserved C# keyword, but didn't account for it in the same way that json.net does – canton7 Apr 21 '21 at 14:35
  • YES!! First of all thanks for the quick reply's. And doing some research with your information led me to the conclusion that _operator makes no sense because it is not in the Json stream. Thus leaves me with an empty string. It should be : public string @operator { get; set; } . Now I used the VisualStudio option to make a class Edit->Paste Special -> Paste Json As Class. Apparently that helps but is not perfect. And using a word like "operator" might not be the smartest thing to do but that is not up to me, that is wat the nvd.nist.org returns. Anyway, thanks...it now works perfectly :-) – provell Apr 22 '21 at 09:32
  • @provell Remember that VS doesn't necessarily know anything about json.net, which is the library which you're using these generated C# classes with. It might be generating code which would have worked with e.g. `DataContractJsonSerializer` or `JavaScriptSerializer` or perhaps even `System.Text.Json`: these all have different conventions for how objects are serialized/deserialized. There are online tools which will generate C# classes specifically for json.net, which should do a better job when used with json.net – canton7 Apr 22 '21 at 10:08
  • 1
    That's helpful, good to know. I will do that next time as I have lot's of other things with Json I would like to use. – provell Apr 22 '21 at 12:44

0 Answers0