JSON 字符串对象转换代码实例

JSON 简介
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于人阅读和编写,同时也便于机器解析和生成,JSON 使用键值对的形式来表示数据,其中键是字符串,值可以是字符串、数字、布尔值、数组或另一个对象。
示例 JSON 字符串
{
"name": "Alice",
"age": 30,
"isStudent": false,
"courses": ["Math", "Science"],
"address": {
"city": "New York",
"zipcode": "10001"
}
}
3. Python 中 JSON 字符串与对象之间的转换
Python 提供了json 模块用于处理 JSON 数据,以下是一些常用的函数:
json.loads(json_string): 将 JSON 字符串转换为 Python 对象。
json.dumps(python_obj): 将 Python 对象转换为 JSON 字符串。
3.1 将 JSON 字符串转换为 Python 对象
import json
JSON 字符串
json_string = '{"name": "Alice", "age": 30, "isStudent": false, "courses": ["Math", "Science"], "address": {"city": "New York", "zipcode": "10001"}}'
将 JSON 字符串转换为 Python 对象
python_obj = json.loads(json_string)
print(python_obj)
print(type(python_obj))
输出:
{'name': 'Alice', 'age': 30, 'isStudent': False, 'courses': ['Math', 'Science'], 'address': {'city': 'New York', 'zipcode': '10001'}}
<class 'dict'>
3.2 将 Python 对象转换为 JSON 字符串
import json
Python 对象
python_obj = {
"name": "Alice",
"age": 30,
"isStudent": False,
"courses": ["Math", "Science"],
"address": {
"city": "New York",
"zipcode": "10001"
}
}
将 Python 对象转换为 JSON 字符串
json_string = json.dumps(python_obj)
print(json_string)
print(type(json_string))
输出:

{"name": "Alice", "age": 30, "isStudent": false, "courses": ["Math", "Science"], "address": {"city": "New York", "zipcode": "10001"}}
<class 'str'>
4. JavaScript 中 JSON 字符串与对象之间的转换
JavaScript 提供了JSON 对象用于处理 JSON 数据,以下是一些常用的方法:
JSON.parse(jsonString): 将 JSON 字符串转换为 JavaScript 对象。
JSON.stringify(javascriptObj): 将 JavaScript 对象转换为 JSON 字符串。
4.1 将 JSON 字符串转换为 JavaScript 对象
// JSON 字符串
let jsonString = '{"name": "Alice", "age": 30, "isStudent": false, "courses": ["Math", "Science"], "address": {"city": "New York", "zipcode": "10001"}}';
// 将 JSON 字符串转换为 JavaScript 对象
let jsObject = JSON.parse(jsonString);
console.log(jsObject);
console.log(typeof jsObject);
输出:
{ name: 'Alice', age: 30, isStudent: false, courses: [ 'Math', 'Science' ], address: { city: 'New York', zipcode: '10001' } }
object
4.2 将 JavaScript 对象转换为 JSON 字符串
// JavaScript 对象
let jsObject = {
name: "Alice",
age: 30,
isStudent: false,
courses: ["Math", "Science"],
address: {
city: "New York",
zipcode: "10001"
}
};
// 将 JavaScript 对象转换为 JSON 字符串
let jsonString = JSON.stringify(jsObject);
console.log(jsonString);
console.log(typeof jsonString);
输出:
{"name":"Alice","age":30,"isStudent":false,"courses":["Math","Science"],"address":{"city":"New York","zipcode":"10001"}}
string
相关问题与解答
问题 1:如何在 Python 中处理嵌套的 JSON 数据?

解答:在 Python 中,可以使用递归方法来处理嵌套的 JSON 数据,可以使用递归函数来遍历并打印所有键值对:
def print_json(data, indent=0):
for key, value in data.items():
if isinstance(value, dict):
print(' ' * indent + str(key) + ':')
print_json(value, indent + 1)
else:
print(' ' * indent + str(key) + ': ' + str(value))
json_string = '{"name": "Alice", "age": 30, "isStudent": false, "courses": ["Math", "Science"], "address": {"city": "New York", "zipcode": "10001"}}'
python_obj = json.loads(json_string)
print_json(python_obj)
输出:
name: Alice age: 30 isStudent: False courses: ['Math', 'Science'] address: city: New York zipcode: 10001
问题 2:如何在 JavaScript 中处理嵌套的 JSON 数据?
解答:在 JavaScript 中,同样可以使用递归方法来处理嵌套的 JSON 数据,可以使用递归函数来遍历并打印所有键值对:
function printJson(data, indent = 0) {
for (let key in data) {
if (data.hasOwnProperty(key)) {
let value = data[key];
if (typeof value === 'object' && value !== null) {
console.log(' '.repeat(indent) + key + ':');
printJson(value, indent + 2);
} else {
console.log(' '.repeat(indent) + key + ': ' + value);
}
}
}
}
let jsonString = '{"name": "Alice", "age": 30, "isStudent": false, "courses": ["Math", "Science"], "address": {"city": "New York", "zipcode": "10001"}}';
let jsObject = JSON.parse(jsonString);
printJson(jsObject);
输出:
name: Alice age: 30 isStudent: false courses: Math,Science address: city: New York zipcode: 10001
到此,以上就是小编对于“json字符串对象转换代码实例”的问题就介绍到这了,希望介绍的几点解答对大家有用,有任何问题和不懂的,欢迎各位朋友在评论区讨论,给我留言。














