简体   繁体   English

JSON.NET将完整的键值对解析为键

[英]JSON.NET Parsing Full Key-Value Pair as Key

I'm rolling my own ValidateAntiForgeryToken attribute for Web API 2.2 for one of my controllers that processes form data through AJAX calls. 我正在为我的一个通过AJAX调用处理表单数据的控制器滚动自己的Web API 2.2的ValidateAntiForgeryToken属性。

I'm sending the data to my controller as JSON and it looks like this (pulled from Fiddler): 我将数据作为JSON发送到我的控制器,它看起来像这样(从Fiddler中拉出):

{"__RequestVerificationToken":"E8EoBCaFbqSOXhQZiuM93jciTcOAYeLjZj682-3SZRaQ6OOtrm-caZI_IWnX1FH_nwe_AuWnWwxy5ulS0Ynz0STlNptqN09Lu69HxyTeA9PUln8h73yjahB24QPxqI010","ProjectInfo.Description":"Test Description 2"}

I was trying to get the __RequestVerificationToken data like this, but it's not working: 我试图获取像这样的__RequestVerificationToken数据,但是它不起作用:

JToken json = (JToken)actionContext.ActionArguments["json"];
formToken = (string)json["__RequestVerificationToken"];

formToken keeps containing null values so through some debugging I've found that the entire JSON value is being considered a Key, with an empty value as seen in the screenshot below: formToken一直包含空值,因此通过一些调试,我发现整个JSON值都被视为Key,具有空值,如下面的屏幕快照所示:

值作为键

I'm not sure why this is happening, since the JSON appears to be valid. 我不确定为什么会这样,因为JSON似乎有效。 Fiddler is able to parse the JSON without issues in it's built in Json Viewer, but JSON.NET appears to be sticking the entire json string as the Key instead of parsing it. Fiddler能够解析Json Viewer中内置的JSON,而不会出现问题,但是JSON.NET似乎将整个json字符串粘贴为Key而不是解析它。 I know that I could hack it and manually parse out the value I need from the key, but that's dirty as I rather this be done properly. 我知道我可以破解它并手动从密钥中解析出我需要的值,但这很脏,因为我宁愿正确地执行此操作。

Is there something wrong with my JSON or method that I'm using to obtain it, or is there a bug in the json.net library/asp.net causing this behavior? 我用来获取它的JSON或方法是否存在问题,或者json.net库/asp.net中是否存在导致此行为的错误? Any idea why this might be happening? 知道为什么会这样吗?

EDIT: 编辑:

It's possible that somehow the data is being serialized twice, but I'm not sure why/how. 数据可能以某种方式被序列化了两次,但是我不确定为什么/如何进行。 From my debugger here's the stringified value of json : 在我的调试器中,这是json的字符串化值:

Root = {
"{\"__RequestVerificationToken\":\"yqob-3bUW8C8sUrHWu_feRFOz2KPUKqugo1QoN2s8v9UhlMTwSonxoEdnh85TdM56Xj-aixZdgSQXs8D6ureAQTU83wVtvsoLBd2tDl0ZPyq_2sFefObQx0VHOExQjgh0\",\"ProjectInfo.Description\":\"Test Description\"}": ""
}

Here's the code that generates the JSON on the client side. 这是在客户端生成JSON的代码。 It's a jQuery extension: 这是jQuery扩展:

$.fn.serializeObject = function () {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } 
        else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

This is then called as such in the submit method: 然后在submit方法中这样调用它:

$('#description-editable form').submit(function (e) {
    e.preventDefault();
    var data = JSON.stringify($(this).serializeObject());

    //ajax method for PUT here...

});

It depends on what your AJAX options are, but it looks like your object is being stringified twice before it gets sent up. 这取决于您的AJAX选项是什么,但是看起来您的对象在发送之前已被两次字符串化了。 You should be able to remove the stringify call: 您应该能够删除stringify调用:

$('#description-editable form').submit(function (e) {
    e.preventDefault();
    var data = $(this).serializeObject();

    //ajax method for PUT here...

});

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

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