2016-10-04 73 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]); 
} 

实际上,访问相应类中的数据是一种语法问题。