Se rendre au contenu
Odoo Menu
  • Se connecter
  • Essai gratuit
  • Applications
    Finance
    • Comptabilité
    • Facturation
    • Notes de frais
    • Feuilles de calcul (BI)
    • Documents
    • Signature
    Ventes
    • CRM
    • Ventes
    • PdV Boutique
    • PdV Restaurant
    • Abonnements
    • Location
    Sites web
    • Site Web
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Chaîne d'approvisionnement
    • Inventaire
    • Fabrication
    • PLM
    • Achats
    • Maintenance
    • Qualité
    Ressources Humaines
    • Employés
    • Recrutement
    • Congés
    • Évaluations
    • Recommandations
    • Parc automobile
    Marketing
    • Marketing Social
    • E-mail Marketing
    • SMS Marketing
    • Événements
    • Marketing Automation
    • Sondages
    Services
    • Projet
    • Feuilles de temps
    • Services sur Site
    • Assistance
    • Planification
    • Rendez-vous
    Productivité
    • Discussion
    • Validations
    • Internet des Objets
    • VoIP
    • Connaissances
    • WhatsApp
    Applications tierces Odoo Studio Plateforme Cloud d'Odoo
  • Industries
    Commerce de détail
    • Librairie
    • Magasin de vêtements
    • Magasin de meubles
    • Épicerie
    • Quincaillerie
    • Magasin de jouets
    Food & Hospitality
    • Bar et Pub
    • Restaurant
    • Fast-food
    • Guest House
    • Distributeur de boissons
    • Hotel
    Real Estate
    • Real Estate Agency
    • Cabinet d'architecture
    • Construction
    • Gestion immobilière
    • Jardinage
    • Association de copropriétaires
    Consulting
    • Accounting Firm
    • Partenaire Odoo
    • Agence Marketing
    • Cabinet d'avocats
    • Aquisition de talents
    • Audit & Certification
    Fabrication
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Cadeaux d'entreprise
    Santé & Fitness
    • Club de sports
    • Opticien
    • Salle de fitness
    • Praticiens bien-être
    • Pharmacie
    • Salon de coiffure
    Trades
    • Bricoleur
    • Matériel informatique et support
    • Solar Energy Systems
    • Cordonnier
    • Services de nettoyage
    • HVAC Services
    Others
    • Nonprofit Organization
    • Agence environnementale
    • Location de panneaux d'affichage
    • Photographie
    • Leasing de vélos
    • Revendeur de logiciel
    Browse all Industries
  • Communauté
    Apprenez
    • Tutoriels
    • Documentation
    • Certifications
    • Formation
    • Blog
    • Podcast
    Renforcer l'éducation
    • Programme éducatif
    • Business Game Scale-Up!
    • Rendez-nous visite
    Obtenir le logiciel
    • Téléchargement
    • Comparez les éditions
    • Versions
    Collaborer
    • Github
    • Forum
    • Événements
    • Traductions
    • Devenez partenaire
    • Services for Partners
    • Enregistrer votre cabinet comptable
    Nos Services
    • Trouver un partenaire
    • Trouver un comptable
    • Rencontrer un conseiller
    • Services de mise en œuvre
    • Références clients
    • Assistance
    • Mises à niveau
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Obtenir une démonstration
  • Tarification
  • Aide

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Comptabilité
  • Inventaire
  • PoS
  • Projet
  • MRP
All apps
Vous devez être inscrit pour interagir avec la communauté.
Toutes les publications Personnes Badges
Étiquettes (Voir toutl)
odoo accounting v14 pos v15
À propos de ce forum
Vous devez être inscrit pour interagir avec la communauté.
Toutes les publications Personnes Badges
Étiquettes (Voir toutl)
odoo accounting v14 pos v15
À propos de ce forum
Aide

Get Value From JSON code

S'inscrire

Recevez une notification lorsqu'il y a de l'activité sur ce poste

Cette question a été signalée
pythonjson
3 Réponses
13369 Vues
Avatar
abdelwahed chiheb

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 ?

image description

0
Avatar
Ignorer
Avatar
abdelwahed chiheb
Auteur Meilleure réponse

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']
0
Avatar
Ignorer
Martin

Glad to help. Pity I missed the karma points by deleting too fast, duh.

Avatar
Martin
Meilleure réponse

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']
0
Avatar
Ignorer
Avatar
Martin
Meilleure réponse

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.
0
Avatar
Ignorer
Vous appréciez la discussion ? Ne vous contentez pas de lire, rejoignez-nous !

Créez un compte dès aujourd'hui pour profiter de fonctionnalités exclusives et échanger avec notre formidable communauté !

S'inscrire
Publications associées Réponses Vues Activité
How to get a value from json?
python json odoo9.0
Avatar
0
janv. 16
4807
new python env
python
Avatar
0
mars 25
2240
What means "Too many values to unpack" message? Résolu
python
Avatar
Avatar
Avatar
Avatar
Avatar
4
avr. 24
175661
have no data in screen. read data in my own module from different model
python
Avatar
0
déc. 23
2888
How to insert value to a one2many field in table with create method? Résolu
python
Avatar
Avatar
Avatar
Avatar
Avatar
5
juil. 25
231837
Communauté
  • Tutoriels
  • Documentation
  • Forum
Open Source
  • Téléchargement
  • Github
  • Runbot
  • Traductions
Services
  • Hébergement Odoo.sh
  • Assistance
  • Migration
  • Développements personnalisés
  • Éducation
  • Trouver un comptable
  • Trouver un partenaire
  • Devenez partenaire
À propos
  • Notre société
  • Actifs de la marque
  • Contactez-nous
  • Emplois
  • Événements
  • Podcast
  • Blog
  • Clients
  • Informations légales • Confidentialité
  • Sécurité.
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo est une suite d'applications open source couvrant tous les besoins de votre entreprise : CRM, eCommerce, Comptabilité, Inventaire, Point de Vente, Gestion de Projet, etc.

Le positionnement unique d'Odoo est d'être à la fois très facile à utiliser et totalement intégré.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now