首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将JSON字符串转换为字典Not列表

将JSON字符串转换为字典Not列表
EN

Stack Overflow用户
提问于 2013-10-21 05:56:21
回答 3查看 603K关注 0票数 247

我尝试传入一个JSON文件并将数据转换为字典。

到目前为止,我所做的是:

代码语言:javascript
复制
import json
json1_file = open('json1')
json1_str = json1_file.read()
json1_data = json.loads(json1_str)

我期望json1_data是一个dict类型,但当我用type(json1_data)检查它时,它实际上是一个list类型。

我遗漏了什么?我需要这是一个字典,这样我就可以访问其中的一个密钥。

EN

回答 3

Stack Overflow用户

发布于 2019-04-22 03:00:38

您可以使用以下内容:

代码语言:javascript
复制
import json

 with open('<yourFile>.json', 'r') as JSON:
       json_dict = json.load(JSON)

 # Now you can use it like dictionary
 # For example:

 print(json_dict["username"])
票数 12
EN

Stack Overflow用户

发布于 2019-11-20 19:47:05

我正在使用一个REST API的Python代码,所以这是为那些在类似项目中工作的人准备的。

我使用POST请求从URL提取数据,原始输出为JSON。由于某些原因,输出已经是一个字典,而不是一个列表,我能够立即引用嵌套的字典键,如下所示:

代码语言:javascript
复制
datapoint_1 = json1_data['datapoints']['datapoint_1']

datapoint_1在数据点字典中的位置。

票数 1
EN

Stack Overflow用户

发布于 2019-05-10 20:08:58

使用javascript ajax从get方法传递数据。

代码语言:javascript
复制
    **//javascript function    
    function addnewcustomer(){ 
    //This function run when button click
    //get the value from input box using getElementById
            var new_cust_name = document.getElementById("new_customer").value;
            var new_cust_cont = document.getElementById("new_contact_number").value;
            var new_cust_email = document.getElementById("new_email").value;
            var new_cust_gender = document.getElementById("new_gender").value;
            var new_cust_cityname = document.getElementById("new_cityname").value;
            var new_cust_pincode = document.getElementById("new_pincode").value;
            var new_cust_state = document.getElementById("new_state").value;
            var new_cust_contry = document.getElementById("new_contry").value;
    //create json or if we know python that is call dictionary.        
    var data = {"cust_name":new_cust_name, "cust_cont":new_cust_cont, "cust_email":new_cust_email, "cust_gender":new_cust_gender, "cust_cityname":new_cust_cityname, "cust_pincode":new_cust_pincode, "cust_state":new_cust_state, "cust_contry":new_cust_contry};
    //apply stringfy method on json
            data = JSON.stringify(data);
    //insert data into database using javascript ajax
            var send_data = new XMLHttpRequest();
            send_data.open("GET", "http://localhost:8000/invoice_system/addnewcustomer/?customerinfo="+data,true);
            send_data.send();

            send_data.onreadystatechange = function(){
              if(send_data.readyState==4 && send_data.status==200){
                alert(send_data.responseText);
              }
            }
          }

django视图

代码语言:javascript
复制
    def addNewCustomer(request):
    #if method is get then condition is true and controller check the further line
        if request.method == "GET":
    #this line catch the json from the javascript ajax.
            cust_info = request.GET.get("customerinfo")
    #fill the value in variable which is coming from ajax.
    #it is a json so first we will get the value from using json.loads method.
    #cust_name is a key which is pass by javascript json. 
    #as we know json is a key value pair. the cust_name is a key which pass by javascript json
            cust_name = json.loads(cust_info)['cust_name']
            cust_cont = json.loads(cust_info)['cust_cont']
            cust_email = json.loads(cust_info)['cust_email']
            cust_gender = json.loads(cust_info)['cust_gender']
            cust_cityname = json.loads(cust_info)['cust_cityname']
            cust_pincode = json.loads(cust_info)['cust_pincode']
            cust_state = json.loads(cust_info)['cust_state']
            cust_contry = json.loads(cust_info)['cust_contry']
    #it print the value of cust_name variable on server
            print(cust_name)
            print(cust_cont)
            print(cust_email)
            print(cust_gender)
            print(cust_cityname)
            print(cust_pincode)
            print(cust_state)
            print(cust_contry)
            return HttpResponse("Yes I am reach here.")**
票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19483351

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档