将python字符串列表转换为JSON时的JSON语法错误

时间:2018-09-08 21:08:20

标签: javascript python json

我正在尝试将json数组呈现为html文件,但是它正在引起语法错误。当数组仅包含数字时,它可以工作。因此,如果我有python列表,例如

list = []  
list.append(3)
list.append(4)

,然后通过以下操作将其转换为JSON:

json.dumps(list)

但是当python列表由字符串组成时,我得到一个错误。例如fcc_table是一个字符串列表。

fcc_table = []

if(frequency_min_input >= 0.275):
    freq_tera_fcc_alloc = str(frequency_min_input) + " THz " + " to " + str(frequency_max_input)+ " THz " + " :" + " Not Allocated."
    fcc_table.append(freq_tera_fcc_alloc)

else:
    with open('assets/Freq_alloc_3.csv', 'r') as csv_file:
        csv_reader = csv.reader(csv_file)
        for line in csv_reader:
            try:
                if( ((float(line[0]) / 1e3) >= frequency_min_input) & ((float(line[1]) / 1e3) <= frequency_max_input)) :
                    freq_tera_fcc_alloc = line[0]+ " GHz " + " to " + line[1]+ "GHz" + " : " + line[2]
                    fcc_table.append(freq_tera_fcc_alloc)

            except ValueError:
                print("")

data_torender = {"path_llimit": json.dumps(restricted_min_path_loss_value), "path_ulimit":json.dumps(restricted_max_path_loss_value), "fc_table":json.dumps(fcc_table)} 

用于接收数据并使用Java脚本解析的HTML文件

var path_llimit = JSON.parse("{{path_llimit}}");
var path_ulimit = JSON.parse("{{path_ulimit}}");
var window_infoz = JSON.parse("{{fc_table}}");
console.log(window_infoz); 

控制台日志错误:

Uncaught SyntaxError: Unexpected token & in JSON at position 1
    at JSON.parse (<anonymous>)

当我单击它查看错误在哪里时...显示此行导致了错误

var window_infoz = JSON.parse("[&quot;1.0 THz  to 10.0 THz  : NotAll.&quot;]");

0 个答案:

没有答案