2014-02-07 18 views
0

我向Facebook發送一個API圖形查詢,該響應是一個巨大的JSON響應。在json中爲特定鍵提取數據

這裏是JSON的一小部分:

"id": "100005146959001", 
"albums": { 
    "data": [ 
     { 
     "id": "246858672162363", 
     "created_time": "2014-02—04T23:23:13+0000" 
     }, 
     { 
      "id": "223430357838528", 
      "created_time": "2013-12-08T20:23:10+0000", 
      "photos": { 
       "data": [ 
        { 
         "id": "242389792609251", 
         "from": { 
          "name": "Shamsi Farrokhi", 
          "id": "lO0005146959001" 
         },  
         "picture": "https://fbcdn-photos-g-a.akamaihd.net/hphotos-ak-ask 
         "source": "https://fbcdn—sphotos—g—a.akamaihd.net/hphotos—ak—asr 
         "height": 540, 
         "width": 720,  
         "images": [ 
          { 
           "height": 1536, 
           "width": 2048, 
           "source": "https://fbcdn—sphotos-g—a.akamaihd.net/hphotos— 
          }, 
          { 
           "height": 960, 

正如你看到的,有「圖片」和「源」後的鏈接。我怎樣才能獲得所有這些鏈接?

謝謝。

+4

爲什麼要粘貼圖片而不是純文本? – Barmar

+0

上週有類似要求,我用Json.Net框架。 https://json.codeplex.com/releases 它很容易使用,效果很好... – Ofir

+0

除了@Ofir的建議,Json.NET也可以用Linq查詢,這可能會幫助您獲得到鏈接值更容易:http://james.newtonking.com/json/help/index.html – Quintium

回答

0

How to: Serialize and Deserialize JSON Data

在.NET 4.0中,有standard tools for working with JSON。作爲選項 - DataContractJsonSerializer。這是一個使用它的例子。描述數據結構:

[DataContract] 
public class ProfileType 
{ 
    [DataMember] 
    public int ProfileTypeIDT { get; set; } 
    [DataMember] 
    public string SingularName { get; set; } 
    [DataMember] 
    public string PluralName { get; set; } 
    [DataMember] 
    public ProfileField[] Fields { get; set; } 
} 

[DataContract] 
public class ProfileField 
{ 
    [DataMember] 
    public int ProfileFieldIDT { get; set; } 
    [DataMember] 
    public int ProfileTypeIDT { get; set; } 
    [DataMember] 
    public string FieldName { get; set; } 
    [DataMember] 
    public string DataType { get; set; } 
    [DataMember] 
    public int Length { get; set; } 
} 

byte[] byteArray = Encoding.Unicode.GetBytes(jsonString); 
MemoryStream stream = new MemoryStream(byteArray); 
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ProfileType[])); 
ProfileType[] profileTypes = (ProfileType[])serializer.ReadObject(stream);