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
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
Then make you fragment valid json by adding a trailing
}
and Deserialise itBut 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
But we only want 2 items out of this, then we just create the class(es) and structure that we want and Deserialize that.
So everything that you don’t want and need is simply ignored.
Nobody splits a json string to find something, the common and the most relailable way is to parse it