2015-06-12 100 views
0

我將集合轉換爲JSON字符串。如何從JSON字符串中獲取值?

ReadOnlyCollection<Post> wall = api.Wall.Get(-group.Id.Value, out totalCount, count); 
string json = JsonConvert.SerializeObject(wall, Formatting.Indented); 

GET JSON字符串這樣

[ 
    { 
    "Id": 144159, 
    "Text": "Если", 
    "Likes": { 
     "Count": 9307, 
     "UserLikes": false, 
     "CanLike": true, 
     "CanPublish": true 
    }, 
    "PostType": "post", 
    "PostSource": { 
     "Type": "api", 
     "Data": null 
    }, 
    "Attachments": [ 
     { 
     "Instance": { 
      "AlbumId": -7, 
      "Photo75": "http://cs629506.vk.me/v629506780/3a56/Sn3eI5qEtc0.jpg", 
      "Photo130": "http://cs629506.vk.me/v629506780/3a57/tKRNTJlSH1s.jpg", 
      "Photo604": "http://cs629506.vk.me/v629506780/3a58/wyz6H7Py_8U.jpg", 
     }, 
     "Type": "VkNet.Model.Attachments.Photo, VkNet, Version=1.0.16.0, Culture=neutral, PublicKeyToken=null" 
     } 
    ] 

我怎麼可能會從 「Photo604」 的價值?我想要圖片的網址。請幫幫我!

+5

你爲什麼要序列化集合呢?爲什麼不只是得到'wall [0] .Attachments [0] .Instance.Photo604'? –

+0

你可以顯示「Post」類型嗎? –

+0

是的,它是郵政類型 –

回答

1

您需要先投實例的照片是這樣的:

var photo = wall[0].Attachments[0].Instance as Photo 
if (photo != null) 
    var uri = photo.Photo604 

當然沒有錯誤檢查在這裏。您應該在訪問之前檢查空值和數組長度,否則您可能會收到空引用異常。