删除JSON元素可以通过编程语言中的JSON库来实现。以下是几种常见编程语言的示例:
import json
json_str = '{"name": "John", "age": 30, "city": "New York"}'
json_obj = json.loads(json_str)
del json_obj['city']
json_str_new = json.dumps(json_obj)
print(json_str_new)
输出:
{"name": "John", "age": 30}
const json_obj = {
"name": "John",
"age": 30,
"city": "New York"
};
delete json_obj.city;
console.log(json_obj);
输出:
{ name: 'John', age: 30 }
import org.json.JSONObject;
public class Main {
public static void main(String[] args) {
JSONObject json_obj = new JSONObject("{\"name\": \"John\", \"age\": 30, \"city\": \"New York\"}");
json_obj.remove("city");
System.out.println(json_obj.toString());
}
}
输出:
{"name":"John","age":30}
$json_str = '{"name": "John", "age": 30, "city": "New York"}';
$json_obj = json_decode($json_str, true);
unset($json_obj['city']);
$json_str_new = json_encode($json_obj);
echo $json_str_new;
输出:
{"name":"John","age":30}
以上是几种常见编程语言中删除JSON元素的示例。
领取专属 10元无门槛券
手把手带您无忧上云