Bokbob Bokbob - 4 months ago 5
JSON Question

was not able to parse JSON in C#

My JSON is like below and I couldn't parse this. not sure how to go from looking at content in foreach...

My restSharp code is

var client = new RestClient("https://survey.qualtrics.com//WRAPI/ControlPanel/api.php?API_SELECT=ControlPanel&Version=2.5&Request=getPanel&User=dgfdhfgh&Token=DfghgfhghjUNUGaefghjP&Format=JSON&LibraryID=UR_5cJwNFX8lwFXFwp&PanelID=ML_b8IoBrawfm3MCd7");
var request = new RestRequest(Method.GET);

client.AddHandler("application/json", new JsonDeserializer());

var result = client.Execute<List<RootObject>>(request);

if (result != null && (result.StatusCode == HttpStatusCode.OK) ) // It's probably not necessary to test both
{

foreach (var j in result.Content)
{
XXXXXXX;

}


and JSON is as follows

[
{"RecipientID":"MLRP_1BNnjFoPFgeajfT","FirstName":"Darfghryl","LastName":"Wright","Email":"heoaxcvdcbk7@gmail.com","ExternalDataReference":null,"Unsubscribed":"0","EmbeddedData":{"Participant ID":"16941","DateTaken":"20160710","TriggerResponseID":"R_VKe0amWmZIXcuZ3","TriggerSurveyID":"SV_eA35TXE0iRlexBX","AllEmailsSchduledForSurvey2":"yes"}},
{"RecipientID":"MLRP_cCjJTFP6MxpZFhH","FirstName":"fghfgh","LastName":"Henzell","Email":"fghfgh@gmail.com","ExternalDataReference":null,"Unsubscribed":"0","EmbeddedData":{"Participant ID":"29377","DateTaken":"20160711","TriggerResponseID":"R_z14mwG7l7DBCYg1","TriggerSurveyID":"SV_eA35TXE0iRlexBX","AllEmailsSchduledForSurvey2":"yes"}}]


and my classes are as below please help

public class EmbeddedData
{
public string Participant_ID { get; set; }
public string DateTaken { get; set; }
public string TriggerResponseID { get; set; }
public string TriggerSurveyID { get; set; }
public string AllEmailsSchduledForSurvey2 { get; set; }
public string AllEmailsSchduledForSurvey3 { get; set; }
}

public class RootObject
{
public string RecipientID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public object ExternalDataReference { get; set; }
public string Unsubscribed { get; set; }
public List<EmbeddedData> EmbeddedData { get; set; }
}

Answer

Please try

 if (result != null && (result.StatusCode == HttpStatusCode.OK) ) 
 {
   // here I made changes 

    RestSharp.Deserializers.JsonDeserializer deserial = new JsonDeserializer();

    List<RootObject> content = deserial.Deserialize<List<RootObject>>(result);

    foreach (var cont in content)
    {
        // Do your Stuff
    }


 }
Comments