2016-07-22 190 views
-3

我的JSON就像下面,我無法解析這一點。不知道如何從看着的foreach內容去...無法解析JSON在C#

我restSharp代碼

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; 

} 

和JSON如下

[ 
{"RecipientID":"MLRP_1BNnjFoPFgeajfT","FirstName":"Darfghryl","LastName":"Wright","Email":"[email protected]","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":"[email protected]","ExternalDataReference":null,"Unsubscribed":"0","EmbeddedData":{"Participant ID":"29377","DateTaken":"20160711","TriggerResponseID":"R_z14mwG7l7DBCYg1","TriggerSurveyID":"SV_eA35TXE0iRlexBX","AllEmailsSchduledForSurvey2":"yes"}}] 

和我的類如下,請幫助

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; } 
} 
+0

根據您的類定義,它應該是JSON中的Participant_ID,而名稱「參與者ID」中有一個空格 – Ash

+1

請在將來更努力地格式化您的代碼,並提供[mcve] - 我們不需要所有這些屬性來查找問題,我懷疑在將實際的代碼/數據減少到[mcve]時,您可能已經發現了這個問題。 –

+0

是的,這是真的,但在我去那裏之前,它不允許我從result.content訪問數據..不知道如何遍歷從result.content獲得的jSON。請幫助 – Bokbob

回答

0

請嘗試

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 
    } 


} 
+0

Hi Muhammad Usman,如何使用RestSharp對此進行反序列化? – Bokbob

+0

@Bokbob請檢查更新回答 –

+0

謝謝穆罕默德烏斯曼。它現在可以工作 – Bokbob