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();
    }
}
d219
  • 2,707
  • 5
  • 31
  • 36
K.Z
  • 5,201
  • 25
  • 104
  • 240
  • 1
    Possible duplicate of [Deserialize json object into dynamic object using Json.net](https://stackoverflow.com/questions/4535840/deserialize-json-object-into-dynamic-object-using-json-net) – Alex Riabov Jun 21 '18 at 10:28

1 Answers1

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);
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40
  • 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} – K.Z Jun 21 '18 at 10:44
  • Try JToken.Parse and ensure your JSON is valid, perhaps try an online validator just to be sure! Thanks! – Terry Lennox Jun 21 '18 at 10:45
  • No problem, glad to help!! – Terry Lennox Jun 21 '18 at 10:48