如何将控件序列化为JSON? [英] How to Serialize a Control to JSON?

查看:151
本文介绍了如何将控件序列化为JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经将Control信息存储在XML文件中,就像这样:

I used to store Control information in an XML file, like so:

<Controls>
    <Label Id="heading" Text="This is a heading!" FontStyle="(FontStyleDataHere)" Location="20, 10" />
    <Label Id="bodyText" Text="This is Body text." FontStyle="(FontStyleDataHere)" Location="20, 70" />
</Controls>

我一直在寻找去年拥有的多页代码的印刷版本,这是我留下的唯一备份,现在找不到.

I have been looking for a printed version of many pages of code that I had last year, and it was the only backup I had left and cannot find it now.

并且由于我不记得自己是怎么做到的,所以我总是觉得XML非常繁琐.所以我想,为什么不去尝试一下JSON.似乎有点容易...

And since I cannot remember how the heck I did this, I always fels that XML was incredibly tedious. So I thought, why not give JSON a go. It seems a bit easier...

现在,根据上面的代码,我能够创建一个类型为Person的类,并将该对象序列化并将其写入文件(或Console或其他):

Now, given the above code, I was able to create a class of type Person, and Serialize the object and write it to the file (or Console - whatever):

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;

namespace SerializeToJson
{
    class Program
    {
        [DataContract]
        internal class Person
        {
            [DataMember]
            internal String Name;

            [DataMember]
            internal Int32 Age;
        }

        static void Main(string[] args)
        {
            Person person = new Person()
            {
                Name = "Jason rules.",
                Age = 19
            };

            MemoryStream stream = new MemoryStream();
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Person));
            serializer.WriteObject(stream, person);

            stream.Position = 0;

            StreamReader reader = new StreamReader(stream);
            Console.Write("Json form of Person object: ");
            Console.WriteLine(reader.ReadToEnd());

            Console.ReadKey();
        }
    }
}

但是问题是,我不知道如何将控件序列化为Json.这就是我真正需要的.而且,显然,我需要在以后的某个时间点对它们进行反序列化,以便可以在运行时重新创建它们.

But the problem is, I do not know how to Serialize Controls to Json. And this is what I really need. And, obviously, I will need to then at a later point in time, deserialize them, so they can be re-created at runtime.

可以使用JSON完成此操作,还是建议我仍然使用XML?

Can this be done, with JSON, or would you recommend I still use XML for this?

推荐答案

好,我在这里了解了什么,我试图给出答案,您可以为xml对象创建等效的类.

OK what I understand here, I am trying to give the answer, You can create equivalent classes for your xml objects.

标签对象的等效类

Public class Lable
{
   [DataMember]
    String Id{get;set;};

    [DataMember]
     String Text {get;set;};

    [DataMember]
    string FontStyle{get;set;}

}

控制对象的等效类

Public class Controls
{
   [DataMember]
   public List<Lable> Lables{ get;set; }
}

现在,您只需要序列化包含多个Lable

Now you just need to Serialize the Controls object that contains multiple Lable

现在您可以使用JavaScriptSerializer这样简单地序列化对象

Now you can serialize object simple using JavaScriptSerializer like this

JavaScriptSerializer serializer = new JavaScriptSerializer();

序列化:

 string jsonString = serializer.Serialize(objControls);

反序列化:

Controls objControls=serializer.Deserialize<Controls>(jsonString);

这篇关于如何将控件序列化为JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