Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在odoo中更改JsonRequest python_Python_Json_Odoo - Fatal编程技术网

在odoo中更改JsonRequest python

在odoo中更改JsonRequest python,python,json,odoo,Python,Json,Odoo,成功后,以如下格式将数据json发送到odoo { "params":{ "name":"admin", "age":"18" } } { "name":"admin", "age":"18" } 但我需要这样的数据 {

成功后,以如下格式将数据json发送到odoo

{
    "params":{
        "name":"admin",
        "age":"18"
        }
}
{
        "name":"admin",
        "age":"18"
}
但我需要这样的数据

{
    "params":{
        "name":"admin",
        "age":"18"
        }
}
{
        "name":"admin",
        "age":"18"
}
我该怎么做? 我已经找到了改变它的代码,但我不知道如何改变它

类JsonRequest(WebRequest): “”“HTTP上的
JSON-RPC 2
”请求处理程序

* ``method`` is ignored
* ``params`` must be a JSON object (not an array) and is passed as keyword
  arguments to the handler method
* the handler method's result is returned as JSON-RPC ``result`` and
  wrapped in the `JSON-RPC Response
  <http://www.jsonrpc.org/specification#response_object>`_

Sucessful request::

  --> {"jsonrpc": "2.0",
       "method": "call",
       "params": {"context": {},
                  "arg1": "val1" },
       "id": null}

  <-- {"jsonrpc": "2.0",
       "result": { "res1": "val1" },
       "id": null}

Request producing a error::

  --> {"jsonrpc": "2.0",
       "method": "call",
       "params": {"context": {},
                  "arg1": "val1" },
       "id": null}

  <-- {"jsonrpc": "2.0",
       "error": {"code": 1,
                 "message": "End user error message.",
                 "data": {"code": "codestring",
                          "debug": "traceback" } },
       "id": null}

"""
_request_type = "json"

def __init__(self, *args):
    super(JsonRequest, self).__init__(*args)

    self.jsonp_handler = None

    args = self.httprequest.args
    jsonp = args.get('jsonp')
    self.jsonp = jsonp
    request = None
    request_id = args.get('id')
    
    if jsonp and self.httprequest.method == 'POST':
        # jsonp 2 steps step1 POST: save call
        def handler():
            self.session['jsonp_request_%s' % (request_id,)] = self.httprequest.form['r']
            self.session.modified = True
            headers=[('Content-Type', 'text/plain; charset=utf-8')]
            r = werkzeug.wrappers.Response(request_id, headers=headers)
            return r
        self.jsonp_handler = handler
        return
    elif jsonp and args.get('r'):
        # jsonp method GET
        request = args.get('r')
    elif jsonp and request_id:
        # jsonp 2 steps step2 GET: run and return result
        request = self.session.pop('jsonp_request_%s' % (request_id,), '{}')
    else:
        # regular jsonrpc2
        request = self.httprequest.stream.read()

    # Read POST content or POST Form Data named "request"
    try:
        self.jsonrequest = json.loads(request)
    except ValueError:
        msg = 'Invalid JSON data: %r' % (request,)
        _logger.info('%s: %s', self.httprequest.path, msg)
        raise werkzeug.exceptions.BadRequest(msg)

    self.params = dict(self.jsonrequest.get("params", {}))
    self.context = self.params.pop('context', dict(self.session.context))
*`method``被忽略
*“`params``必须是JSON对象(不是数组),并作为关键字传递
处理程序方法的参数
*处理程序方法的结果作为JSON-RPC ``result``返回,并且
包装在`JSON-RPC响应中
`_
成功的请求::
-->{“jsonrpc”:“2.0”,
“方法”:“调用”,
“params”:{“context”:{},
“arg1”:“val1”},
“id”:null}
{“jsonrpc”:“2.0”,
“方法”:“调用”,
“params”:{“context”:{},
“arg1”:“val1”},
“id”:null}

在初始化控制器类之前,在任何控制器中放置以下代码

from odoo import http
from odoo.http import request, Response, JsonRequest
from odoo.tools import date_utils

class JsonRequestNew(JsonRequest):

    def _json_response(self, result=None, error=None):

        # response = {
        #    'jsonrpc': '2.0',
        #    'id': self.jsonrequest.get('id')
        #    }
        # if error is not None:
        #    response['error'] = error
        # if result is not None:
        #    response['result'] = result

        responseData = super(JsonRequestNew, self)._json_response(result=result,error=error)

        response = {}
        if error is not None:
            response = error
        if result is not None:
            response = result

        mime = 'application/json'
        body = json.dumps(response, default=date_utils.json_default)

        return Response(
            body, status=error and error.pop('http_status', 200) or 200,
            headers=[('Content-Type', mime), ('Content-Length', len(body))]
        )

class RootNew(http.Root):

    def get_request(self, httprequest):
        
        # deduce type of request

        jsonResponse = super(RootNew, self).get_request(httprequest=httprequest)

        if httprequest.mimetype in ("application/json", "application/json-rpc"):
            return JsonRequestNew(httprequest)
        else:
            return jsonResponse

http.root = RootNew()

class MyController(http.Controller):