How to compare parsed json value in C#

I am a beginner in c# , I am creating a app in which login page requests PHP file in url and it sends Json data as response I was able to decode the Json data,But the decoded data was not able to use in string comparison as below

Program

 private async void Button_Click(object sender, RoutedEventArgs e)
 {
 var username = usernames .Text;
 var password = passwords .Password;
 var postMessage = new StringContent(string.Format("username={0}&password={1}", username, password), Encoding.UTF8 , "application/x-www-form-urlencoded");
 var response = await (new HttpClient()).PostAsync("http://xxxxx.xx.xxx/xlogin.php", postMessage);
 var responseBody = await response.Content.ReadAsStringAsync();
 var jsonString = responseBody ;
        //remove "{" and "}" from sting
 var result = jsonString.Replace("{", "").Replace("}", "");
        //separate property name from it's value
 var pair = result.Split(':');
        //property will contain property name : "result"
 var property = pair[0];
        //value will contain property value : "Invalid"
 var value = pair[1];
       // String van=value .ToString() ;
        MessageBox.Show(value);
 if(value=="Valid")
{
Messagebox.show("success");
}

else
{
   Messagebox.show("Error");
}                 
}

Response from URL Json response from url is {"result":"Invalid"} while validation fails and for validation success {"result":"Valid"}

Problem Every time I get "Valid" From url it is not accepting in if condition,more precisely string is not getting compared...any solutions?

Jon Skeet
people
quotationmark

I suspect the immediate problem is that your pair[1] value still starts and ends with a double quote - so if you print it out you'll see

"Value"

rather than

Value

You could just trim them from the start manually, but I would strongly advise you to use a JSON library instead. There's no good reason to do all of this manually, and trying to do so is very likely to lead to brittle code.

As an example, using Json.NET it's as simple as:

string json = "{\"result\":\"Valid\"}";
JObject parsed = JObject.Parse(json);
string result = (string) parsed["result"];
Console.WriteLine(result); // Prints Valid

people

See more on this question at Stackoverflow