在属性名称中使用点反序列化JSON [英] Deserialize JSON with dot in property name

查看:126
本文介绍了在属性名称中使用点反序列化JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将属性名称中带点的JSON反序列化为键值格式.我正在使用内置的ASP.NET MVC模型绑定.似乎将点解释为对象表示法,而不仅仅是键值对象.有什么方法可以使它正确地反序列化为忽略点的键值吗?这很重要,因为数据将需要再次以这种格式输出.

I am trying to deserialize JSON with dots in the property names into a key-value format. I am using the inbuilt ASP.NET MVC model binding. It seems to be interpreting the dots as a object notation instead of just a key-value object. Is there any way to make it deserialize correctly as a key value ignoring the dots? This is important as the data will need to be output again in this format.

控制器动作

[HttpPost]
public ActionResult SaveProgress(int id, ProgressVM data)
{
    // ProgressVM Data property has an item with key "prop" and a null value, nothing else
}

查看模型

public class ProgressVM
{
    public int ID { get; set; }
    public Dictionary<string, string> Data { get; set; }
}

JSON示例

{
    "ID": 123,
    "Data": {
        "prop.0.name": "value",
        "prop.0.id": "value",
        "prop.1.name": "value",
        "prop.2.name": "value",
        "prop.3.name": "value"
    }
}

推荐答案

首先,在POST动作中传递参数时,仅传递一个参数,而不传递多个参数.因此,尝试创建一个包含Id和ProgressVM模型的模型.

First of all, when passing parameters in a POST action, you pass only one parameter and not multiple. So, try to create a model where the Id and the ProgressVM model will be included.

现在,对于反序列化的问题. JsonValueProviderFactory 类似乎有问题,例如它不绑定ProgressVM模型中的嵌套Dictionary<string, string>.我知道,因为我尝试为此模型创建ModelBinder,但是它在DictionaryValueProvider上失败,没有绑定Data属性.不幸的是,我没有搜索为什么会发生这种情况,这是JsonValueProviderFactory上的错误吗?我不知道,所以我迅速转到了另一个解决方法.

Now, for the issue with the deserialization. Something seems wrong with the JsonValueProviderFactory class, as it does not bind the nested Dictionary<string, string> from ProgressVM model. I know, because I tried to create a ModelBinder for this model, but it fails on the DictionaryValueProvider, not binding the Data property. Unfortunately, I haven't searched why this is happening, is it a bug on JsonValueProviderFactory? I do not know, so I moved quickly to another workaround.

为什么不尝试将JSON字符串提供给控制器而不是模型,然后在控制器主体中反序列化输入呢? 看下面的例子:

Why don't you try to give the JSON string to the controller instead of the model, and then in the body of the controller deserialize the input? Look at the example below:

JavaScript代码,使用jQuery AJAX发布数据

var data = {
    "ID": 123,
    "Data": {
        "prop.0.name": "value",
        "prop.0.id": "value",
        "prop.1.name": "value",
        "prop.2.name": "value",
        "prop.3.name": "value"
    }
}; 

$.ajax({
    url: '@Url.Action("SaveProgress", "Home")',
    data: { "data": JSON.stringify(data) },
    method: "POST"
});

ASP.NET MVC控制器操作

[HttpPost]
public ActionResult SaveProgress(string data)
{
    var json = JsonConvert.DeserializeObject<ProgressVM>(data);
    // Rest of code here
}

这是在回答您的问题吗?

Is this answering your question?

这篇关于在属性名称中使用点反序列化JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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