Hi evryone,
i'm calling a python function from a javascript code, so i'm passing the record with json format. i want to get the value of some fileds from the JSON code. How can i do this ?
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
Hi evryone,
i'm calling a python function from a javascript code, so i'm passing the record with json format. i want to get the value of some fileds from the JSON code. How can i do this ?
Hi Martin, thank's .... I tried this code and it work :D
def delete_reserve(self, cr, uid, ids , record,this): to_json = json.dumps(record,separators=(','':')) data_json = json.loads(to_json) wg_product = data_json['attributes']['product_id'] wg_qty_uos = data_json['attributes']['product_uos_qty']
Glad to help. Pity I missed the karma points by deleting too fast, duh.
Heh!
I deleted my answer because I noticed you wanted JavaScript. You'll see both my earlier versions below:
Improving my old Python answer (below), you may find it far nicer to work with json.loads() with built in conversion to a dot separated namespace object, like this :
data_json = json.loads(
to_json
, object_hook=lambda d: namedtuple('X', d.keys())(*d.values()))
wg_product = data_json.attributes.product_id
wg_qty_uos = data_json.attributes.product_uos_qty
If you still want JavaScript as you originally asked . . .
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<body>
<p id="rslt">Result here.</p>
<p id="alt">Alternate result here.</p>
<button type="button" onclick="tryIt()">Try</button>
</body>
<script>
var jsonGlossary = "{";
jsonGlossary += " \"glossary\": {";
jsonGlossary += " \"title\": \"example glossary\",";
jsonGlossary += " \"GlossDiv\": {";
jsonGlossary += " \"title\": \"S\",";
jsonGlossary += " \"GlossList\": {";
jsonGlossary += " \"GlossEntry\": {";
jsonGlossary += " \"ID\": \"SGML\",";
jsonGlossary += " \"SortAs\": \"SGML\",";
jsonGlossary += " \"GlossTerm\": \"Standard Generalized Markup Language\",";
jsonGlossary += " \"Acronym\": \"SGML\",";
jsonGlossary += " \"Abbrev\": \"ISO 8879:1986\",";
jsonGlossary += " \"GlossDef\": {";
jsonGlossary += " \"para\": \"A meta-markup language, used to create markup languages such as DocBook.\",";
jsonGlossary += " \"GlossSeeAlso\": [\"GML\", \"XML\"]";
jsonGlossary += " },";
jsonGlossary += " \"GlossSee\": \"markup\"";
jsonGlossary += " }";
jsonGlossary += " }";
jsonGlossary += " }";
jsonGlossary += " }";
jsonGlossary += "}";
function tryIt()
{
obj = JSON.parse(jsonGlossary);
document.getElementById("rslt").innerHTML=obj['glossary']['GlossDiv']['GlossList']['GlossEntry']['GlossTerm'];
document.getElementById("alt").innerHTML=obj.glossary.GlossDiv.GlossList.GlossEntry.GlossDef.para;
}
</script>
</html>
Not that it matters now, but this was the first answer (that I deleted while you were answering).
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
from json import loads
jsonGlossary = '{'
jsonGlossary += ' "glossary": {'
jsonGlossary += ' "title": "example glossary",'
jsonGlossary += ' "GlossDiv": {'
jsonGlossary += ' "title": "S",'
jsonGlossary += ' "GlossList": {'
jsonGlossary += ' "GlossEntry": {'
jsonGlossary += ' "ID": "SGML",'
jsonGlossary += ' "SortAs": "SGML",'
jsonGlossary += ' "GlossTerm": "Standard Generalized Markup Language",'
jsonGlossary += ' "Acronym": "SGML",'
jsonGlossary += ' "Abbrev": "ISO 8879:1986",'
jsonGlossary += ' "GlossDef": {'
jsonGlossary += ' "para": "A meta-markup language, used to create markup languages such as DocBook.",'
jsonGlossary += ' "GlossSeeAlso": ["GML", "XML"]'''
jsonGlossary += ' },'
jsonGlossary += ' "GlossSee": "markup"'
jsonGlossary += ' }'
jsonGlossary += ' }'
jsonGlossary += ' }'
jsonGlossary += ' }'
jsonGlossary += '}'
print jsonGlossary
print ' #A - - '
dictGlossary = loads (jsonGlossary)
print dictGlossary
print ' #B - - '
print dictGlossary['glossary']['GlossDiv']['GlossList']['GlossEntry']['GlossDef']['para']
Hi,
Try something like this :
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
from json import loads
jsonGlossary = '{'
jsonGlossary += ' "glossary": {'
jsonGlossary += ' "title": "example glossary",'
jsonGlossary += ' "GlossDiv": {'
jsonGlossary += ' "title": "S",'
jsonGlossary += ' "GlossList": {'
jsonGlossary += ' "GlossEntry": {'
jsonGlossary += ' "ID": "SGML",'
jsonGlossary += ' "SortAs": "SGML",'
jsonGlossary += ' "GlossTerm": "Standard Generalized Markup Language",'
jsonGlossary += ' "Acronym": "SGML",'
jsonGlossary += ' "Abbrev": "ISO 8879:1986",'
jsonGlossary += ' "GlossDef": {'
jsonGlossary += ' "para": "A meta-markup language, used to create markup languages such as DocBook.",'
jsonGlossary += ' "GlossSeeAlso": ["GML", "XML"]'''
jsonGlossary += ' },'
jsonGlossary += ' "GlossSee": "markup"'
jsonGlossary += ' }'
jsonGlossary += ' }'
jsonGlossary += ' }'
jsonGlossary += ' }'
jsonGlossary += '}'
print jsonGlossary
print ' #A - - '
dictGlossary = loads (jsonGlossary)
print dictGlossary
print ' #B - - '
print dictGlossary['glossary']['GlossDiv']['GlossList']['GlossEntry']['GlossDef']['para']
Your output should look this :
{ "glossary": { "title": "example glossary", "GlossDiv": { "title . . .
#A - -
{u'glossary': {u'GlossDiv': {u'GlossList': {u'GlossEntry': {u'GlossDe . . .
#B - -
A meta-markup language, used to create markup languages such as DocBook.
Tạo tài khoản ngay hôm nay để tận hưởng các tính năng độc đáo và tham gia cộng đồng tuyệt vời của chúng tôi!
Đăng kýBài viết liên quan | Trả lời | Lượt xem | Hoạt động | |
---|---|---|---|---|
|
0
thg 1 16
|
4081 | ||
|
0
thg 3 25
|
1242 | ||
|
4
thg 4 24
|
173924 | ||
|
0
thg 12 23
|
2015 | ||
|
5
thg 7 25
|
227370 |