IJsonDocument.ReadFromFile

Syntax

ReadFromFile(FileName: String);

ReadFromFile(FileName: String);

Parameters

FileName. JSON file path and name.

Description

The ReadFromFile method loads JSON structure from the specified file.

Example

Executing the example requires the JSON file: d:\Work\Json\testfile.json.

Add links to the ForeSystem (for the Fore.NET example) system assembly.

Sub UserProc;
Var
    Doc: IJsonDocument;
    JSON: IJsonObject;
Begin
    Doc := 
New JsonDocument.Create;
    Doc.ReadFromFile(
"d:\Work\Json\testfile.json");
    JSON := Doc.Root;
    ShowElements(JSON);
End Sub UserProc;

Sub ShowElements(JSON: IJsonObject);
Var
    i, c: Integer;
    Element: IJsonElement;
Begin
    c := JSON.Count;
    
For i := 0 To c - 1 Do
        Element := JSON.ItemByIndex(i);
        Debug.WriteLine(
"Name: " + JSON.Property_(i));
        Debug.Write(
"Type: " + Element.GetType.ToString + ". Value: ");
        ShowElement(Element);
    End For;
End Sub ShowElements;

Sub ShowElement(Element: IJsonElement);
Begin
    
Select Case Element.GetType
        
Case JsonElementType.Boolean: Debug.WriteLine(Element.AsBoolean);
        
Case JsonElementType.Double: Debug.WriteLine(Element.AsDouble);
        
Case JsonElementType.Integer: Debug.WriteLine(Element.AsInteger);
        
Case JsonElementType.NullValue: Debug.WriteLine("null value");
        
Case JsonElementType.String: Debug.WriteLine(Element.AsString);
        
Case JsonElementType.Array: ShowArray(Element.AsArray);
        
Case JsonElementType.Object: ShowObject(Element.AsObject);
    
End Select;
End Sub ShowElement;

Sub ShowArray(JSONArray: IJsonArray);
Var
    Element: IJsonElement;
    i, c: Integer;
Begin
    Debug.Indent;
    c := JSONArray.Count;
    Debug.WriteLine(
"Number of selected elements: " + c.ToString);
    
For i := 0 To c - 1 Do
        Element := JSONArray.Item(i);
        ShowElement(Element);
    
End For;
    Debug.Unindent;
End Sub ShowArray;

Sub ShowObject(Obj: IJsonObject);
Var
    Element: IJsonElement;
    i, c: Integer;
Begin
    Debug.Indent;
    c := Obj.Count;
    
For i := 0 To c - 1 Do
        Element := Obj.ItemByIndex(i);
        ShowElement(Element);
    
End For;
    Debug.Unindent;
End Sub ShowObject;

Imports Prognoz.Platform.Interop.ForeSystem;

Sub UserProc();
Var
    Doc: IJsonDocument = 
New JsonDocumentClass();
    JSON: IJsonObject;
Begin
    Doc.ReadFromFile(
"d:\Work\Json\testfile.json");
    JSON := Doc.Root;
    ShowElements(JSON);
End Sub;

Sub ShowElements(JSON: IJsonObject);
Var
    i, c: Integer;
    Element: IJsonElement;
Begin
    c := JSON.Count - 
1;
    
For i := 0 To c Do
        Element := JSON.ItemByIndex[i];
        System.Diagnostics.Debug.WriteLine(
"Name: " + JSON.@Property[i]);
        System.Diagnostics.Debug.Write(
"Type: " + Element.GetType.ToString() + ". Value: ");
        ShowElement(Element);
    End For;
End Sub;

Sub ShowElement(Element: IJsonElement);
Begin
    
Select Case Element.GetType
        
Case JsonElementType.jetBoolean: System.Diagnostics.Debug.WriteLine(Element.AsBoolean);
        
Case JsonElementType.jetDouble: System.Diagnostics.Debug.WriteLine(Element.AsDouble);
        
Case JsonElementType.jetInteger: System.Diagnostics.Debug.WriteLine(Element.AsInteger);
        
Case JsonElementType.jetNullValue: System.Diagnostics.Debug.WriteLine("null value");
        
Case JsonElementType.jetString: System.Diagnostics.Debug.WriteLine(Element.AsString);
        
Case JsonElementType.jetArray: ShowArray(Element.AsArray);
        
Case JsonElementType.jetObject: ShowObject(Element.AsObject);
    
End Select;
End Sub;

Sub ShowArray(JSONArray: IJsonArray);
Var
    Element: IJsonElement;
    i, c: Integer;
Begin
    System.Diagnostics.Debug.Indent();
    c := JSONArray.Count;
    System.Diagnostics.Debug.WriteLine(
"Number of selected elements: " + c.ToString());
    
For i := 0 To c - 1 Do
        Element := JSONArray.Item[i];
        ShowElement(Element);
    
End For;
    System.Diagnostics.Debug.Unindent();
End Sub;

Sub ShowObject(Obj: IJsonObject);
Var
    Element: IJsonElement;
    i, c: Integer;
Begin
    System.Diagnostics.Debug.Indent();
    c := Obj.Count;
    
For i := 0 To c - 1 Do
        Element := Obj.ItemByIndex[i];
        ShowElement(Element);
    
End For;
    System.Diagnostics.Debug.Unindent();
End Sub;

After executing the example, the JSON structure will be read from the file. Information about single structure elements will be displayed in the development environment console.

See also:

IJsonDocument