我有一個dynamic
對象,我無法訪問它的屬性。無法訪問動態屬性
爲了便於閱讀,我修改了下面的代碼。
我使用的包:Newtonsoft.Json.8.0.1\lib\net45\Newtonsoft.Json.dll
我試圖讀取對象mDevice
在下面的代碼:
代碼
foreach (dynamic mDevice in dynamicList.mobile_devices)
{
MobileDevice mobileDevice = new MobileDevice() //====> Throws Exception
{
Id = mDevice.id,
Name = mDevice.name
};
}
的Exception
是如下:
「Newtonsoft.Json.Linq.JProperty」不包含「身份證」
如果我用一個定義我Watch Window
輸出mDevice
發生Exception
之前,我得到以下結果:
任何人都可以解釋爲什麼我無法訪問屬性?
更新
dynamicList
來源:
忽略爲什麼我轉換XML轉換成JSON這有其他不相干的目的
string MobileDevicesInJSON = JsonConvert.SerializeXmlNode(doc);
dynamic dynamicList = JsonConvert.DeserializeObject(MobileDevicesInJSON);
原始JSON的原因:
{"?xml":{"@version":"1.0","@encoding":"UTF-8"},"mobile_device_application":{"general":{"id":"5","name":"Acta Dome Calculator - Free","display_name":"Acta Dome Calculator - Free","description":null,"bundle_id":"com.itwcalculator.calculatorforipadfree","version":"3.1.1","internal_app":"true","category":{"id":"-1","name":"No category assigned"},"ipa":{"name":null,"uri":null,"data":null},"icon":null,"mobile_device_provisioning_profile":null,"url":{"@deprecated":"9.4","#text":"https://itunes.apple.com/nl/app/calculator-free/id398129933?mt=8&uo=4"},"itunes_store_url":"https://itunes.apple.com/nl/app/calculator-free/id398129933?mt=8&uo=4","deployment_type":"Install Automatically/Prompt Users to Install","deploy_automatically":"true","deploy_as_managed_app":"true","remove_app_when_mdm_profile_is_removed":"false","prevent_backup_of_app_data":"false","keep_description_and_icon_up_to_date":"false","free":"true","take_over_management":"false","host_externally":"true","external_url":"https://itunes.apple.com/nl/app/calculator-free/id398129933?mt=8&uo=4","site":{"id":"1","name":"Acta Dome"}},"scope":{"all_mobile_devices":"false","all_jss_users":"false","mobile_devices":{"mobile_device":{"id":"9","name":"iPad R&D 01S","udid":"dd1dff5d598e3fce0b4b16288f0b9bf1551d0eb2","wifi_mac_address":"9C:35:EB:53:00:84"}},"mobile_device_groups":{"mobile_device_group":{"id":"9","name":"Acta Dome Unassigned"}},"buildings":null,"departments":null,"jss_users":{"user":[{"id":"9","name":"ACTA_Astrid"},{"id":"7","name":"ACTA_RenD01"}]},"jss_user_groups":{"user_group":{"id":"7","name":"Acta Dome StudentGroup 01"}},"limit_to_users":{"user_groups":null},"network_limitations":{"any_ip_address":"true","network_segments":null},"limitations":{"users":null,"user_groups":null,"network_segments":null},"exclusions":{"mobile_devices":null,"mobile_device_groups":null,"buildings":null,"departments":null,"jss_users":null,"jss_user_groups":null,"users":null,"user_groups":null,"network_segments":null}},"self_service":{"self_service_description":null,"self_service_icon":null,"feature_on_main_page":"false","self_service_categories":null}}}
循環內'mDevice'的實際類型是什麼,它是'JProperty'? – Codor
是的,帽子是我的直接窗口告訴我的,但感覺不對......不應該是JPropertyList? –
也許你應該訪問'mDevice.mobile_device.id'? –