简体   繁体   English

将对象转换为字符串

[英]Converting object to string

Here's the content of my object: 这是我的对象的内容:

-       tree    {ItemTree}  ItemTree
        id  "0" string
        im0 null    string
    -       item    Count = 1   System.Collections.Generic.List<ItemTree>
    -       [0] {ItemTree}  ItemTree
            id  "F_1"   string
            im0 "something.gif" string
    +       item    Count = 16  System.Collections.Generic.List<ItemTree>
                 parentId   "0" string
                 text   "someName"  string
  +                  Raw View       
                 parentId   null    string
                 text   ""  string

And I build it dynamically, so it's bigger. 而且我是动态构建的,因此更大。

It is an object from this class: 它是此类的一个对象:

public class ItemTree
{
    public String id { get; set; }

    public String text { get; set; }

    public List<ItemTree> item { get; set; }

    public string im0 { get; set; }

    public String parentId { get; set; }
}

So, the class ItemTree has a property which itself is a List of ItemTree objects. 因此,类ItemTree具有一个属性,该属性本身就是ItemTree对象的列表。

I want to convert this to string. 我想将其转换为字符串。 When I make: 当我做:

tree.ToString()

I only get: 我只会得到:

        tree.ToString() "ItemTree"  string

But I want to convert the whole tree structure to string. 但是我想将整个树结构转换为字符串。 How to do this? 这个怎么做?

You need to override the ToString() method in your class. 您需要在类中重写 ToString()方法。

When you create a custom class or struct, you should override the ToString method in order to provide information about your type to client code. 创建自定义类或结构时,应重写ToString方法,以便将有关类型的信息提供给客户端代码。

You can use XmlSerializer to serialize your object to XML. 您可以使用XmlSerializer将对象序列化为XML。

You need to override the ToString method and print your tree representation there 您需要重写ToString方法并在其中打印树的表示形式

public class ItemTree
{
   public override string ToString()
   {
      return "Tree " + id +....
   }
}

otherwise you will always see class name as result of base ToString() 否则,您将始终看到类名是基于基础ToString()的结果

You can Override the ToString Method of your class ItemTree 您可以重写类ItemTreeToString方法

Or May be you can try with serializing with json-net 或者也许您可以尝试使用json-net进行序列化

string json = JsonConvert.SerializeObject(tree);

If you override the ToString method, your implementation will be used by other code that calls ToString, simply because it's a standard method (inherited from Object). 如果重写ToString方法,则其他实现将被其他调用ToString的代码使用,这仅仅是因为它是一种标准方法(从Object继承)。

Optionally you can implement a new method. (可选)您可以实现新方法。

Either way, to avoid manually updating your method, you can generate a string using Json.Net like this: 无论哪种方式,为了避免手动更新方法,都可以使用Json.Net生成一个字符串,如下所示:

string str = JsonConvert.SerializeObject(someObject);

Here is a sample from the documentation : 这是文档中的示例:

Product product = new Product();

product.Name = "Apple";
product.ExpiryDate = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };

string output = JsonConvert.SerializeObject(product);
//{
//  "Name": "Apple",
//  "ExpiryDate": "2008-12-28T00:00:00",
//  "Price": 3.99,
//  "Sizes": [
//    "Small",
//    "Medium",
//    "Large"
//  ]
//}

Product deserializedProduct = JsonConvert.DeserializeObject<Product>(output);

Nuget package: http://nuget.org/packages/Newtonsoft.Json/ Nuget软件包: http ://nuget.org/packages/Newtonsoft.Json/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM