This language bar is your friend. Select your favorite languages!
Select your favorite languages :
- Or search :
JSONValue x = "data.json".readText.parseJSON;
struct User {
int age;
string name;
this(JSONValue user) {
age = user["age"];
name = user["name"];
}
}
auto user = User(x);
We could use the JSONValue x as it is, but if we want to fill a given structure the best is to define a specific constructor.
Map x = json.jsonDecode(new File('data.json').readAsStringSync());
x is a Map
defmodule JsonTest do
def get_json(filename) do
with {:ok, body} <- File.read(filename),
{:ok, json} <- Poison.decode(body), do: {:ok, json}
end
end
x = JsonTest.get_json("data.json")
Ensure {:poison, "~> version"} entered under deps in mix.exs
let x = ::serde_json::from_reader(File::open("data.json")?)?;
Requires x implements Deserialize from serde.
Serde is used to create an object from data, rather than populate an existing object.
Serde is used to create an object from data, rather than populate an existing object.