我有一个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'
的定义
如果我在Watch Window
发生之前使用mDevice
输出Exception
,我会得到以下结果:
任何人都可以解释为什么我无法访问这些属性吗?
的更新
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}}}
答案 0 :(得分:2)
此处的动态对象是字典(键值对),其中" mobile_device "是一个键和对应的对象,包含 id,name 等是该键的值。
你应该可以使用 mobile_device 作为mDevice上的键来访问它,例如mDevice [" mobile_device "]。这应该返回一个对象,它再次具有键值对( id 键和 9 是值,名称键和 ipad 是价值......)。再次对返回的对象使用相同的语法(键/值)来获取相应的值。
答案 1 :(得分:2)
如果您查看Watch窗口中的Type列(您的帖子中没有完整显示,并且它包含非常重要的信息),您会看到mDevice
的类型是{ {1}}(btw也是异常消息中暗示的)而不是Newtonsoft.Json.Linq.JProperty
。这反过来意味着Newtonsoft.Json.Linq.JObject
不是您期望的dynamicList.mobile_devices
,实际上它是Newtonsoft.Json.Linq.JArray
实例。所以整个逻辑是错误的。这是一个基于帖子的“原始JSON”的工作示例:
Newtonsoft.Json.Linq.JObject
或者
string originalJSON = ...;
dynamic root = JsonConvert.DeserializeObject(originalJSON);
dynamic mobileDevices = root.mobile_device_application.scope.mobile_devices;
dynamic mobileDevice = mobileDevices.mobile_device;
var id = (int)mobileDevice.id;
var name = (string)mobileDevice.name;
作为一般建议,请开始使用除值之外的Locals / Watch窗口功能。例如,有一个名为“动态视图”的东西出现在展开对象的底部并带有动态支持,在这种情况下显示:
答案 2 :(得分:1)
查看对象,更改为此代码应该有效:
Id = mDevice.mobile_device.id,
Name = mDevice.mobile_device.name
从您的观察窗口可以清楚地看到这一点。
我没有看到任何名为mobile_devices
的内容 - 您的意思是mobile_device
吗?
您显示的代码不应该迭代循环,因为该属性名称不应该存在。
答案 3 :(得分:0)
这可能是一个解决方案。
foreach (dynamic mDevice in dynamicList)
{
object mDeviceProperties = mDevice.Value;
var mobileDevice = JsonConvert.DeserializeObject<MobileDevice>(mDeviceProperties.ToString());
}