JSON: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 258: | Line 258: | ||
"ocean":{"water":["cold","blue"]}} |
"ocean":{"water":["cold","blue"]}} |
||
</pre> |
</pre> |
||
=={{header|EGL}}== |
|||
Structures used both to construct and to parse JSON: |
|||
<lang EGL> |
|||
record familyMember |
|||
person person; |
|||
relationships relationship[]?; |
|||
end |
|||
record person |
|||
firstName string; |
|||
lastName string; |
|||
age int; |
|||
end |
|||
record relationship |
|||
relationshipType string; |
|||
id int; |
|||
end |
|||
</lang> |
|||
Construct JSON string: |
|||
<lang EGL> |
|||
people Person[]; // Array of people |
|||
people.appendElement(new Person { firstName = "Frederick", lastName = "Flintstone", age = 35} ); |
|||
people.appendElement(new Person { firstName = "Wilma", lastName = "Flintstone", age = 34} ); |
|||
people.appendElement(new Person { firstName = "Pebbles", lastName = "Flintstone", age = 2} ); |
|||
people.appendElement(new Person { firstName = "Bernard", lastName = "Rubble", age = 32} ); |
|||
people.appendElement(new Person { firstName = "Elizabeth", lastName = "Rubble", age = 29} ); |
|||
people.appendElement(new Person { firstName = "Bam Bam", lastName = "Rubble", age = 2} ); |
|||
family Dictionary; // Construct dictionary using 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}] }; |
|||
family["3"] = new FamilyMember{ person = people[3], relationships = [new Relationship{ relationshipType="mother", id = 2 }, new Relationship{ relationshipType="father", id = 1}] }; |
|||
family["4"] = new FamilyMember{ person = people[4], relationships = [new Relationship{ relationshipType="spouse", id = 5 }, new Relationship{ relationshipType="child", id = 6}] }; |
|||
family["5"] = new FamilyMember{ person = people[5], relationships = [new Relationship{ relationshipType="spouse", id = 4 }, new Relationship{ relationshipType="child", id = 6}] }; |
|||
family["6"] = new FamilyMember{ person = people[6], relationships = [new Relationship{ relationshipType="mother", id = 5 }, new Relationship{ relationshipType="father", id = 4}] }; |
|||
// Convert family members to JSON |
|||
jsonString string = jsonLib.convertToJSON(family); |
|||
// Show JSON string |
|||
SysLib.writeStdout(jsonString); |
|||
</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}]}, |
|||
"3":{"person":{"firstName":"Pebbles","lastName":"Flintstone","age":2},"relationships":[{"relationshipType":"mother","id":2},{"relationshipType":"father","id":1}]}, |
|||
"4":{"person":{"firstName":"Bernard","lastName":"Rubble","age":32},"relationships":[{"relationshipType":"spouse","id":5},{"relationshipType":"child","id":6}]}, |
|||
"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> |
|||
Validated Output (partial): |
|||
<pre> |
|||
{ |
|||
"1": { |
|||
"person": { |
|||
"firstName": "Frederick", |
|||
"lastName": "Flintstone", |
|||
"age": 35 |
|||
}, |
|||
"relationships": [ |
|||
{ |
|||
"relationshipType": "spouse", |
|||
"id": 2 |
|||
}, |
|||
{ |
|||
"relationshipType": "child", |
|||
"id": 3 |
|||
} |
|||
] |
|||
}, |
|||
... |
|||
}</pre> |
|||
Parse JSON: |
|||
<lang EGL> |
|||
// Convert JSON string into dictionary of family members |
|||
family Dictionary; |
|||
jsonLib.convertFromJSON(jsonString, family); |
|||
// List family members and their relationships |
|||
familyMember FamilyMember; |
|||
relation FamilyMember; |
|||
keys string[] = family.getKeys(); |
|||
for(i int from 1 to keys.getSize()) |
|||
SysLib.writeStdout("----------------------------------------------------"); |
|||
SysLib.writeStdout(keys[i]); |
|||
familyMember = family[keys[i]]; |
|||
SysLib.writeStdout(familyMember.person.lastName + ", " + familyMember.person.firstName + " - " + familyMember.person.age); |
|||
for(j int from 1 to familyMember.relationships.getSize()) |
|||
id string = familyMember.relationships[j].id; |
|||
relation = family[id]; |
|||
SysLib.writeStdout(familyMember.relationships[j].relationshipType + ": " + |
|||
relation.person.lastName + ", " + relation.person.firstName + " - " + relation.person.age); |
|||
end |
|||
end |
|||
</lang> |
|||
=={{header|Go}}== |
=={{header|Go}}== |