vicky kumar

vicky kumar

  • NA
  • 2
  • 608

read special character from jsonObject key

Aug 31 2021 5:55 PM

I want to read name(test) after conveting string to Json Object.

public class Program{
    public static void Main()
    {
        string raw="{\n" + 
                "\"id(test)\": \"2130556\",\n" + 
                "\"name(test)\": \"6915329\"}";
        JObject jobj=JObject.Parse(raw);
        string s=fetchAsString( jobj,"name(test)");
        Console.WriteLine("fetchAsString "+s);
    }
    public static string fetchAsString(JObject json, string x)
    {
        JToken t = json.SelectToken(x, errorWhenNoMatch: false);
        if (t != null)
        {
            return t.ToString();
        }
        else
        {
            return null;
        }
    }
}