JSON: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 213:
}</lang>
<pre>{"foo":1,"bar":[10,"apples"]}</pre>
 
=={{header|Factor}}==
<lang Factor>
USING: json.writer json.reader ;
 
SYMBOL: foo
 
! Load a JSON string into a data structure
"[[\"foo\",1],[\"bar\",[10,\"apples\"]]]" json> foo set
 
 
! Create a new data structure and serialize into JSON
{ { "blue" { "ocean" "water" } } >json
</lang>
 
=={{header|Fantom}}==
 
<lang fantom>
using util
 
class Json
{
public static Void main ()
{
Str input := """{"blue": [1, 2], "ocean": "water"}"""
Map jsonObj := JsonInStream(input.in).readJson
 
echo ("Value for 'blue' is: " + jsonObj["blue"])
jsonObj["ocean"] = ["water":["cold", "blue"]]
Map ocean := jsonObj["ocean"]
echo ("Value for 'ocean/water' is: " + ocean["water"])
output := JsonOutStream(Env.cur.out)
output.writeJson(jsonObj)
echo ()
}
}
</lang>
 
Output:
<pre>
Value for 'blue' is: [1, 2]
Value for 'ocean/water' is: [cold, blue]
{"blue":[1,2],
"ocean":{"water":["cold","blue"]}}
</pre>
 
=={{header|EGL}}==
 
Structures used both to construct and to parse JSON strings:
<lang EGL>record familyMember
record familyMember
person person;
relationships relationship[]?;
Line 277 ⟶ 230:
relationshipType string;
id int;
end</lang>
</lang>
 
Construct JSON string:
<lang EGL>people Person[]; // Array of people
people Person[]; // Array of people
people.appendElement(new Person { firstName = "Frederick", lastName = "Flintstone", age = 35} );
Line 291 ⟶ 242:
people.appendElement(new Person { firstName = "Bam Bam", lastName = "Rubble", age = 2} );
family Dictionary; // ConstructA dictionary of family members using a uid as key
family["1"] = new FamilyMember{ person = people[1], relationships = [new Relationship{ relationshipType="spouse", id = 2 }, new Relationship{ relationshipType="child", id = 3}] };
family["2"] = new FamilyMember{ person = people[2], relationships = [new Relationship{ relationshipType="spouse", id = 1 }, new Relationship{ relationshipType="child", id = 3}] };
Line 299 ⟶ 250:
family["6"] = new FamilyMember{ person = people[6], relationships = [new Relationship{ relationshipType="mother", id = 5 }, new Relationship{ relationshipType="father", id = 4}] };
// Convert dictionary of family members to JSON string
jsonString string = jsonLib.convertToJSON(family);
 
// Show JSON string
SysLib.writeStdout(jsonString);</lang>
</lang>
 
Raw Output:
<pre>{
{
"1":{"person":{"firstName":"Frederick","lastName":"Flintstone","age":35},"relationships":[{"relationshipType":"spouse","id":2},{"relationshipType":"child","id":3}]},
"2":{"person":{"firstName":"Wilma","lastName":"Flintstone","age":34},"relationships":[{"relationshipType":"spouse","id":1},{"relationshipType":"child","id":3}]},
Line 315 ⟶ 264:
"5":{"person":{"firstName":"Elizabeth","lastName":"Rubble","age":29},"relationships":[{"relationshipType":"spouse","id":4},{"relationshipType":"child","id":6}]},
"6":{"person":{"firstName":"Bam Bam","lastName":"Rubble","age":2},"relationships":[{"relationshipType":"mother","id":5},{"relationshipType":"father","id":4}]}
}</pre>
}
</pre>
 
Validated Output (partial):
<pre>{
{
"1": {
"person": {
Line 342 ⟶ 289:
 
Parse JSON:
<lang EGL>// Convert JSON string into dictionary of family members
<lang EGL>
// Convert JSON string into dictionary of family members
family Dictionary;
jsonLib.convertFromJSON(jsonString, family);
Line 356 ⟶ 302:
SysLib.writeStdout("----------------------------------------------------");
 
SysLib.writeStdout(keys[i]);
familyMember = family[keys[i]];
Line 365 ⟶ 310:
relation = family[id];
SysLib.writeStdout(familyMember.relationships[j].relationshipType + ": " +
relation.person.lastName + ", " + relation.person.firstName + " - " + relation.person.age);
end
 
end</lang>
 
Output:
<pre>Flintstone, Frederick - 35
spouse: Flintstone, Wilma
child: Flintstone, Pebbles
----------------------------------------------------
Flintstone, Wilma - 34
spouse: Flintstone, Frederick
child: Flintstone, Pebbles
----------------------------------------------------
Flintstone, Pebbles - 2
mother: Flintstone, Wilma
father: Flintstone, Frederick
----------------------------------------------------
Rubble, Bernard - 32
spouse: Rubble, Elizabeth
child: Rubble, Bam Bam
----------------------------------------------------
Rubble, Elizabeth - 29
spouse: Rubble, Bernard
child: Rubble, Bam Bam
----------------------------------------------------
Rubble, Bam Bam - 2
mother: Rubble, Elizabeth
father: Rubble, Bernard</pre>
 
=={{header|Factor}}==
<lang Factor>
USING: json.writer json.reader ;
 
SYMBOL: foo
 
! Load a JSON string into a data structure
"[[\"foo\",1],[\"bar\",[10,\"apples\"]]]" json> foo set
 
 
! Create a new data structure and serialize into JSON
{ { "blue" { "ocean" "water" } } >json
</lang>
 
=={{header|Fantom}}==
 
<lang fantom>
using util
 
class Json
{
public static Void main ()
{
Str input := """{"blue": [1, 2], "ocean": "water"}"""
Map jsonObj := JsonInStream(input.in).readJson
 
echo ("Value for 'blue' is: " + jsonObj["blue"])
jsonObj["ocean"] = ["water":["cold", "blue"]]
Map ocean := jsonObj["ocean"]
echo ("Value for 'ocean/water' is: " + ocean["water"])
output := JsonOutStream(Env.cur.out)
output.writeJson(jsonObj)
echo ()
}
}
</lang>
 
Output:
<pre>
Value for 'blue' is: [1, 2]
Value for 'ocean/water' is: [cold, blue]
{"blue":[1,2],
"ocean":{"water":["cold","blue"]}}
</pre>
 
=={{header|Go}}==
Anonymous user