Converting JSON into Python dict

后端 未结 6 1074
北荒
北荒 2020-12-13 06:49

I\'ve been searching around trying to find an answer to this question, and I can\'t seem to track it down. Maybe it\'s too late in the evening to figure the answer out, so

相关标签:
6条回答
  • 2020-12-13 07:33
    django.utils.simplejson.loads(someJson)
    
    0 讨论(0)
  • 2020-12-13 07:34
    Hello here my example
    import json
    class SimpleObject(object):
        def __init__(self, _dict):
            self.__dict__.update(_dict)
    
    data=json.loads("{\"name\":\"Rishikesh Agrawani\", \"age\": 25}" )  
    so=SimpleObject(data)
    print (so.name)
    print (so.age)
    
    if you transform your data to objects is better and more fast work.
    
    0 讨论(0)
  • 2020-12-13 07:35
    • Use the json module for loading JSON. (Pre-2.6 use the third party simplejson module, which has the same exact API.)

      >>> import json
      >>> s = '{"foo": 6, "bar": [1, 2, 3]}'
      >>> d = json.loads(s)
      >>> print d
      {u'foo': 6, u'bar': [1, 2, 3]}
      
    • Your actual data cannot be loaded this way since it's actually two JSON objects separated by a comma and with a trailing comma. You'll need to separate them or otherwise deal with this.

      • Where did you get this string?
    0 讨论(0)
  • 2020-12-13 07:46

    First thing first.

    Here I have stored your pulled data string into a variable named data_str which has two dictionaries.

    >>> data_str = "{\"description\":\"fdsafsa\",\"order\":\"1\",\"place\":\"22 Plainsman Rd, Mississauga, ON, Canada\",\"lat\":43.5969175,\"lng\":-79.7248744,\"locationDate\":\"03/24/2010\"},{\"description\":\"sadfdsa\",\"order\":\"2\",\"place\":\"50 Dawnridge Trail, Brampton, ON, Canada\",\"lat\":43.7304774,\"lng\":-79.8055435,\"locationDate\":\"03/26/2010\"},"
    

    After that I converted it into another string named data_str2 which is in list form and removed extra comma(,) from end (as it gives error while string data to python object conversion).

    >>> data_str2 = "[" + data_str[0: 1] + data_str[1: len(data_str)-1] + "]"
    

    Finally, I converted this list string (a list having 2 dictionaries) into original python list and stored it in a variable named data_list.

    >>> import json
    >>> data_list = json.loads(data_str2) # Now data_list is a list having 2 dictionaries
    

    Now let's print our data.

    >>> print data_list
    [{u'description': u'fdsafsa', u'order': u'1', u'place': u'22 Plainsman Rd, Mississauga, ON, Canada', u'lat': 43.5969175, u'lng': -79.7248744, u'locationDate': u'03/24/2010'}, {u'description': u'sadfdsa', u'order': u'2', u'place': u'50 Dawnridge Trail, Brampton, ON, Canada', u'lat': 43.7304774, u'lng': -79.8055435, u'locationDate': u'03/26/2010'}]
    >>> 
    >>> print type(data_list)
    <type 'list'>
    >>> 
    >>> print data_list[0]
    {u'description': u'fdsafsa', u'order': u'1', u'place': u'22 Plainsman Rd, Mississauga, ON, Canada', u'lat': 43.5969175, u'lng': -79.7248744, u'locationDate': u'03/24/2010'}
    >>> 
    >>> print data_list[1]
    {u'description': u'sadfdsa', u'order': u'2', u'place': u'50 Dawnridge Trail, Brampton, ON, Canada', u'lat': 43.7304774, u'lng': -79.8055435, u'locationDate': u'03/26/2010'}
    >>> 
    

    Pass this data_list list from views and access it in your Django template as follows,

    {% for data in locations %}
          <tr>
               <td> {{ data.place }} </td>
               <td> {{ data.locationDate }} </td>
          </tr>
    {% endfor %}
    

    A sample code segment for your views.

    def locations(request):
        # YOU HAVE TO WRITE YOUR CODE LOGIC HERE TO GET THE LIST, 
        # I AM WRITING IT DIRECTLY
        data_list = [{u'description': u'fdsafsa', u'order': u'1', u'place': u'22 Plainsman Rd, Mississauga, ON, Canada', u'lat': 43.5969175, u'lng': -79.7248744, u'locationDate': u'03/24/2010'}, {u'description': u'sadfdsa', u'order': u'2', u'place': u'50 Dawnridge Trail, Brampton, ON, Canada', u'lat': 43.7304774, u'lng': -79.8055435, u'locationDate': u'03/26/2010'}]
        return render(request, "locations.html", {"locations": data_list})
    

    IT WORKED NICE.

    Now I wanna explain that how I reached to solution, I think it will be helpful for beginners. Please see the below explained step by step procedure or see here.

    >>> import json   
    >>>
    >>> # A simple attempt
    >>> s = "{\"description\":\"fdsafsa\"}"
    >>> python_dict = json.loads(s)
    >>> python_dict
    {u'description': u'fdsafsa'}
    >>> # Accessing value using key
    >>> python_dict["description"]
    u'fdsafsa'
    >>> 
    >>> # It worked, lets test our given string containing 2 dictionaries(in string form) one by one
    >>> # Converting 1st JSON string to Dict
    >>> s2 = "{\"description\":\"fdsafsa\",\"order\":\"1\",\"place\":\"22 Plainsman Rd, Mississauga, ON, Canada\",\"lat\":43.5969175,\"lng\":-79.7248744,\"locationDate\":\"03/24/2010\"}"
    >>> python_dict2 = json.loads(s2)                                                                                      >>> python_dict2
    {u'description': u'fdsafsa', u'order': u'1', u'place': u'22 Plainsman Rd, Mississauga, ON, Canada', u'lat': 43.5969175, u'lng': -79.7248744, u'locationDate': u'03/24/2010'}
    >>> 
    >>> # Converting 2nd JSON string to Dict
    >>> # remove comma(,) from end otherwise you will get the following error
    >>> s3 = "{\"description\":\"sadfdsa\",\"order\":\"2\",\"place\":\"50 Dawnridge Trail, Brampton, ON, Canada\",\"lat\":43.7304774,\"lng\":-79.8055435,\"locationDate\":\"03/26/2010\"},"
    >>> python_dict3 = json.loads(s3)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 339, in loads
        return _default_decoder.decode(s)
      File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 367, in decode
        raise ValueError(errmsg("Extra data", s, end, len(s)))
    ValueError: Extra data: line 1 column 152 - line 1 column 153 (char 151 - 152)
    >>> 
    >>> # Now I removed comma(,) from end and retried, it worked
    >>> s3 = "{\"description\":\"sadfdsa\",\"order\":\"2\",\"place\":\"50 Dawnridge Trail, Brampton, ON, Canada\",\"lat\":43.7304774,\"lng\":-79.8055435,\"locationDate\":\"03/26/2010\"}"
    >>> python_dict3 = json.loads(s3) 
    >>> 
    >>> # So now we knew that we have not to include any extra comma at end in the string form of JSON
    >>> # For example (Correct form)
    >>> details_str = "{\"name\":\"Rishikesh Agrawani\", \"age\": 25}" 
    >>> details_dict = json.loads(details_str)
    >>> details_dict["name"]
    u'Rishikesh Agrawani'
    >>> details_dict["age"]
    25
    >>> # Now (Incorrect form), here comma(,) is at end, just after } 
    >>> details_str = "{\"name\":\"Rishikesh Agrawani\", \"age\": 25},"
    >>> details_dict = json.loads(details_str)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 339, in loads
        return _default_decoder.decode(s)
      File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 367, in decode
        raise ValueError(errmsg("Extra data", s, end, len(s)))
    ValueError: Extra data: line 1 column 41 - line 1 column 42 (char 40 - 41)
    >>> 
    >>> # The problem is the string does not denote any single python object 
    >>> # So we will convert the string into a list form by appending [ at beginning and ] at end
    >>> # Now our string will denote a single Pytohn object that is list of 2 dictioanaries
    >>> # Lets do this, here I am storing the given string into variable s4
    >>> data_str = "{\"description\":\"fdsafsa\",\"order\":\"1\",\"place\":\"22 Plainsman Rd, Mississauga, ON, Canada\",\"lat\":43.5969175,\"lng\":-79.7248744,\"locationDate\":\"03/24/2010\"},{\"description\":\"sadfdsa\",\"order\":\"2\",\"place\":\"50 Dawnridge Trail, Brampton, ON, Canada\",\"lat\":43.7304774,\"lng\":-79.8055435,\"locationDate\":\"03/26/2010\"},"
    >>> s5 = "[" + s4[0:1] + s4[1: len(s4)-1] + "]"
    >>> s5
    '[{"description":"fdsafsa","order":"1","place":"22 Plainsman Rd, Mississauga, ON, Canada","lat":43.5969175,"lng":-79.7248744,"locationDate":"03/24/2010"},{"description":"sadfdsa","order":"2","place":"50 Dawnridge Trail, Brampton, ON, Canada","lat":43.7304774,"lng":-79.8055435,"locationDate":"03/26/2010"}]'
    >>> # l is a list of 2 dictionaries
    >>> l = json.loads(s5)
    >>> l[0]
    {u'description': u'fdsafsa', u'order': u'1', u'place': u'22 Plainsman Rd, Mississauga, ON, Canada', u'lat': 43.5969175, u'lng': -79.7248744, u'locationDate': u'03/24/2010'}
    >>> 
    >>> l[1]
    {u'description': u'sadfdsa', u'order': u'2', u'place': u'50 Dawnridge Trail, Brampton, ON, Canada', u'lat': 43.7304774, u'lng': -79.8055435, u'locationDate': u'03/26/2010'}
    >>>                                                           
    

    Thanks.

    0 讨论(0)
  • 2020-12-13 07:49

    Just a combination of other answers:

    import json
    yourString = "{\"description\":\"fdsafsa\",\"order\":\"1\",\"place\":\"22 Plainsman Rd, Mississauga, ON, Canada\",\"lat\":43.5969175,\"lng\":-79.7248744,\"locationDate\":\"03/24/2010\"},{\"description\":\"sadfdsa\",\"order\":\"2\",\"place\":\"50 Dawnridge Trail, Brampton, ON, Canada\",\"lat\":43.7304774,\"lng\":-79.8055435,\"locationDate\":\"03/26/2010\"},"
    target = json.loads("[" + yourString[:-1] + "]")
    

    outputs

    [{u'description': u'fdsafsa', u'order': u'1', u'place': u'22 Plainsman Rd, Mississauga, ON, Canada', u'lat': 43.5969175, u'lng': -79.7248744, u'locationDate': u'03/24/2010'}, {u'description': u'sadfdsa', u'order': u'2', u'place': u'50 Dawnridge Trail, Brampton, ON, Canada', u'lat': 43.7304774, u'lng': -79.8055435, u'locationDate': u'03/26/2010'}]
    

    As mentioned

    • this string contains two json objects, so put it inside an array (the [])
    • it has a trailing ,, remove via [:-1] slicing syntax
    0 讨论(0)
  • 2020-12-13 07:51

    The string you show is not a JSON-coded object (eqv to a Python dict) — more like an array (eqv to a list) without brackets and with a stray extra comma at the end. So (using simplejson for version portability — the standard library's json in 2.6 is fine too of course!-):

    >>> import simplejson
    >>> js = "{\"description\":\"fdsafsa\",\"order\":\"1\",\"place\":\"22 Plainsman Rd, Mississauga, ON, Canada\",\"lat\":43.5969175,\"lng\":-79.7248744,\"locationDate\":\"03/24/2010\"},{\"description\":\"sadfdsa\",\"order\":\"2\",\"place\":\"50 Dawnridge Trail, Brampton, ON, Canada\",\"lat\":43.7304774,\"lng\":-79.8055435,\"locationDate\":\"03/26/2010\"},"
    >>> simplejson.loads('[%s]' % js[:-1])
    [{'description': 'fdsafsa', 'order': '1', 'place': '22 Plainsman Rd, Mississauga, ON, Canada', 'lat': 43.596917500000004, 'lng': -79.724874400000004, 'locationDate': '03/24/2010'}, {'description': 'sadfdsa', 'order': '2', 'place': '50 Dawnridge Trail, Brampton, ON, Canada', 'lat': 43.730477399999998, 'lng': -79.805543499999999, 'locationDate': '03/26/2010'}]
    

    If you really want a dict you'll have to specify how to treat these two unnamed items, i.e., what arbitrary keys you want to slap on them...?

    0 讨论(0)
提交回复
热议问题