#1 Working with JSON Data
The samples can differ based on your Razor base class or if you're running an old version.
Selected: Typed (2sxc 16+) Switch to Dynamic (Razor14 or below)
Selected: Typed (2sxc 16+) Switch to Dynamic (Razor14 or below)
Working with JSON Data
JSON data can be tricky. The easiest way is to convert it to a dynamic object using AsDynamic(string)
. Then you can easily use it in your code.
It's important to note that in Typed mode everything is much more strict.
Since JSON itself is very flexible, you must know what the JSON will contain, to write code which is really type safe. As an alternative you can also always parse it using System.Text.Json
.
⬇️ Result | Source ➡️
- Title: This is a JSON title
- IsCool: True
- Sub-item title: sub-item title
- Is this a list/array? False
- Are the tags a list? True
⬇️ Result | Source ➡️
System.Collections.Generic.List`1[System.Object]
- some tag
- another tag
- a third tag
⬇️ Result | Source ➡️
-
title (System.String)
= This is a JSON title -
isCool (System.Boolean)
= True -
subItem (ToSic.Sxc.Data.Internal.Typed.WrapObjectTyped)
= { "title": "sub-item title" } -
tags (System.Collections.Generic.List`1[System.Object])
= System.Collections.Generic.List`1[System.Object]
#1 Working with JSON Data