Click here to Skip to main content
15,896,383 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have a json string that I am trying to parse and assign the values to a variable. I am using System.Web.Script.Serialization JavaScriptSerializer library.

C#
var data = "{
    "data1": {
"EntityList": "Attribute",
"KeyName": "AkeyName",
"Value": "Avalue"
    },
    "data2": {
        "Id": "jsdksjkjdiejkwp12193jdmsldm",
        "Status": "OK"
    }
}"; //variable with json string
JavaScriptSerializer jss = new JavaScriptSerializer();
dynamic drecord = jss.Deserialize<dynamic>(data);


What I have tried:

When I try to parse this json string to a variable for the value of EntityList and KeyName like below I am running into all sorts of errors.

var EntityList = drecord.data1.EntityList


Could you please help me write the value of EntityList and KeyName to a variable.

Thank you.
Posted
Updated 3-Apr-20 4:58am

Looks like the JavaScriptSerializer doesn't support deserializing to a dynamic object. If you look at the type of your drecord variable, you'll see that it's a Dictionary<string, object>, which doesn't support dynamic access.

You'll need to use indexers to access the properties:
C#
dynamic drecord = jss.Deserialize<dynamic>(data);
string entityList = drecord["data1"]["EntityList"];
string keyName = drecord["data1"]["KeyName"];
 
Share this answer
 
Comments
Member 12586110 4-Apr-20 18:22pm    
Thank you Richard Deeming.
According to the MS Documentation for this method, you should be using it.
Quote:
JavaScriptSerializer Class
Definition
Namespace:
System.Web.Script.Serialization
Assembly:
System.Web.Extensions.dll
For .NET Framework 4.7.2 and later versions, use the APIs in the System.Text.Json namespace for serialization and deserialization. For earlier versions of .NET Framework, use Newtonsoft.Json. This type was intended to provide serialization and deserialization functionality for AJAX-enabled applications.


So, using Newtonsoft.Json (AKA Json.net)
C#
using System;
using Newtonsoft.Json;

namespace Answer
{
    class Program
    {
        public class DataType1
        {
            public string EntityList { get; set; }
            public string KeyName { get; set; }
            public string Value { get; set; }
        }

        public class DataType2
        {
            public string Id { get; set; }
            public string Status { get; set; }
        }

        public class MyData
        {
            public DataType1 data1 { get; set; }
            public DataType2 data2 { get; set; }
        }

        static void Main(string[] args)
        {
            var data = @"{
                ""data1"": {
                    ""EntityList"": ""Attribute"",
                    ""KeyName"": ""AkeyName"",
                    ""Value"": ""Avalue""
                        },
                ""data2"": {
                    ""Id"": ""jsdksjkjdiejkwp12193jdmsldm"",
                    ""Status"": ""OK""
                }
            }"; //variable with json string

            var myData = JsonConvert.DeserializeObject<MyData>(data);
            

        Console.WriteLine($"EntityList:{myData.data1.EntityList}, KeyName:{myData.data1.KeyName}");
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900