Help Json to Python
#1
hi all need to code python for afficher liste produit
this is code json
Code:
[
  {
    "listProduit": [
      {
        "title": "Tom Yam Kung",
        "descriptionProduit": "Soupe aux crevettes, champignons frais, citronnelle (piquant)",
        "price": "39,00DHS",
        "cat": "Soupes & Salades"
      },
      {
        "title": "Tom Kha Kai",
        "descriptionProduit": "Poulet, lait de coco, citronnelle",
        "price": "35,00DHS",
        "cat": "Soupes & Salades"
      }
],
    "nameCategory": "Soupes & Salades",
    "imgCategory": "http://assets.hellofood.ma/dynamic/images/menucategories/13299_menu.jpg"
  },
and this my code python for affiche
Code:
r = http.request('GET', 'http://localhost/category.json')
j=json.loads(r.data.decode("utf-8"))

mode    = args.get('mode', None)
extra   = args.get('extra',None)


if mode is None:
    url = build_url({'mode': 'folder', 'extra': 'Restaurant'})
    li = xbmcgui.ListItem('Restaurant', iconImage='DefaultFolder.png')
    xbmcplugin.addDirectoryItem(handle=addon_handle, url=url, listitem=li, isFolder=True)

    xbmcplugin.endOfDirectory(addon_handle)
    
elif mode[0] == 'folder':
    i=0
    while i<len(j):
        url = build_url({'mode': 'Category', 'extra': j[i]['listProduit']})
        li = xbmcgui.ListItem(j[i]['nameCategory'], iconImage=j[i]['imgCategory'])
        xbmcplugin.addDirectoryItem(handle=addon_handle, url=url,listitem=li, isFolder=True)
        i=i+1

    xbmcplugin.endOfDirectory(addon_handle)    

this last party in erreur

elif mode[0] == 'Category':
    i=0
    while i<len(j):
        url = build_url({'mode': 'Produit', 'extra': j[i]['listProduit']})
        li = xbmcgui.ListItem(j[i]['listProduit']['title'])
        xbmcplugin.addDirectoryItem(handle=addon_handle, url=url,listitem=li, isFolder=True)
        i=i+1

    xbmcplugin.endOfDirectory(addon_handle)
Reply
#2
listProduit is a list, so you need to use indexes:
Code:
j[i]['listProduit'][0]['title']
Do not PM or e-mail Team-Kodi members directly asking for support.
Always read the Forum rules, Kodi online-manual, FAQ, Help and Search the forum before posting.
Reply
#3
(2016-02-25, 12:05)ronie Wrote: listProduit is a list, so you need to use indexes:
Code:
j[i]['listProduit'][0]['title']

thank you but when i shose this index y afficher the first title , but i need to show all titles in the listProduit
Reply
#4
Then loop over them.
Code:
for produit in j[i]['listProduit']:
    print produit["title"]

Also, it seems that the dict containing listProduit is itself, part of a list. You may therefore need to loop over that list too.
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#5
(2016-02-25, 14:33)el_Paraguayo Wrote: Then loop over them.
Code:
for produit in j[i]['listProduit']:
    print produit["title"]

Also, it seems that the dict containing listProduit is itself, part of a list. You may therefore need to loop over that list too.

thank you look what im doing but this code show all the list produit and i have to show list the produit in the same category

Code:
elif mode[0] == 'Category':
    for category in j:
        for produit in category['listProduit']:
            if(category['nameCategory']==produit['cat']):
                url = build_url({'mode': 'Produit'})
                li = xbmcgui.ListItem(produit['title'])
                xbmcplugin.addDirectoryItem(handle=addon_handle, url=url,listitem=li, isFolder=True)
                
    xbmcplugin.endOfDirectory(addon_handle)
Reply
#6
If you know the name of the category you want, you can just extract that at the beginning:
Code:
elif mode[0] == 'Category':

    # Just get one category using a list comprehension
    filtered_category = [x for x in j if x["cat"] == "Soupes & Salades"]

    for category in filtered_category:
        for produit in category['listProduit']:
            url = build_url({'mode': 'Produit'})
            li = xbmcgui.ListItem(produit['title'])
            xbmcplugin.addDirectoryItem(handle=addon_handle, url=url,listitem=li, isFolder=True)
                
    xbmcplugin.endOfDirectory(addon_handle)

Is that what you're after?
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply

Logout Mark Read Team Forum Stats Members Help
Help Json to Python1