2

I have json data in coming in dynamic in Web API and need to read through data but I am getting no where, also not sure how I can declare following below json data as string or dynamic in c# class?

json data

{
"sessionA": [
 {
   "order": 0,
   "type": "hidden",
   "name": "formId",
 },
{
  "order": 0,
  "type": "hidden",
  "name": "consultationId",
},
{
  "order": 0,
  "type": "hidden",
  "name": "clientId",
}
],
"sessionB": [
{
  "order": 0,
  "type": "heading",
  "label": "Super Quiz",
  "name": "title",
  "value": "Super Quiz",
  "validations": []
},
{
  "order": 5,
  "type": "separator",
  "label": "",
  "name": "separator",
  "value": "",
  "validations": []
  }
 ]
}

c# test console class

class Program
{
    static void Main(string[] args)
    {

        dynamic myjson = "   {
"sessionA": [
 {
   "order": 0,
   "type": "hidden",
   "name": "formId",
 },
{
  "order": 0,
  "type": "hidden",
  "name": "consultationId",
},
{
  "order": 0,
  "type": "hidden",
  "name": "clientId",
}
],
"sessionB": [
{
  "order": 0,
  "type": "heading",
  "label": "Super Quiz",
  "name": "title",
  "value": "Super Quiz",
  "validations": []
},
{
  "order": 5,
  "type": "separator",
  "label": "",
  "name": "separator",
  "value": "",
  "validations": []
  }
 ]
}
";

        Console.WriteLine("dynamic json convert to object");
        Console.WriteLine("---------------------------------");



        Console.Read();
    }
}
1

1 Answer 1

1

You can use Newtonsoft Json.net https://www.newtonsoft.com/json to parse JSON data in .NET, this is available via Nuget.

var myjson = @"{
    ""sessionA"": [
     {
       ""order"": 0,
       ""type"": ""hidden"",
       ""name"": ""formId"",
     },
    {
      ""order"": 0,
      ""type"": ""hidden"",
      ""name"": ""consultationId"",
    },
    {
      ""order"": 0,
      ""type"": ""hidden"",
      ""name"": ""clientId"",
    }
    ],
    ""sessionB"": [
    {
      ""order"": 0,
      ""type"": ""heading"",
      ""label"": ""Super Quiz"",
      ""name"": ""title"",
      ""value"": ""Super Quiz"",
      ""validations"": []
    },
    {
      ""order"": 5,
      ""type"": ""separator"",
      ""label"": """",
      ""name"": ""separator"",
      ""value"": """",
      ""validations"": []
      }
     ]
    }";

dynamic myObject = JToken.Parse(myjson);
// Log sessionA first order
Console.WriteLine(myObject.sessionA[0].order);

// Another option
JToken jToken = JToken.Parse(myjson);

// Get Session B first label
var label = jToken.SelectToken("sessionB[0].label").Value<string>();
Console.WriteLine("Label: " + label);
Sign up to request clarification or add additional context in comments.

3 Comments

I am getting error when I try to prase jsonObject ------ Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: The best overloaded method match for 'Newtonsoft.Json.Linq.JObject.Parse(string)' has some invalid arguments at CallSite.Target(Closure , CallSite , Type , Object ) at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1) at Ant.Analysis.Infrastructure.Commands.SaveFormQuestionsAnswers.Execute() in C:\Developments\AntDev\Workers\Ant.Analysis.Infrastructure\Commands\SaveFormQuestionsAnswers.cs:line 40}
Try JToken.Parse and ensure your JSON is valid, perhaps try an online validator just to be sure! Thanks!
No problem, glad to help!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.