Why can't Python parse this JSON data?

后端 未结 9 2453
傲寒
傲寒 2020-11-21 05:06

I have this JSON in a file:

{
    \"maps\": [
        {
            \"id\": \"blabla\",
            \"iscategorical\         


        
相关标签:
9条回答
  • Your data.json should look like this:

    {
     "maps":[
             {"id":"blabla","iscategorical":"0"},
             {"id":"blabla","iscategorical":"0"}
            ],
    "masks":
             {"id":"valore"},
    "om_points":"value",
    "parameters":
             {"id":"valore"}
    }
    

    Your code should be:

    import json
    from pprint import pprint
    
    with open('data.json') as data_file:    
        data = json.load(data_file)
    pprint(data)
    

    Note that this only works in Python 2.6 and up, as it depends upon the with-statement. In Python 2.5 use from __future__ import with_statement, in Python <= 2.4, see Justin Peel's answer, which this answer is based upon.

    You can now also access single values like this:

    data["maps"][0]["id"]  # will return 'blabla'
    data["masks"]["id"]    # will return 'valore'
    data["om_points"]      # will return 'value'
    
    0 讨论(0)
  • 2020-11-21 05:17

    Justin Peel's answer is really helpful, but if you are using Python 3 reading JSON should be done like this:

    with open('data.json', encoding='utf-8') as data_file:
        data = json.loads(data_file.read())
    

    Note: use json.loads instead of json.load. In Python 3, json.loads takes a string parameter. json.load takes a file-like object parameter. data_file.read() returns a string object.

    To be honest, I don't think it's a problem to load all json data into memory most cases.

    0 讨论(0)
  • 2020-11-21 05:17
    data = []
    with codecs.open('d:\output.txt','rU','utf-8') as f:
        for line in f:
           data.append(json.loads(line))
    
    0 讨论(0)
  • 2020-11-21 05:17

    As a python3 user,

    The difference between load and loads methods is important especially when you read json data from file.

    As stated in the docs:

    json.load:

    Deserialize fp (a .read()-supporting text file or binary file containing a JSON document) to a Python object using this conversion table.

    json.loads:

    json.loads: Deserialize s (a str, bytes or bytearray instance containing a JSON document) to a Python object using this conversion table.

    json.load method can directly read opened json document since it is able to read binary file.

    with open('./recipes.json') as data:
      all_recipes = json.load(data)
    

    As a result, your json data available as in a format specified according to this conversion table:

    https://docs.python.org/3.7/library/json.html#json-to-py-table

    0 讨论(0)
  • Here you go with modified data.json file:

    {
        "maps": [
            {
                "id": "blabla",
                "iscategorical": "0"
            },
            {
                "id": "blabla",
                "iscategorical": "0"
            }
        ],
        "masks": [{
            "id": "valore"
        }],
        "om_points": "value",
        "parameters": [{
            "id": "valore"
        }]
    }
    

    You can call or print data on console by using below lines:

    import json
    from pprint import pprint
    with open('data.json') as data_file:
        data_item = json.load(data_file)
    pprint(data_item)
    

    Expected output for print(data_item['parameters'][0]['id']):

    {'maps': [{'id': 'blabla', 'iscategorical': '0'},
              {'id': 'blabla', 'iscategorical': '0'}],
     'masks': [{'id': 'valore'}],
     'om_points': 'value',
     'parameters': [{'id': 'valore'}]}
    

    Expected output for print(data_item['parameters'][0]['id']):

    valore
    
    0 讨论(0)
  • 2020-11-21 05:24

    If you're using Python3, you can try changing your (connection.json file) JSON to:

    {
      "connection1": {
        "DSN": "con1",
        "UID": "abc",
        "PWD": "1234",
        "connection_string_python":"test1"
      }
      ,
      "connection2": {
        "DSN": "con2",
        "UID": "def",
        "PWD": "1234"
      }
    }
    

    Then using the following code:

    connection_file = open('connection.json', 'r')
    conn_string = json.load(connection_file)
    conn_string['connection1']['connection_string_python'])
    connection_file.close()
    >>> test1
    
    0 讨论(0)
提交回复
热议问题