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