JavaScriptSerializer问题解析反序列化的json

时间:2015-07-23 17:44:18

标签: c# json

我有以下程序,我试图从json中使用javascriptserializer发送的WebRequest响应中提取特定值来解析值。以下是代码:

       public class Self
    {
        public string href { get; set; }
    }

    public class User
    {
        public string href { get; set; }
    }

    public class Xframe
    {
        public string href { get; set; }
    }

    public class Links
    {
        public Self self { get; set; }
        public User user { get; set; }
        public Xframe xframe { get; set; }
    }

    public class RootObject
    {
        public Links _links { get; set; }
    }


    public class Foo
    {
        public Foo() { objects = new List<RootObject>(); }
        public Links Self { get; set; }
        public Links User { get; set; }
        public Links Xframe { get; set; }
        public List<RootObject> objects { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {

            string sURL;
            sURL = "<replacing value>";
            WebRequest wrGETURL;
            wrGETURL = WebRequest.Create(sURL);
            Stream objStream;
            objStream = wrGETURL.GetResponse().GetResponseStream();
            StreamReader objReader = new StreamReader(objStream);

            string sLine = "";
            string uLine = "";

                sLine = objReader.ReadLine();
                if (sLine != null)
                    uLine = sLine;
                    //Console.WriteLine(uLine);

                JavaScriptSerializer ser = new JavaScriptSerializer();
                Foo foo = ser.Deserialize<Foo>(uLine);
                Console.WriteLine(foo);

当我尝试编写foo输出并查看foo对象的属性时,所有属性都为null。我相信我的问题在Foo班,但我似乎无法理解我所缺少的。任何帮助都感激不尽。我使用jsontocsharp为反序列化的json字符串创建类。

1 个答案:

答案 0 :(得分:0)

将数据解析为JSON对象使我能够做自己需要的事情。