Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial sample for writable JSON DOM
- Loading branch information
Showing
5 changed files
with
58 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Writable DOM | ||
|
||
This sample shows how to parse and access number, string and an array values from JSON string. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<LangVersion>preview</LangVersion> | ||
</PropertyGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Hosting; | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
|
||
WebApplication app = WebApplication.Create(); | ||
|
||
app.Run(async context => | ||
{ | ||
var numberNode = JsonNode.Parse(@"{""age"" : 34 }"); | ||
int age = (int) numberNode["age"]; | ||
await context.Response.WriteAsync($"{numberNode} \n"); | ||
await context.Response.WriteAsync($"Value of age is {age}\n\n\n"); | ||
var stringNode = JsonNode.Parse(@"{""name"" : ""anne""}"); | ||
string name = (string) stringNode["name"]; | ||
await context.Response.WriteAsync($"{stringNode} \n"); | ||
await context.Response.WriteAsync($"Value of name is {name}\n\n\n"); | ||
var numberArrayNode = JsonNode.Parse("[2, 4, 6]"); | ||
await context.Response.WriteAsync($"{numberArrayNode}\n"); | ||
int item0 = (int) numberArrayNode[0]; | ||
int item1 = (int) numberArrayNode[1]; | ||
int item2 = (int) numberArrayNode[2]; | ||
await context.Response.WriteAsync($"Values of array: {item0}, {item1}, {item2}\n\n"); | ||
//alternatively | ||
JsonArray childrenAge = numberArrayNode.AsArray(); | ||
await context.Response.WriteAsync($"Size of array: {childrenAge.Count}\nValues of array: "); | ||
foreach(JsonNode r in childrenAge) | ||
{ | ||
await context.Response.WriteAsync($"{r}, "); | ||
} | ||
}); | ||
|
||
await app.RunAsync(); |