【问题标题】:Convert JSON object into single one variables将 JSON 对象转换为单个变量
【发布时间】:2018-11-27 01:26:21
【问题描述】:

我使用 ajax 发出了 POST 请求。但我收到这样的数据

[{u'name': u'cuenta', u'value': u'01050160181244049'}, {u'name': u'beneficiario', u'value': u'xxxxxxx'}, {u'name': u'identificador', u'value': u'V'}, {u'name': u'identidad', u'value': u'2423439'}, {u'name': u'email', u'value': u'dfgfdgfd@gmail.com'}, {u'name': u'monto', u'value': u'1'}, {u'name': u'concepto', u'value': u'pago'}]

我需要在我的后端处理它,但我需要以这种方式处理它

{
"cuenta": "010501601811634534549",
"beneficiario": "xxxxxxx",
"identificador": "V",
"identidad": "23432423",
"email": "xxxxx@gmail.com",
"monto": "1",
"concepto": "pago"
}

如何转换它?或者我怎样才能以我需要的正常格式发送我的 ajax 请求?这是我的js代码

$(document).ready(function() {

var frm = $('#formulario_datos');
frm.submit(function (e) {
    frm.attr("disabled", "disabled");

    e.preventDefault();
    var formData = JSON.stringify($("#formulario_datos").serializeArray());

    $.ajax({
        type: "POST",
        url: '/transferencia',
        data: formData,
        success: function (data) {
            alert(data.Message);
        },
        error: function () {
            alert("Error!!!");
        },
        dataType: "json",
        contentType : "application/json"
    });
});

});

【问题讨论】:

  • 您需要显示构成 JSON 回复的 Python 代码。该JS代码与问题无关。 (另外,没有 JSON 对象之类的东西)
  • 你最好修复 Python 部分,而不是为转换做额外的编码。
  • 好的,谢谢你,我在 js 中修复了我的代码以正确发送数据。这是我在 python global cuenta_actual post_data = request.get_json() if cuenta_actual == "" 或 cuenta_actual == post_data.get('cuenta') 上的代码: cuenta_actual = post_data.get('cuenta')

标签: javascript python json api


【解决方案1】:

看起来您只需要将reduce 放入一个对象中,从数组中的每个项目中提取namevalue 属性:

// const arr = $("#formulario_datos").serializeArray();
const arr = [{'name': 'cuenta', 'value': '01050160181244049'}, {'name': 'beneficiario', 'value': 'xxxxxxx'}, {'name': 'identificador', 'value': 'V'}, {'name': 'identidad', 'value': '2423439'}, {'name': 'email', 'value': 'dfgfdgfd@gmail.com'}, {'name': 'monto', 'value': '1'}, {'name': 'concepto', 'value': 'pago'}];

const transformedObj = arr.reduce((a, { name, value }) => {
  a[name] = value;
  return a;
}, {});
console.log(transformedObj);

/*
$.ajax({
  type: "POST",
  url: '/transferencia',
  data: JSON.stringify(transformedObj),
  ...
*/

【讨论】:

    【解决方案2】:

    您可以对结果使用dict 推导式。这是您可以在 REPL 中尝试的示例:

    >>> result = [
            {u'name': u'cuenta', u'value': u'01050160181244049'}, 
            {u'name': u'beneficiario', u'value': u'xxxxxxx'}, 
            {u'name': u'identificador', u'value': u'V'}, 
            {u'name': u'identidad', u'value': u'2423439'}, 
            {u'name': u'email', u'value': u'dfgfdgfd@gmail.com'}, 
            {u'name': u'monto', u'value': u'1'},
            {u'name': u'concepto', u'value': u'pago'}]
    >>> {e["name"]: e["value"] for e in result}
    {u'beneficiario': u'xxxxxxx', u'monto': u'1', u'identificador': u'V', u'concepto': u'pago', u'cuenta': u'01050160181244049', u'identidad': u'2423439', u'email': u'dfgfdgfd@gmail.com'}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-14
      • 1970-01-01
      • 2013-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-06
      • 2017-06-16
      相关资源
      最近更新 更多