|
Oktatás
* Programozás 1 + feladatsor + GitHub oldal
* Szkriptnyelvek + feladatsor + quick link
Teaching
• Programming 1 (BI) ▸ exercises ▸ quick link
teaching assets
Félévek
* 2025/26/2 * archívum
Linkek
* kalendárium * tételsorok * jegyzetek * szakdolgozat / PhD * ösztöndíjak * certificates * C lang. * C# * D lang. * Java * Nim * Nim2 + exercises * XC=BASIC * old ✦C++, ✦Clojure, ✦Scala
[ edit | logout ] [ sandbox | passwd ]
|
| (1) create JSON
|
import std.stdio;
import std.json;
void main()
{
// Method 1: Using parseJSON (often easier)
JSONValue person1 = parseJSON(`{
"name": "John",
"age": 30,
"isStudent": false,
"hobbies": ["reading", "gaming", "coding"]
}`);
writeln(person1); // person1.toString() is called automatically
// Method 2: Using JSONValue constructor functions
JSONValue person2 = [
"name": JSONValue("John"),
"age": JSONValue(30),
"isStudent": JSONValue(false),
"hobbies": JSONValue(["reading", "gaming", "coding"])
];
writeln(person2);
}
|
Output:
{"age":30,"hobbies":["reading","gaming","coding"],"isStudent":false,"name":"John"}
{"age":30,"hobbies":["reading","gaming","coding"],"isStudent":false,"name":"John"}
| (2) write JSON to file
|
import std.stdio;
import std.json;
void main()
{
JSONValue person = [
"name": JSONValue("John"),
"age": JSONValue(30),
"isStudent": JSONValue(false),
"hobbies": JSONValue(["reading", "gaming", "coding"])
];
writeln(person);
File f = File("person.json", "w");
// v1
// f.writeln(person);
// v2
// f.writeln(person.toString());
// v3
f.writeln(person.toPrettyString());
f.close();
}
|
Output of v1 and v2 (they're equivalent):
{"age":30,"hobbies":["reading","gaming","coding"],"isStudent":false,"name":"John"}
Output of v3:
{
"age": 30,
"hobbies": [
"reading",
"gaming",
"coding"
],
"isStudent": false,
"name": "John"
}
You could also write to a .json file with a one-liner:
File("person.json", "w").writeln(person);
|
Blogjaim, hobbi projektjeim
* The Ubuntu Incident * Python Adventures * @GitHub * heroku * extra * haladó Python * YouTube listák
Debrecen | la France
[ edit ]
|