Hướng dẫn how do i parse json stringify in python? - làm cách nào để phân tích cú pháp json stringify trong python?

4

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

var jsData = {
    id:         'E1',
    firstname:  'Peter',
    lastname:   'Funny',
    project: { id: 'P1' },
    activities: [
        { id: 'A1' },
        { id: 'A2' }
    ]};

var jsonData = JSON.stringify(jsData);


$('#click').click(function(){

    $.ajax({
        url: "test/",
        type: "POST",
        data: jsData,
        dataType: "json",
        success: function (data){
        console.log(data);
        },
        error:function(){$('#text').html('FATAL_ERROR')}

    })
})

Đây là mã JS và JSDATA nên được gửi đến máy chủ (Python). Trên máy chủ, tôi nhận được một cái gì đó như {'{id:' e1 ', FirstName:' Peter ', LastName:' Funny ', Project: {id:' p1 '}, hoạt động: [{id:' a1 '}, {{ id: 'a2'}]}; ':' '}

Có cách nào thông minh để có được chuỗi 'Dict bên trong' ra khỏi 'Dict bên ngoài' không?!

Hỏi ngày 28 tháng 4 năm 2013 lúc 15:20Apr 28, 2013 at 15:20

Hướng dẫn how do i parse json stringify in python? - làm cách nào để phân tích cú pháp json stringify trong python?

1

Python có một thư viện phân tích cú pháp JSON tích hợp. Thêm

import json
personString = "{'{id:'E1',firstname:'Peter',... " # This would contain your JSON string
person = json.loads( personString ) # This will parse the string into a native Python dictionary, be sure to add some error handling should a parsing error occur

person['firstname'] # Yields 'Peter'
person['activities'] # Yields a list with the activities.
5 cung cấp chức năng phân tích cú pháp JSON cơ bản, có thể được sử dụng như sau:

import json
personString = "{'{id:'E1',firstname:'Peter',... " # This would contain your JSON string
person = json.loads( personString ) # This will parse the string into a native Python dictionary, be sure to add some error handling should a parsing error occur

person['firstname'] # Yields 'Peter'
person['activities'] # Yields a list with the activities.

Thông tin thêm ở đây: http://docs.python.org/2/l Library/json.html

Đã trả lời ngày 28 tháng 4 năm 2013 lúc 15:28Apr 28, 2013 at 15:28

2

Bởi vì bạn đang sử dụng sai biến!

