custom json parse Archives - Microsoft Dynamics 365 Blog http://microsoftdynamics.in/category/custom-json-parse/ Microsoft Dynamics CRM . Microsoft Power Platform Fri, 24 Apr 2020 14:31:47 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://i0.wp.com/microsoftdynamics.in/wp-content/uploads/2020/04/cropped-Microsoftdynamics365-blogs.png?fit=32%2C32 custom json parse Archives - Microsoft Dynamics 365 Blog http://microsoftdynamics.in/category/custom-json-parse/ 32 32 176351444 Parse JSON in C# (without Newton JSON) specially for Dynamics 365 CRM V9 http://microsoftdynamics.in/2018/01/15/parse-json-in-c-without-newton-json-specially-for-dynamics-365-crm-v9/ Mon, 15 Jan 2018 10:50:00 +0000 Below is the Json from which i need to get  “Name”, “Email ID” and “Phone number” , Please use https://jsonformatter.curiousconcept.com/ for JSON Formatter. {'context':{'agent':{'name':'manish'},'customer':{'browserType':'Chrome','os':'WINDOWS'},'chat':{'prechatSurvey':[{'question':'What is your name?','answer':'manish'},{'question':'Phone Number','answer':'9393939384'},{'question':'Email Address','answer':'manish@gmail.com'}]}}} 1. Context is root list object public class RootObject     {         public Context context { get; set; }     } 2. As you can see...

The post Parse JSON in C# (without Newton JSON) specially for Dynamics 365 CRM V9 appeared first on Microsoft Dynamics 365 Blog.

]]>
Below is the Json from which i need to get  “Name”, “Email ID” and “Phone number” , Please use https://jsonformatter.curiousconcept.com/ for JSON Formatter.

{'context':{
'agent':{'name':'manish'},
'customer':{'browserType':'Chrome','os':'WINDOWS'},
'chat':{'prechatSurvey':[{'question':'What is your name?','answer':'manish'},{'question':'Phone Number','answer':'9393939384'},{'question':'Email Address','answer':'manish@gmail.com'}]}}
}

1. Context is root list object

public class RootObject
    {
        public Context context { get; set; }
    }

2. As you can see under Context there are 3 more list ‘agent , ‘customer’ and ‘chat’

  public class Context
    {
        public Agent agent { get; set; }
        public Customer customer { get; set; }
        public Chat chat { get; set; }
    }
3. Now Chat has list object,
  public class Agent
    {
        public string name { get; set; }
    }
    public class Customer
    {
        public string browserType { get; set; }    
        public string os { get; set; }
    }

4. Now ‘Chat’ has a list ‘prechatSurvey’

public class Chat
    {
        public List<PrechatSurvey> prechatSurvey { get; set; }
    }
5. ‘prechatSurvey’ contain Question and answer required
public class PrechatSurvey
    {
        public string question { get; set; }
        public string answer { get; set; }
    }

Now we can parse the JSON and get required value, please refer below code
 ———————————————————————————————–
   class Program

    {
        static void Main(string[] args)
        {
            var parameterValue1 = “{‘context’:{‘agent’:{‘name’:’manish’},’customer’:{‘browserType’:’Chrome’,‘os’:’WINDOWS’},’chat’:{‘prechatSurvey’:[{‘question’:’What is your name?’,’answer’:’manish’},{‘question’:’Phone Number’,’answer’:’9211151304′},{‘question’:’Email Address’,’answer’:’manishch@gmail.com‘}]}}}”;
            string fullname = string.Empty;
            string Phonenumber = string.Empty;
            string emailID = string.Empty;
            string message = string.Empty;
            byte[] result = Encoding.UTF8.GetBytes(parameterValue1.Replace(“‘”, “””));
            using (var jsonReader = JsonReaderWriterFactory.CreateJsonReader(result, XmlDictionaryReaderQuotas.Max))
            {
                var outputSerialiser = new DataContractJsonSerializer(typeof(RootObject));
                RootObject output = (RootObject)outputSerialiser.ReadObject(jsonReader);
                List<PrechatSurvey> prechatSurveyList = output.context.chat.prechatSurvey;
                foreach (PrechatSurvey prechatSurvey in prechatSurveyList)
                {
                    string question = prechatSurvey.question;
                    switch (question)
                    {
                        case “What is your name?”:
                            fullname = prechatSurvey.answer;
                            break;
                        case “Phone Number”:
                            Phonenumber = prechatSurvey.answer;
                            break;
                        case “Email Address”:
                            emailID = prechatSurvey.answer;
                            break;                      
                        default:
                            break;
                    }
                }
            }
           
        }
    }
    public class Agent
    {
        public string name { get; set; }
    }
    public class Customer
    {
      
        public string browserType { get; set; }    
        public string os { get; set; }
    }
    public class PrechatSurvey
    {
        public string question { get; set; }
        public string answer { get; set; }
    }
    public class Chat
    {
        public List<PrechatSurvey> prechatSurvey { get; set; }
    }
    public class Context
    {
        public Agent agent { get; set; }
        public Customer customer { get; set; }
        public Chat chat { get; set; }
    }
    public class RootObject
    {
        public Context context { get; set; }
    }
}
—————————————————————————————-
Hope this was Helpfull .thanks

SOURCE : mscrm.com

The post Parse JSON in C# (without Newton JSON) specially for Dynamics 365 CRM V9 appeared first on Microsoft Dynamics 365 Blog.

]]>
2749