在不同的编程语言中,遍历JSON变量的方法可能有所不同。以下是一些常见编程语言的示例代码来遍历JSON变量:
import json
# JSON variable
json_data = '{"name": "John", "age": 30, "city": "New York"}'
# Parse JSON
data = json.loads(json_data)
# Traverse JSON
for key, value in data.items():
print(key, value)
// JSON variable
var jsonData = '{"name": "John", "age": 30, "city": "New York"}';
// Parse JSON
var data = JSON.parse(jsonData);
// Traverse JSON
for (var key in data) {
if (data.hasOwnProperty(key)) {
console.log(key, data[key]);
}
}
import org.json.JSONObject;
// JSON variable
String jsonData = "{\"name\": \"John\", \"age\": 30, \"city\": \"New York\"}";
// Parse JSON
JSONObject data = new JSONObject(jsonData);
// Traverse JSON
for (String key : data.keySet()) {
System.out.println(key + " " + data.get(key));
}
这些示例代码可以帮助你在不同的编程语言中遍历JSON变量。根据你使用的编程语言,你可以选择适当的代码示例。
上一篇:遍历JSONArray导致崩溃