2016-10-04 28 views
0

我試圖反序列化使用C++/CLIC++/CLI類和IList的

[ 
    { 
     "id":"046e075ad92684", 
     "NfcA":{ 
      "maxTransceiveLength":253, 
      "sak":0, 
      "atqa":"4400", 
      "timeout":618 
     }, 
     "Ndef":[ 
      { 
       "records":[ 
        { 
         "id":"", 
         "tnf":1, 
         "type":"54", 
         "payload":"02656e48656c6c6f206d792041737365742049442069733a20303030303031" 
        } 
       ] 
      } 
     ], 
     "tech":[ 
      "android.nfc.tech.NfcA", 
      "android.nfc.tech.MifareUltralight", 
      "android.nfc.tech.Ndef" 
     ], 
     "time":1472468356002 
    } 
] 

以下JSON我已宣佈,以獲得JSON數據的內容,下面的類。

ref class tech { 
    public: String^ tech1; 
    public: String^ tech2; 
    public: String^ tech3; 
}; 

ref class Record { 
    public: String^ id; 
    public: int tnf; 
    public: String^ type; 
    public: String^ payload; 
}; 

ref class Topic_nfc { 
    public: String^ id; 
    public: ref class NfcA { 
    public: int maxTransceiveLength; 
    public: int sak; 
    public: int atqa; 
    public: int timeout; 
    }; 
    public: ref class Ndef { 
    public: System::Collections::Generic::IList<Record^>^ records; 
    }; 
    public: System::Collections::Generic::IList<Ndef^>^ Ndef; 
    public: System::Collections::Generic::IList<String^>^ tech; 
    public: unsigned long long time; 

    public: 
    NfcA^ NfcA; 
}; 

反序列化後,我可以訪問ID和maxTransceiveLength正常使用

printf("MyRawdata[i]->id : %s\n", MyRawdata[i]->id); 
printf("MyRawdata[i]->NfcA->maxTransceiveLength : %d\n", MyRawdata[i]->NfcA->maxTransceiveLength); 

其中MyRawdata

System::Collections::Generic::IList<Topic_nfc^>^ MyRawdata = JsonConvert::DeserializeObject<System::Collections::Generic::IList<Topic_nfc^>^>(MyJson); 

但是得到的,我想不出怎麼才能訪問Ndeftech數據會員如​​。你能指出哪一個是IList的等價物嗎?

謝謝

+0

使用'List'而不是'IList',Json.NET不知道應該使用哪個具體類來反序列化到接口。 –

+0

即使使用List來代替IList,我也不清楚哪一個是訪問這些數據的代碼。這將是'printf(「MyRawdata [i] - > id:%s \ n」,MyRawdata [i] - > id)的等價物;'爲了獲取tech1數據?這對我來說有點困惑。 – Fotakis

+0

我不認爲問題是'IList'。通過在Visual Studio中設置斷點,我可以通過VS的用戶界面評估'tech1'的值,並且這些值是正確的 - 因此可以使用'JsonConvert'很好地設置。問題是我如何引用MyRawdata結構來使用代碼訪問這些數據? – Fotakis

回答

0

由於@Lucas Trzesniewski的幫助,我的結論是,這些列表可以使用下面的代碼進行訪問。

for (int k = 0 ; k < MyRawdata[i]->Ndef->Count ; k++) { 
    printf("MyRawdata[i]->Ndef[k]->records[k]->payload : %s\n", MyRawdata[i]->Ndef[k]->records[k]->payload); 
} 

for (int k = 0 ; k < MyRawdata[i]->tech->Count ; k++) { 
    printf("MyRawdata[i]->tech[k] : %s\n", MyRawdata[i]->tech[k]); 
} 

實際上,訪問相應類中的數據是一種語法問題。