这是反序列化Facebook JSON字符串的好方法吗?

时间:2011-12-28 18:40:27

标签: c# json facebook-graph-api

这是我第一次尝试反序列化Facebook返回的JSON字符串 我想确保未来的开发人员可以舒适地维护我的代码,所以我想知道这是否可行。我担心如果JSON字符串改变,那么我将需要重写我的一些类 从Facebook返回的JSON字符串可能会发生变化吗?例如,如果位置成为不同的对象,我将需要进行更改吗?

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Web;
//--- If you are not able to add reference dont worry. You probably need to install ASP.NET Ajax or target higher .NET Framework (3.5 or 4).
//---   There are Stack Overflow threads about this.
using System.Web.Script.Serialization;

//--- Our Facebook Person class.
public class FBPerson
{
    public string id { get; set; }
    public string email { get; set; }
    public string name { get; set; }
    public string gender { get; set; }
    public IDName location { get; set; }
    public List<FBObject> work { get; set; }
    public List<FBObject> education { get; set; }
}
//-- In some cases only id and name will be accessed
public class IDName
{
    public string id { get; set; }
    public string name { get; set; }
    public string type { get; set; }
}
//-- work and education presently are array of json strings
public class FBObject
{
    public IDName employer { get; set; }
    public IDName school { get; set; }
}
static class Program
{
    //-- Sample JSON string returned by Facebook
    const string json2 = @"{
        ""id"":""11111111111111111"",
        ""name"":""Tester Test"",
        ""first_name"":""Tester"",
        ""last_name"":""Test"",
        ""link"":""http:\/\/www.facebook.com\/profile.php?id=11111111111111111"",
        ""location"":
            {""id"":""107991659233606"",""name"":""Atlanta, Georgia""},
        ""work"":
            [{""employer"":{""id"":""222222222222222222"",""name"":""Various""}}],
        ""education"":
            [
                {""school"":{""id"":""108000000000000"",""name"":""Test High School""},""type"":""High School""},
                {""school"":{""id"":""105000000000000"",""name"":""Tester College""},""type"":""College""}
            ],
        ""gender"":""male"",
        ""email"":""tester\u0040gmail.com"",
        ""timezone"":-5,""locale"":""en_US"",
        ""verified"":true,
        ""updated_time"":""2011-11-21T21:10:20+0000""
    }";

    static void Main()
    {
        JavaScriptSerializer ser = new JavaScriptSerializer();
        FBPerson fperson = ser.Deserialize<FBPerson>(json2);

        //-- Display the user info from the JSON string
        Console.WriteLine(fperson.id.ToString());
        Console.WriteLine(fperson.name.ToString());
        Console.WriteLine(fperson.gender.ToString());
        Console.WriteLine(fperson.email.ToString());
        Console.WriteLine(fperson.location.name.ToString());
        Console.WriteLine(fperson.work[0].employer.name.ToString());
        Console.WriteLine(fperson.education[0].school.name.ToString());
        Console.ReadLine();
    }
}

2 个答案:

答案 0 :(得分:2)

这似乎很好。无论你如何进行反序列化,对JSON数据格式的更改都会让你失望。出于这个原因,API作者非常努力避免进行此类更改。

答案 1 :(得分:2)

我不知道您是否对其他方法持开放态度,但如果您想尝试, 这是一种不需要FBPerson IDName FBObject类的替代方法,并使用Json.Netthis extension methods

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

static class Program
{
    //-- Sample JSON string returned by Facebook
    const string json2 = @"{
        ""id"":""11111111111111111"",
        ""name"":""Tester Test"",
        ""first_name"":""Tester"",
        ""last_name"":""Test"",
        ""link"":""http:\/\/www.facebook.com\/profile.php?id=11111111111111111"",
        ""location"":
            {""id"":""107991659233606"",""name"":""Atlanta, Georgia""},
        ""work"":
            [{""employer"":{""id"":""222222222222222222"",""name"":""Various""}}],
        ""education"":
            [
                {""school"":{""id"":""108000000000000"",""name"":""Test High School""},""type"":""High School""},
                {""school"":{""id"":""105000000000000"",""name"":""Tester College""},""type"":""College""}
            ],
        ""gender"":""male"",
        ""email"":""tester\u0040gmail.com"",
        ""timezone"":-5,""locale"":""en_US"",
        ""verified"":true,
        ""updated_time"":""2011-11-21T21:10:20+0000""
    }";

    public static void Main()
    {

        dynamic fperson = JsonUtils.JsonObject.GetDynamicJsonObject(json2);

        //-- Display the user info from the JSON string
        Console.WriteLine(fperson.id.ToString());
        Console.WriteLine(fperson.name.ToString());
        Console.WriteLine(fperson.gender.ToString());
        Console.WriteLine(fperson.email.ToString());
        Console.WriteLine(fperson.location.name.ToString());
        Console.WriteLine(fperson.work[0].employer.name.ToString());
        Console.WriteLine(fperson.education[0].school.name.ToString());
        Console.ReadLine();
    }
}