如何解析在MVC Controller中收到的JSON [英] How to parse JSON received in MVC Controller

查看:354
本文介绍了如何解析在MVC Controller中收到的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a JSON file that is being sent to an MVC controller via a POST request from the client side. Since there isn't a direct way to store JSON in C#, I'm trying to create models to store the data. Here is the structure of the 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对象没有固定数量的元素,因此我需要一种动态存储每个属性的方法。 如何做到这一点?

For some reason, if I create the model classes with this structure, the Root Object is still null after the it is passed the JSON object from the controller. However, when I explicitly create attributes to store each value of the JSON object, it works. Since the JSON object doesn't have a fixed number of elements, I need a way to dynamically store each attribute. How can this be done?

推荐答案

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

Because of the varying key in the root object and subscriptions, you should use a dictionary.

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,但你松散了强类型的键/属性名称

The above would serialize to the JSON in the OP but you loose the strongly typed keys/property names

这篇关于如何解析在MVC Controller中收到的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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