【发布时间】:2016-04-10 17:49:38
【问题描述】:
我有一个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”不包含“id”的定义
如果我在Exception 发生之前使用我的Watch Window 输出mDevice,我会得到以下结果:
谁能解释我为什么无法访问这些属性?
更新dynamicList 来源:
IGNORE我将 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吗? -
也许你应该访问
mDevice.mobile_device.id? -
在 Locals 或 Watch 窗口中展开
mDevice变量,看看里面有什么。立即窗口不利于显示结构。 -
@NickProzee:给我们一些 JSON 来重现问题。几乎可以肯定,问题是反序列化没有完成您的预期,但是如果没有可测试的可重现版本的错误,很难确定您的问题出在哪里。我试图自己重现你的问题,它对我来说很好,所以它很难帮助......
-
我为需要的人添加了原始 JSON