dlang语言对应php中jsonencode和jsondecode的函数给出几个典型的完整可以运行的例子已经知道stdjson的sjsonStringify好像不存在请给出最新2020年以后最新语法版本我知道stdjsonJSONValue已经存在并且stdjsonparseJSON也存在
- 使用std.json.JSONValue进行JSON编码:
import std.json;
void main()
{
JSONValue json = ["apple", "banana", "orange"];
string jsonString = json.toString();
writeln(jsonString);
}
输出:
["apple","banana","orange"]
- 使用std.json.parseJSON进行JSON解码:
import std.json;
void main()
{
string jsonString = `{"name": "John", "age": 30, "city": "New York"}`;
JSONValue json = parseJSON(jsonString);
string name = json["name"].str;
int age = json["age"].get!int;
string city = json["city"].str;
writeln(name, ", ", age, ", ", city);
}
输出:
John, 30, New York
- 使用std.json.JSONValue的构造函数进行JSON编码:
import std.json;
void main()
{
JSONValue json = JSONValue();
json["name"] = "John";
json["age"] = 30;
json["city"] = "New York";
string jsonString = json.toString();
writeln(jsonString);
}
输出:
{"name":"John","age":30,"city":"New York"}
``
原文地址: https://www.cveoy.top/t/topic/fo7M 著作权归作者所有。请勿转载和采集!