在json中提取特定键的数据

时间:2014-02-07 17:14:26

标签: c# json

我向Facebook发送API图表查询,响应是一个巨大的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,

如您所见,“图片”和“来源”之后有一个链接。我怎样才能获得所有这些链接?

感谢。

1 个答案:

答案 0 :(得分: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);