如何解析在MVC Controller

时间:2017-09-20 14:47:51

标签: c# asp.net json http model-view-controller

我有一个JSON文件,通过来自客户端的POST请求发送到MVC控制器。由于没有直接的方法在C#中存储JSON,我试图创建模型来存储数据。这是JSON的结构:

{
    "SubscriptionsType1": {
        "Obj1": {
            "Value1": "3454234",
            "Value2": "345643564",
            "Value3": "665445",
            "Value4": "True"
        },
        "Obj2": {
            "Value1": "3454234",
            "Value2": "345643564",
            "Value3": "665445",
            "Value4": "True"
        },
        "Obj3": {
            "Value1": "3454234",
            "Value2": "345643564",
            "Value3": "665445",
            "Value4": "True"
        }
    },
"SubscriptionsType2": {
                "Obj1": {
                    "Value1": "3667635",
                    "Value2": "34563456",
                    "Value3": "234545",
                    "Value4": "False"
                },
                "Obj2": {
                    "Value1": "97865",
                    "Value2": "356356",
                    "Value3": "665757445",
                    "Value4": "True"
                },
                "Obj3": {
                    "Value1": "3454234",
                    "Value2": "345643564",
                    "Value3": "665445",
                    "Value4": "False"
                }
            },
    //etc..

    }

模型如下:

  public class RootObject {
        public IEnumerable<SubscriptionObj> Subscriptions { get; set; }
}


public class Subscriptions {
    IEnumerable<SubscriptionObj> objs;
}


public class SubscriptionObj
{
    public Int64 Value1 {get;set;}
    public Int64 Value2 {get;set;}
    public Int64 Value3 {get;set;}
    public Boolean Value4 {get;set;}
}

出于某种原因,如果我使用此结构创建模型类,则从控制器传递JSON对象后,根对象仍为null。 但是,当我显式创建属性来存储JSON对象的每个值时,它可以正常工作。由于JSON对象没有固定数量的元素,因此我需要一种动态存储每个属性的方法。 如何做到这一点?

4 个答案:

答案 0 :(得分:4)

由于根对象和订阅中的键不同,您应该使用字典。

public class RootObject : Dictionary<string, Subscriptions> { }

public class Subscriptions : Dictionary<string, SubscriptionObj> { }

public class SubscriptionObj {
    public Int64 Value1 {get;set;}
    public Int64 Value2 {get;set;}
    public Int64 Value3 {get;set;}
    public Boolean Value4 {get;set;}
}

上面会序列化到OP中的JSON,但你松开强类型的键/属性名称

答案 1 :(得分:2)

与你的c#类对应的json应该使用数组,比如

{
    "Subscriptions": [ 
    {  "Obj": [   
        {
            "Value1": "3454234",
            "Value2": "345643564",
            "Value3": "665445",
            "Value4": "True"
        },
        {
            "Value1": "3454234",
            ...
        },
        {
            "Value1": "3454234",
            ...
        } ] 
    },  //end of Subscription 1
    {  "Obj": [   
        {
            "Value1": "3454234",
            ...
        },
        {
            "Value1": "3454234",
            ...
        },
        {
            "Value1": "3454234",
            ...
        } ] 
    }  //end of Subscription 2
    ] 
}

对应于没有数组的json的c#类看起来像:

public class ObjType
{
    public string Value1 { get; set; }
    public string Value2 { get; set; }
    public string Value3 { get; set; }
    public string Value4 { get; set; }
}

public class SubscriptionsType
{
    public ObjType Obj1 { get; set; }
    public ObjType Obj2 { get; set; }
    public ObjType Obj3 { get; set; }
}

public class RootObject
{
    public SubscriptionsType SubscriptionsType1 { get; set; }
    public SubscriptionsType SubscriptionsType2 { get; set; }
}

答案 2 :(得分:2)

对于该数据结构,我将对象反序列化为动态。然后你可以检查是否有“SubscriptionsType1”或“SubscriptionsType2”,...

使用动态我会创建模型类并将它们填充到自酿的反序列化方法/类中。

模型的设计不是序列化友好的,您可以通过更改与调用者的合同来更改您将获得的json吗?

如果有的话,这是我的建议:

{ "Subscriptions": [
    {
    "Objects": [{
        "Value1": "3454234",
        "Value2": "345643564",
        "Value3": "665445",
        "Value4": "True"
    },
    {
        "Value1": "3454234",
        "Value2": "345643564",
        "Value3": "665445",
        "Value4": "True"
    },
    {
        "Value1": "3454234",
        "Value2": "345643564",
        "Value3": "665445",
        "Value4": "True"
    }
    ]
},
{
            "Objects": [
            {
                "Value1": "3667635",
                "Value2": "34563456",
                "Value3": "234545",
                "Value4": "False"
            },
            {
                "Value1": "97865",
                "Value2": "356356",
                "Value3": "665757445",
                "Value4": "True"
            },
            {
                "Value1": "3454234",
                "Value2": "345643564",
                "Value3": "665445",
                "Value4": "False"
            }]
        },
//etc..
]
}

您的模型将更改为:

public class RootObject {
        public Subscriptions SubscriptionCollection { get; set; }
}


public class Subscriptions {
    IEnumerable<SubscriptionObj> objs;
}

public class SubscriptionObj
{
    public Int64 Value1 {get;set;}
    public Int64 Value2 {get;set;}
    public Int64 Value3 {get;set;}
    public Boolean Value4 {get;set;}
}

是你想要得到的?

答案 3 :(得分:0)

从您的JSON文件中,我们可以提取以下模型类:

public class RootModel{
    public SubscriptionTypeModel SubscriptionsType1 { get; set; }

    public SubscriptionTypeModel SubscriptionsType2 { get; set; }

    //etc...
}

public class SubscriptionTypeModel {
    public ObjectModel Obj1 { get; set; }
    public ObjectModel Obj2 { get; set; }
    public ObjectModel Obj3 { get; set; }
}

public class ObjectModel {
    public string Value1 { get; set; }
    public string Value2 { get; set; }
    public string Value3 { get; set; }
    public bool Value4 { get; set; }
}

如果订阅列表是动态的,那么您应该将JSON文件更改为如下所示:

[
{"Subscriptions": [
    {
        "Value1": "3454234",
        "Value2": "345643564",
        "Value3": "665445",
        "Value4": "True"
    },
    {
        "Value1": "3454234",
        "Value2": "345643564",
        "Value3": "665445",
        "Value4": "True"
    },
    {
        "Value1": "3454234",
        "Value2": "345643564",
        "Value3": "665445",
        "Value4": "True"
    }
]}, { "Subscriptions":[
            {
                "Value1": "3667635",
                "Value2": "34563456",
                "Value3": "234545",
                "Value4": "False"
            },
            {
                "Value1": "97865",
                "Value2": "356356",
                "Value3": "665757445",
                "Value4": "True"
            },
            {
                "Value1": "3454234",
                "Value2": "345643564",
                "Value3": "665445",
                "Value4": "False"
            }
        ]}
]