dlang语言对应php中jsonencode和jsondecode的函数给出几个典型的完整可以运行的例子
以下是对应php中json_encode和json_decode函数的dlang语言实现:
- json_encode函数:
import std.json;
void main()
{
auto data = ["name":"John", "age":30, "city":"New York"];
auto jsonString = data.jsonStringify;
writeln(jsonString); //输出:{"name":"John","age":30,"city":"New York"}
}
- json_decode函数:
import std.json;
void main()
{
auto jsonString = `{"name":"John","age":30,"city":"New York"}`;
auto data = jsonString.jsonParse;
writeln(data["name"]); //输出:John
writeln(data["age"]); //输出:30
writeln(data["city"]); //输出:New York
}
- 从文件中读取json数据并解析:
import std.json;
import std.stdio;
void main()
{
auto file = File("data.json", "r");
auto jsonString = file.readText();
file.close();
auto data = jsonString.jsonParse;
writeln(data); //输出解析后的json数据
}
``
原文地址: https://www.cveoy.top/t/topic/fo6b 著作权归作者所有。请勿转载和采集!