Recent Changes - Search:

Oktatás

* Programozás 2
  + feladatsor
  + C feladatsor
  + Python feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

* Adator. prog.
  + feladatsor
  + quick link

Teaching

* Prog. for Data Sci. (TODO)

teaching assets


Félévek

* 2025/26/1
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Scala


[ edit | logout ]
[ sandbox | passwd ]

Create a JSON object

(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);
Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2025 August 21, 19:58