skip to Main Content
{
  "MethodParameters": {
    "operation": [ "write", "click", "text" ],
    "stepDetail": "login",
    "welcome": "Hello Bob!"
  }

if I want to get the value of "stepDetail" under "MethodParameters".

I’m trying it like this:

public static JObject? jsconfig1 = ReadJson(@"DataFilesjsconfig1.json".ToString()); 

public static string loginA = .SelectToken("MethodParameters").SelectToken("stepDetail").Value<string>();

but it does not work for me. any help is appreciated.

4

Answers


  1. Why cannot you do something like {nameOfYourObject}.MethodParameters.stepDetail?

    Login or Signup to reply.
  2. Why don’t try this

        using Newtonsoft.Json;
    
        json = File.ReadAllText(@"DataFilesjsconfig1.json");
    
        var methodParameters = JObject.Parse(json)["MethodParameters"];
        string loginA = (string) methodParameters["stepDetail"];
    
    Login or Signup to reply.
  3. Your json is saved in variables you can access to nested field.

    var json = [YOUR JSON];
    var nestedField = json["MethodParameters"]["stepDetail"]);
    
    Login or Signup to reply.
  4. You should convert your json to an object with Newtonsoft.Json and then work with it as any object.

    This should be your class:

      public class MethodParameters
        {
            public List<string> operation { get; set; }
            public string stepDetail { get; set; }
            public string welcome { get; set; }
        }
    
        public class Root
        {
            public MethodParameters MethodParameters { get; set; }
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search