2016-01-06 135 views
4

我有一個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之前,我得到以下結果:

Watch Window

任何人都可以解釋爲什麼我無法訪問屬性?

更新
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}}} 
+3

循環內'mDevice'的實際類型是什麼,它是'JProperty'? – Codor

+0

是的,帽子是我的直接窗口告訴我的,但感覺不對......不應該是JPropertyList? –

+2

也許你應該訪問'mDevice.mobile_device.id'? –

回答

2

如果你看看Watch窗口中的Type列(你沒有完全顯示在你的文章中,並且它包含了一個非常重要的信息),你會看到mDevice的類型是Newtonsoft.Json.Linq.JProperty(它的btw也包含在例外消息中),而不是您所期望的Newtonsoft.Json.Linq.JObject。這又意味着dynamicList.mobile_devices不是您所期望的Newtonsoft.Json.Linq.JArray,實際上它是一個Newtonsoft.Json.Linq.JObject實例。所以整個邏輯是錯誤的。這是基於從帖子上的「原始JSON」的工作例如:

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; 

或可替代

foreach (var item in mobileDevices) 
{ 
    dynamic mobileDevice = item.Value; 
    var id = (int)mobileDevice.id; 
    var name = (string)mobileDevice.name; 
} 

作爲一般的建議,開始使用當地人/比其他值監視窗口功能。例如,有一種叫做「動態視圖」出現在動態支持擴展的對象,在這種情況下顯示的底部:

enter image description here

+0

謝謝您的簡要和明確的答案,這解釋了什麼問題! –

1

看的對象,更改此代碼它應該工作:

Id = mDevice.mobile_device.id, 
    Name = mDevice.mobile_device.name 

這是從你的觀察窗口清晰。


我沒有看到任何所謂mobile_devices - 你的意思mobile_device

您顯示的代碼不應該迭代循環,因爲該屬性名稱不應該存在。

+0

是的,因爲dynamicList.mobile_devices是'mobile_device'的列表。我在上面的代碼中留下了一個,因爲它不相關,我試圖訪問'mDevice' –

2

動態對象在這裏是一個字典(鍵值對)其中「mobile_device」是含有ID,名稱等密鑰和相應的對象是該鍵的值。

,你應該能夠訪問使用mobile_device爲重點的mDevice像mDevice [ 「mobile_device」。這應返回你的對象,又具有鍵值對(ID鍵和是價值鍵和iPad的值....)。再次對返回的對象使用相同的語法(鍵/值)以獲取相應的值。

+0

嘗試訪問其類似的mDevice [「mobile_device」]或mDevice [「mobile_device」]。鍵拋出異常:無法訪問孩子在Newtonsoft.Json.Linq.JProperty上的價值。 –

0

這可能是一個解決方案。

foreach (dynamic mDevice in dynamicList) 
{ 
    object mDeviceProperties = mDevice.Value; 

    var mobileDevice = JsonConvert.DeserializeObject<MobileDevice>(mDeviceProperties.ToString()); 
} 
+0

問題是:任何人都可以解釋爲什麼我無法訪問這些屬性?請看@Ivan Stoev如何知道答案應該看起來像...... –

+0

你只是反序列化mDevice到對象,這就是爲什麼你不能訪問屬性。必須將mDevice反序列化爲MobileDevice,然後才能訪問屬性。希望這至少能部分回答你的問題。 – Kos