skip to Main Content

I have a JSON string, which I am splitting in 2 different string. But I want to use that to validate some condition.

Here is the split of json string:

string[] strMyObjects = jsonString.Split(',');

And getting it in string like

string strValidCheck = strMyObjects[0];

The split JSON string looks like

{ "IsAuthenticated": "true"

I want to use

if strValidCheck == true

then

{ do something  }

So how can I write that in C#?

2

Answers


  1. If you turn your string fragment into a full valid JSON string, then you can simply Deserialise it and check the value.

    So first create a simple class to Deserialise into

    public class ReallySimple
    {
        public bool IsAuthenticated { get; set; } = false;
    }
    

    Then make you fragment valid json by adding a trailing } and Deserialise it

    string strValidCheck = strMyObjects[0];
    var simpleClass = JsonConvert.DeserializeObject<ReallySimple>(strValidCheck + "}");
    
    
    if(simpleClass.IsAuthenticated == true)  
    { 
         //do something  
    }
    

    But you might be able to handle everything simpler than you think.
    One of the advantages of JSON Deserialization is that you only need to deserialize the properties that you actually need.

    So even if you have a long complex JSON, you can just pick you the things that you want.

    So lets say you have the following JSON

    {
        "title": "example glossary",
        "IsAuthenticated": "true",
        "MyIntValue": 69,
        "ID": "SGML",
        "SortAs": "SGML",
        "GlossTerm": "Standard Generalized Markup Language",
        "Acronym": "SGML",
        "Abbrev": "ISO 8879:1986",
        "GlossDef": {
            "para": "A meta-markup language, used to create markup languages such as DocBook.",
            "GlossSeeAlso": [
                "GML",
                "XML"
            ]
        },
        "GlossSee": "markup"
    }
    

    But we only want 2 items out of this, then we just create the class(es) and structure that we want and Deserialize that.

    public class MyCutDownClass
    {
        public bool IsAuthenticated { get; set; }
        public int MyIntValue { get; set; }
    }
    
    var json = "{rn    "title": "example glossary",rn    "IsAuthenticated": "true",rn    "MyIntValue": 69,rn    "ID": "SGML",rn    "SortAs": "SGML",rn    "GlossTerm": "Standard Generalized Markup Language",rn    "Acronym": "SGML",rn    "Abbrev": "ISO 8879:1986",rn    "GlossDef": {rn        "para": "A meta-markup language, used to create markup languages such as DocBook.",rn        "GlossSeeAlso": [rn            "GML",rn            "XML"rn        ]rn    },rn    "GlossSee": "markup"rn}";
    
    var myObj = JsonConvert.DeserializeObject<MyCutDownClass>(json); 
    Console.WriteLine($"Int value = {myObj.MyIntValue}");
    Console.WriteLine($"Authenticated = {myObj.IsAuthenticated}");
    

    So everything that you don’t want and need is simply ignored.

    Login or Signup to reply.
  2. Nobody splits a json string to find something, the common and the most relailable way is to parse it

        using Newtonsoft.Json;
    
        var jsonString = "{ "IsAuthenticated": "true"}";
        
        var jObj = JObject.Parse(jsonString);
    
        //the first way
        bool strValidCheck = (bool)jObj.Properties().First().Value;
        bool strValidCheck1 = (bool)jObj.Properties().ToArray()[0].Value;
        
        //the second way
        bool strValidCheck2 = (bool)jObj["IsAuthenticated"];
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search