简体   繁体   English

使用python读取文件json

[英]Read file json with python

i have this file JSON: 我有此文件JSON:

And i want read this file in python, but this code report one error. 我想在python中读取此文件,但是此代码报告了一个错误。

{ tipoGrafico: 'Multi Serie Chart',
  min_value: '1',
  max_value: '1',
  min_strategy: '2',
  max_strategy: '3',
  mutation: '4',
  cxpb: '5',
  mutpb: '6',
  value_tournSize: '7',
  pop_size: '100' 
}

my code python: 我的代码python:

import json
import sys
print("nome del json: ",sys.argv[1])
data = json.load(open(sys.argv[1]))
data["tipoGrafico"]

but i have this error: 但是我有这个错误:

json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 3 (char 2)

Your file is not a valid JSON file - see Single vs double quotes in JSON and JSON standard . 您的文件不是有效的JSON文件-请参阅JSONJSON标准中的 单引号与双引号 In fact, it's not even a valid python dictionary, since keys are not enclosed in any quotes. 实际上,它甚至不是有效的python字典,因为键没有包含在任何引号中。

For your file to be read, you'll need to change it, like this: 要读取文件,您需要对其进行更改,如下所示:

{ "tipoGrafico": "Multi Serie Chart",
  "min_value": "1",
  "max_value": "1",
  "min_strategy": "2",
... }

Also, to ensure proper file handling and closing, I would recommend to use the with statement when opening your file: 另外,为确保正确处理和关闭文件,我建议在打开文件时使用with语句:

with open(sys.argv[1]) as json_file:
    data = json.load(json_file)
data["tipoGrafico"]

This "JSON" is actually valid YAML, so you can simply load it with the yaml module instead (after installing the pyyaml package): 该“ JSON”实际上是有效的YAML,因此您可以简单地使用yaml模块加载它(在安装pyyaml软件包之后):

import yaml
import sys
print("nome del yaml: ",sys.argv[1])
data = yaml.load(open(sys.argv[1]))
data["tipoGrafico"]

I take this "json" from my application with this js: 我用这个js从我的应用程序中获取这个“ json”:

$(document).ready(function() {
    $("#submit").on('click', function(){
        // send ajax
        $.ajax({

            url: 'http://127.0.0.1:8081/', // url where to submit the request
            type : "POST", // type of action POST || GET
            dataType : 'json', // data type
            data : $("#form").serialize(), // post data || get data
            success : function(result) {
                // you can see the result from the console
                // tab of the developer tools
                console.log(result);
            },
            error: function(xhr, resp, text) {
                console.log(xhr, resp, text);
                }
            })
        });
});

on this server node: 在此服务器节点上:

var express = require("express");
var bodyParser = require("body-parser");

var app = express();
var jsonParser = bodyParser.json();
var urlencodedParser = bodyParser.urlencoded({ extended: false });


var obj = {
    table: []
};

app.post("/", urlencodedParser, function(request, response) {   
    console.log(request.body); //This prints the JSON document received (if it is a JSON document)
    obj.table.push(request.body);
});

var json = JSON.stringify(obj);

var fs = require('fs');
fs.writeFile('myjsonfile.json', json, 'utf8');

//Start the server and make it listen for connections on port 8080
app.listen(8081);

how to take the correct json file? 如何获取正确的json文件?


that is not a valid JSON. 这不是有效的JSON。
Try with this: 试试这个:

{
    "tipoGrafico": "Multi Serie Chart",
    "min_value": "1",
    "max_value": "1",
    "min_strategy": "2",
    "max_strategy": "3",
    "mutation": "4",
    "cxpb": "5",
    "mutpb": "6",
    "value_tournSize": "7",
    "pop_size": "100"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM