user8634222
user8634222

Reputation: 105

The order of dictionary is varied each time

The order of dictionary is varied each time although using OrderedDict. I wrote in views.py

from collections import OrderedDict

from django.shortcuts import render
import json

def index(request):
    with open('./data/data.json', 'r') as f:
        json_dict = json.loads(f.read())
        json_data = OrderedDict()
        json_data = json_dict
    return render(request, 'index.html', {'json_data': json_data})

and I wrote in index.html

<html>
 <head>
 <script type="text/javascript" src="//code.jquery.com/jquery-1.11.0.min.js"></script>
  <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/chosen/1.8.2/chosen.jquery.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.jquery.js"></script>
 <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.css">
 </head>
 <body>
    <select id="mainDD" data-placeholder="Choose" class="chzn-select" style="width:600px;">
    {% for i in json_data.items.values %}
            <option>{{ i }}</option>
    {% endfor %}
    </select>
    <select name="type" id="type1">
    {% for j in json_data.type1.values %}
            <option>{{ j }}</option>
    {% endfor %}
    </select>
    <select name="type" id="type2">
    {% for k in json_data.type2.values %}
            <option>{{ k }}</option>
    {% endfor %}
    </select>
    <select name="type" id="type3">
    {% for l in json_data.type3.values %}
            <option>{{ l }}</option>
    {% endfor %}
    </select>
    <select name="type" id="type4">
    {% for m in json_data.type4.values %}
            <option>{{ m }}</option>
    {% endfor %}
    </select>

  </script>

  </body>
</html>

Variable of i&j&k&l&m has result of json_data,but this dictionary of json_data is not the order.For example i has {'items': [{'---': '---', ‘A’: ‘a’, ‘B’: ‘b’, ‘C: ‘c’, ‘D’: ‘d’}]} but the order of drill down is b=>c=>d=>a.I want to show a =>b=>c=>d .I think this can be done by using OrderedDict() but it is wrong.How should i fix this?What should I write it?

Upvotes: 0

Views: 65

Answers (2)

cezar
cezar

Reputation: 12012

Your idea is correct. Using OrderedDict will preserve the order of the items in the dictionary. However there is an error in your code:

json_data = OrderedDict()
json_data = json_dict

First you initialize json_data as OrderedDict, but in the second statement you override the variable and assign it the value of json_dict.

Daniel Roseman was faster than me and gave you better answer, I'd just naively done this:

json_data = OrderedDict(json_dict)

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599610

All you've done is overwrite your parsed JSON with an OrderedDict object; that doesn't do anything useful at all.

Instead, as the documentation shows, you can pass that class as the object_pairs_hook parameter:

json_dict = json.loads(f.read(), object_pairs_hook=OrderedDict)

Upvotes: 5

Related Questions