简体   繁体   English

使用JavaScriptSerializer将实体映射到JSON

[英]Map entity to JSON using JavaScriptSerializer

My entities are like this: 我的实体是这样的:

class Address
{
     public string Number { get; set; }
     public string Street { get; set; }
     public string City { get; set; }
     public string Country { get; set; }
}

class Person
{
     public string Name { get; set; }
     public int Age { get; set; }
     public Address PostalAddress { get; set; }
}

Person newPerson = 
    new Person()
    {
       Name = "Kushan",
       Age = 25,
       PostalAddress = 
           new Address()
           {
               Number = "No 25",
               Street = "Main Street",
               City = "Matale",
               Country = "Sri Lanka"
           }
    };

Now I wanna map this newPerson object into JSON object like this, 现在我想将这个newPerson对象映射到这样的JSON对象,

{ 
     "PER_NAME" : "Kushan",
     "PER_AGE" : "25",
     "PER_ADDRESS" : {
                          "ADD_NUMBER" : "No 25",
                          "ADD_STREET" : "Main Street",
                          "ADD_CITY" : "Matale",
                          "ADD_COUNTRY" : "Sri Lanka"
                     }
}

Note: Above is just an example. 注意:上面只是一个例子。

What I need is, I need to customize the Key at the serializing time. 我需要的是,我需要在序列化时自定义Key。 by default it is taking property name as the key. 默认情况下,它将属性名称作为键。 I can't change property names. 我无法更改属性名称。 How to do this? 这该怎么做?

Also, is it possible to change to order of appearing key-value pairs in JSON obj.? 此外,是否可以更改为JSON obj中出现的键值对的顺序?

You need to add DataContract attributes to your classes and DataMember to the properties. 您需要将DataContract属性添加到类,将DataMember到属性。 Set Name property of DataMemeber attribute to your custom property name and Order property to define the order. DataMemeber属性的Name属性设置为自定义属性名称,将Order属性设置为定义顺序。

[DataContract]
public class Person
{
    [DataMember(Name = "PER_NAME", Order = 1)]
    public string Name { get; set; }

    [DataMember(Name = "PER_AGE", Order = 2)]
    public int Age { get; set; }

    [DataMember(Name = "PER_ADDRESS", Order = 3)]
    public Address PostalAddress { get; set; }
}

Then you can do this: 然后你可以这样做:

var newPerson = new Person()
{
    Name = "Kushan",
    Age = 25,
    PostalAddress = new Address()
    {
        Number = "No 25",
        Street = "Main Street",
        City = "Matale",
        Country = "Sri Lanka"
    }
};

MemoryStream stream = new MemoryStream();
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Person));
ser.WriteObject(stream, newPerson);

To check the result: 要检查结果:

var result = Encoding.ASCII.GetString(stream.ToArray());

{"PER_NAME":"Kushan","PER_AGE":25,"PER_ADDRESS":{"ADD_NUMBER":"No 25","ADD_STREET":"Main Street","ADD_CITY":"Matale","ADD_COUNTRY":"Sri Lanka"}}

您可以使用JavaScriptSerializer序列化匿名类型,因此您可以尝试将对象投影到要序列化的形状中:

person.Select(s => new { PER_NAME = s.Name, PER_AGE = s.Age });

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

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