var jsonData = JSON.stringify(jsData);
..
$.ajax({
      ..,
      contentType: "application/json", //Remember to set this.
      data: jsData,
            ^^^^^^ => Shouldn't you be passing "jsonData" here?

Khi bạn vượt qua một từ điển JavaScript đơn giản, JQuery sẽ mã hóa các khóa và giá trị theo định dạng mã hóa phần trăm. Đó là lý do tại sao bạn đang nhìn thấy chế độ bên trong như chuỗi.

Những gì bạn phải làm lý tưởng (IMO), là vượt qua chuỗi JSON thay vì chuỗi được mã hóa phần trăm một phần.

Lưu ý rằng bạn có thể sẽ phải thay đổi cách máy chủ của bạn đang đọc dữ liệu. Theo cách này, sẽ không còn có tham số yêu cầu HTTP/POST. Chỉ là một chuỗi JSON đơn giản trong phần thực thể HTTP.

Đã trả lời ngày 28 tháng 4 năm 2013 lúc 15:46Apr 28, 2013 at 15:46

Hướng dẫn how do i parse json stringify in python? - làm cách nào để phân tích cú pháp json stringify trong python?

UltrainStincultrainStinctUltraInstinct

42.3K12 Huy hiệu vàng78 Huy hiệu bạc102 Huy hiệu đồng12 gold badges78 silver badges102 bronze badges

1

Thay thế điều này

$('#click').click(function(){
    $.ajax({
        url: "test/",
        type: "POST",
        data: jsData,
        dataType: "json",
        success: function (data){
        console.log(data);
        },
        error:function(){$('#text').html('FATAL_ERROR')}

    })
})

Với cái này

$('#click').click(function(){
    $.ajax({
        url: "test/",
        type: "POST",
        data: {"jsData": jsData },
        dataType: "json",
        success: function (data){
        console.log(data);
        },
        error:function(){$('#text').html('FATAL_ERROR')}

    })
})

Để bây giờ bạn sẽ có thể nhận được jsdata trong yêu cầu.post.get ('jsdata') hoặc requst.get.get ('jsdata') và bạn chỉ cần phân tích dữ liệu mà bạn nhận được trong khóa jsdata .

Và bạn tốt để đi.

Đã trả lời ngày 23 tháng 6 lúc 12:06Jun 23 at 12:06

Hướng dẫn how do i parse json stringify in python? - làm cách nào để phân tích cú pháp json stringify trong python?

Giới thiệu

Đối tượng

import json
personString = "{'{id:'E1',firstname:'Peter',... " # This would contain your JSON string
person = json.loads( personString ) # This will parse the string into a native Python dictionary, be sure to add some error handling should a parsing error occur

person['firstname'] # Yields 'Peter'
person['activities'] # Yields a list with the activities.
6, có sẵn trong tất cả các trình duyệt hiện đại, có hai phương pháp hữu ích để đối phó với nội dung có định dạng JSON:
import json
personString = "{'{id:'E1',firstname:'Peter',... " # This would contain your JSON string
person = json.loads( personString ) # This will parse the string into a native Python dictionary, be sure to add some error handling should a parsing error occur

person['firstname'] # Yields 'Peter'
person['activities'] # Yields a list with the activities.
7 và
import json
personString = "{'{id:'E1',firstname:'Peter',... " # This would contain your JSON string
person = json.loads( personString ) # This will parse the string into a native Python dictionary, be sure to add some error handling should a parsing error occur

person['firstname'] # Yields 'Peter'
person['activities'] # Yields a list with the activities.
8.

import json personString = "{'{id:'E1',firstname:'Peter',... " # This would contain your JSON string person = json.loads( personString ) # This will parse the string into a native Python dictionary, be sure to add some error handling should a parsing error occur person['firstname'] # Yields 'Peter' person['activities'] # Yields a list with the activities. 9

import json
personString = "{'{id:'E1',firstname:'Peter',... " # This would contain your JSON string
person = json.loads( personString ) # This will parse the string into a native Python dictionary, be sure to add some error handling should a parsing error occur

person['firstname'] # Yields 'Peter'
person['activities'] # Yields a list with the activities.
9 lấy một chuỗi JSON và biến nó thành một đối tượng JavaScript.

let userStr = '{"name":"Sammy","email":"","plan":"Pro"}';

let userObj = JSON.parse(userStr);

console.log(userObj);

Thực hiện mã này sẽ tạo ra đầu ra sau:

Output

{name: 'Sammy', email: '', plan: 'Pro'} email: "" name: "Sammy" plan: "Pro"

Dấu phẩy không có giá trị trong JSON, vì vậy

import json
personString = "{'{id:'E1',firstname:'Peter',... " # This would contain your JSON string
person = json.loads( personString ) # This will parse the string into a native Python dictionary, be sure to add some error handling should a parsing error occur

person['firstname'] # Yields 'Peter'
person['activities'] # Yields a list with the activities.
9 đã ném lỗi nếu chuỗi được truyền cho nó có dấu phẩy.

import json
personString = "{'{id:'E1',firstname:'Peter',... " # This would contain your JSON string
person = json.loads( personString ) # This will parse the string into a native Python dictionary, be sure to add some error handling should a parsing error occur

person['firstname'] # Yields 'Peter'
person['activities'] # Yields a list with the activities.
9 có thể lấy một hàm như một đối số thứ hai có thể biến đổi các giá trị đối tượng trước khi chúng được trả về.

Ở đây, các giá trị của đối tượng được chuyển đổi thành chữ hoa trong đối tượng được trả về của phương thức

import json
personString = "{'{id:'E1',firstname:'Peter',... " # This would contain your JSON string
person = json.loads( personString ) # This will parse the string into a native Python dictionary, be sure to add some error handling should a parsing error occur

person['firstname'] # Yields 'Peter'
person['activities'] # Yields a list with the activities.
7:

let userStr = '{"name":"Sammy","email":"","plan":"Pro"}';

let userObj = JSON.parse(userStr, (key, value) => {
  if (typeof value === 'string') {
    return value.toUpperCase();
  }
  return value;
});

console.log(userObj);

Thực hiện mã này sẽ tạo ra đầu ra sau:

Output

{name: 'SAMMY', email: '', plan: 'PRO'} email: "" name: "SAMMY" plan: "PRO"

Dấu phẩy không có giá trị trong JSON, vì vậy

import json
personString = "{'{id:'E1',firstname:'Peter',... " # This would contain your JSON string
person = json.loads( personString ) # This will parse the string into a native Python dictionary, be sure to add some error handling should a parsing error occur

person['firstname'] # Yields 'Peter'
person['activities'] # Yields a list with the activities.
9 đã ném lỗi nếu chuỗi được truyền cho nó có dấu phẩy.

import json personString = "{'{id:'E1',firstname:'Peter',... " # This would contain your JSON string person = json.loads( personString ) # This will parse the string into a native Python dictionary, be sure to add some error handling should a parsing error occur person['firstname'] # Yields 'Peter' person['activities'] # Yields a list with the activities. 9 có thể lấy một hàm như một đối số thứ hai có thể biến đổi các giá trị đối tượng trước khi chúng được trả về.

Ở đây, các giá trị của đối tượng được chuyển đổi thành chữ hoa trong đối tượng được trả về của phương thức

import json
personString = "{'{id:'E1',firstname:'Peter',... " # This would contain your JSON string
person = json.loads( personString ) # This will parse the string into a native Python dictionary, be sure to add some error handling should a parsing error occur

person['firstname'] # Yields 'Peter'
person['activities'] # Yields a list with the activities.
7:

let userObj = {
  name: "Sammy",
  email: "",
  plan: "Pro"
};

let userStr = JSON.stringify(userObj);

console.log(userStr);

Thực hiện mã này sẽ tạo ra đầu ra sau:

import json
personString = "{'{id:'E1',firstname:'Peter',... " # This would contain your JSON string
person = json.loads( personString ) # This will parse the string into a native Python dictionary, be sure to add some error handling should a parsing error occur

person['firstname'] # Yields 'Peter'
person['activities'] # Yields a list with the activities.
0

Dấu phẩy không có giá trị trong JSON, vì vậy

import json
personString = "{'{id:'E1',firstname:'Peter',... " # This would contain your JSON string
person = json.loads( personString ) # This will parse the string into a native Python dictionary, be sure to add some error handling should a parsing error occur

person['firstname'] # Yields 'Peter'
person['activities'] # Yields a list with the activities.
9 đã ném lỗi nếu chuỗi được truyền cho nó có dấu phẩy.

import json
personString = "{'{id:'E1',firstname:'Peter',... " # This would contain your JSON string
person = json.loads( personString ) # This will parse the string into a native Python dictionary, be sure to add some error handling should a parsing error occur

person['firstname'] # Yields 'Peter'
person['activities'] # Yields a list with the activities.
9 có thể lấy một hàm như một đối số thứ hai có thể biến đổi các giá trị đối tượng trước khi chúng được trả về.

import json
personString = "{'{id:'E1',firstname:'Peter',... " # This would contain your JSON string
person = json.loads( personString ) # This will parse the string into a native Python dictionary, be sure to add some error handling should a parsing error occur

person['firstname'] # Yields 'Peter'
person['activities'] # Yields a list with the activities.
1

Thực hiện mã này sẽ tạo ra đầu ra sau:

import json
personString = "{'{id:'E1',firstname:'Peter',... " # This would contain your JSON string
person = json.loads( personString ) # This will parse the string into a native Python dictionary, be sure to add some error handling should a parsing error occur

person['firstname'] # Yields 'Peter'
person['activities'] # Yields a list with the activities.
2

Dấu phẩy không có giá trị trong JSON, vì vậy

import json
personString = "{'{id:'E1',firstname:'Peter',... " # This would contain your JSON string
person = json.loads( personString ) # This will parse the string into a native Python dictionary, be sure to add some error handling should a parsing error occur

person['firstname'] # Yields 'Peter'
person['activities'] # Yields a list with the activities.
9 đã ném lỗi nếu chuỗi được truyền cho nó có dấu phẩy.

import json
personString = "{'{id:'E1',firstname:'Peter',... " # This would contain your JSON string
person = json.loads( personString ) # This will parse the string into a native Python dictionary, be sure to add some error handling should a parsing error occur

person['firstname'] # Yields 'Peter'
person['activities'] # Yields a list with the activities.
9 có thể lấy một hàm như một đối số thứ hai có thể biến đổi các giá trị đối tượng trước khi chúng được trả về.

import json
personString = "{'{id:'E1',firstname:'Peter',... " # This would contain your JSON string
person = json.loads( personString ) # This will parse the string into a native Python dictionary, be sure to add some error handling should a parsing error occur

person['firstname'] # Yields 'Peter'
person['activities'] # Yields a list with the activities.
3

Thực hiện mã này sẽ tạo ra đầu ra sau:

import json
personString = "{'{id:'E1',firstname:'Peter',... " # This would contain your JSON string
person = json.loads( personString ) # This will parse the string into a native Python dictionary, be sure to add some error handling should a parsing error occur

person['firstname'] # Yields 'Peter'
person['activities'] # Yields a list with the activities.
4

Các vết lõm đã được thay thế bằng

$('#click').click(function(){
    $.ajax({
        url: "test/",
        type: "POST",
        data: jsData,
        dataType: "json",
        success: function (data){
        console.log(data);
        },
        error:function(){$('#text').html('FATAL_ERROR')}

    })
})
4.

Sự kết luận

Trong hướng dẫn này, bạn đã sử dụng các phương thức

import json
personString = "{'{id:'E1',firstname:'Peter',... " # This would contain your JSON string
person = json.loads( personString ) # This will parse the string into a native Python dictionary, be sure to add some error handling should a parsing error occur

person['firstname'] # Yields 'Peter'
person['activities'] # Yields a list with the activities.
9 và
var jsonData = JSON.stringify(jsData);
..
$.ajax({
      ..,
      contentType: "application/json", //Remember to set this.
      data: jsData,
            ^^^^^^ => Shouldn't you be passing "jsonData" here?
4. Nếu bạn muốn tìm hiểu thêm về việc làm việc với JSON trong JavaScript, hãy xem Cách làm việc của chúng tôi với JSON trong hướng dẫn JavaScript.

Để biết thêm thông tin về mã hóa trong JavaScript, hãy xem Cách viết mã của chúng tôi trong loạt JavaScript hoặc xem trang chủ đề JavaScript của chúng tôi cho các bài tập và dự án lập trình.

Chức năng của JSON Stringify và JSON parse () là gì?

Hàm json.parse () được sử dụng để chuyển đổi chuỗi thành đối tượng javascript trong khi hàm json.Stringify () được sử dụng để chuyển đổi đối tượng javascript thành một chuỗi. parse() function is used to convert a string into a JavaScript object while the JSON. stringify() function is used to convert a JavaScript object into a string.

Phương thức JSON Stringify () làm gì?

Phương thức json.Stringify () chuyển đổi giá trị javascript thành chuỗi JSON, tùy chọn thay thế các giá trị nếu hàm thay thế được chỉ định hoặc tùy chọn chỉ bao gồm các thuộc tính được chỉ định nếu một mảng thay thế được chỉ định.

Làm cách nào để phân tích một chuỗi trong JSON?

Ví dụ - phân tích cú pháp JSON Sử dụng hàm javascript json.parse () để chuyển đổi văn bản thành đối tượng javascript: const obj = json.parse ('{"name": "john", "tuổi": 30, "thành phố": "mớiYork "} ');Hãy chắc chắn rằng văn bản ở định dạng JSON, nếu không bạn sẽ gặp lỗi cú pháp.Use the JavaScript function JSON.parse() to convert text into a JavaScript object: const obj = JSON.parse('{"name":"John", "age":30, "city":"New York"}'); Make sure the text is in JSON format, or else you will get a syntax error.

Làm thế nào để bạn đọc một biến JSON trong Python?

Đọc từ Json Python có một gói tích hợp có tên JSON, có thể được sử dụng để làm việc với dữ liệu JSON.Nó được thực hiện bằng cách sử dụng mô -đun JSON, cung cấp cho chúng tôi rất nhiều phương thức trong số các phương thức tải () và tải () sẽ giúp chúng tôi đọc tệp JSON.using the JSON module, which provides us with a lot of methods which among loads() and load() methods are gonna help us to read the JSON file.