JSON SerializeObject,自定义json字符串的格式

时间:2012-09-05 09:31:06

标签: c# json serialization

我有一个返回json对象的wcf方法。

public String GetProductItmes()
{
    SetupInvitationController _InvitationController = new SetupInvitationController();
    var json = _InvitationController.ListingProduct_JSONString();

    log.Debug("Serialized " + JsonConvert.SerializeObject(json, Formatting.Indented));

    return JsonConvert.SerializeObject(json, Formatting.Indented);
}

日志输出

2012-09-05 16:29:07,971 [8] DEBUG Service [(null)] <(null)> - Serialized [
{
"ID": "72482e23-958c-4712-a273-672e6d637e46",
"ProductName": "Accommodation 1",
"FieldMasterID": "5b6f9940-d27d-4733-b378-a29ce8759945",
"FieldMasterName": "FieldAccommodationMaster1",
"Display": "Field Accommodation Master 1",
"ControlType": "Dropdown",
"Value": "1",
"Description": "Desc 1",
"Sequence": 5
},
{
"ID": "72482e23-958c-4712-a273-672e6d637e46",
"ProductName": "Accommodation 1",
"FieldMasterID": "5b6f9940-d27d-4733-b378-a29ce8759945",
"FieldMasterName": "FieldAccommodationMaster1",
"Display": "Field Accommodation Master 1",
"ControlType": "Dropdown",
"Value": "2",
"Description": "Desc 2",
"Sequence": 5
}, ......... ]

到目前为止,一切都是正确的 但是JSON格式不是我之前的预期。

我想得到的JSON格式如下......

{
"ProductItems" : 
    [
        {
            "ID": "82FAD3C5-043C-4DAE-84C4-FCA500C22FEE",
            "productName": "Meal 2",                        
            "FieldMaster" : 
            [
                {
                    "FieldMasterID": "B8BDEB69-1FB1-4C07-A0F0-2182B7A4FC25",
                    "FieldMasterName": "FieldMaster1",
                    "Display": "Field Meal Master 1",
                    "ControlType": "Dropdown",
                    "FieldMasterValue" : 
                    [
                        {
                            "Value": "1",
                            "Description": "Desc 1" 
                        }, 
                        {
                            "Value": "2",
                            "Description": "Desc 2"
                        },
                        {
                            "Value": "3",
                            "Description": "Desc 3"
                        }
                    ] 
                }
            ] 
        },
        {
            "ID": "DDDB5286-6E10-4221-B0A4-13185EB31BB6",
            "productName": "Meal 3",                        
            "FieldMaster" : 
            [
                {
                    "FieldMasterID": "B8BDEB69-1FB1-4C07-A0F0-2182B7A4FC25",
                    "FieldMasterName": "FieldMaster1",
                    "Display": "Field Meal Master 1",
                    "ControlType": "Dropdown",
                    "FieldMasterValue" : 
                    [
                        {
                            "Value": "1",
                            "Description": "Desc 1" 
                        }, 
                        {
                            "Value": "2",
                            "Description": "Desc 2"
                        },
                        {
                            "Value": "3",
                            "Description": "Desc 3"
                        }
                    ] 
                }, 
                {
                    "FieldMasterID": "82D2256E-6B20-416F-B27B-EB1DB97E7E0F",
                    "FieldMasterName": "FieldMaster2",
                    "ControlType": "Dropdown",
                    "FieldMasterValue" : 
                    [
                        {
                        "Value": "1",
                        "Description": "Desc 1" 
                        }, 
                        {
                        "Value": "2",
                        "Description": "Desc 2"
                        }
                    ] 
                }
            ]
        }
    ]
}

所以现在我创建了实体类。

ProductItem类

namespace Entity
{
[DataContract]
public class ProductItem
{
    [DataMember]
    public string ID
    {
        get;
        set;
    }

    [DataMember]
    public string ProductName
    {
        get;
        set;
    }

    [DataMember]
    public List<FieldMaster> FieldMasters
    {
        get;
        set;
    }

    public ProductItem()
    {
        FieldMasters = new List<FieldMaster>();
    }
}
}

FieldMaster课程

namespace Entity
{
[DataContract]
public class FieldMaster
{   
    [DataMember]
    public string FieldMasterID
    {
        get;
        set;
    }

    [DataMember]
    public string FieldMasterName
    {
        get;
        set;
    }

    [DataMember]
    public string Display
    {
        get;
        set;
    }

    [DataMember]
    public string ControlType
    {
        get;
        set;
    }

    [DataMember]
    public List<FieldMasterValue> FieldMasterValues
    {
        get;
        set;
    }

    public FieldMaster()
    {
        FieldMasterValues = new List<FieldMasterValue>();
    }
}
}

FieldValueMaster类

namespace Entity
{
[DataContract]
public class FieldMasterValue
{   
    [DataMember]
    public string Value
    {
        get;
        set;
    }

    [DataMember]
    public string Description
    {
        get;
        set;
    }
}
}

最后,我想知道接下来应该做些什么。

我将不胜感激,可以告诉我如何按照我的方式序列化json。

0 个答案:

没有答案