Read json file inline in Blazor (MAUI)

Anonymous
2023-11-04T09:25:35.9566667+00:00

The below code was given in this forum and it works fine in Blazor but it doesn't work in MAUI.

Can someone help me refactor code to read the JSON inline and not from

"sample-data/employees.txt"

Like assign it to some variable and load the list.

ex var x= "

"burned": {
    "ACTIVITY": [
      {
        "CD": "1003",
        "METS": "14",
        "CATEGORY": "bicycling",
        "DESC": "bicycling, mountain, uphill, vigorous"
      },
      {
        "CD": "1004",
        "METS": "16",
        "CATEGORY": "bicycling",
        "DESC": "bicycling, mountain, competitive, racing"
      }

"

employees.txt

As in MAUI I am getting error and cannot read file.

@page "/FetchData"
@inject HttpClient Http
@using Microsoft.AspNetCore.Components.Forms
@using System.Net.Http.Json

<PageTitle>Activity Dropdown</PageTitle>

<h3>Activity</h3>

<EditForm Model="@submitActivity" OnSubmit="@Submit">
    <div>
        <InputSelect @bind-Value="submitActivity.CD">
            @if (submitActivity != null)
            {
                <option value="">--Select--</option>
                @foreach (var activity in ActivityList)
                {
                    <option value="@activity.CD">@activity.DESC</option>
                }
            }
        </InputSelect>
    </div>
    <br />
    <div class="row">
        <div class="col-md-3">
            <p>Weight (in kgs)</p>
        </div>
        <div class="col-md-4">
            <input placeholder="Weight (in kgs)" @bind="@weight" />
        </div>
    </div>

    <br />
    <div class="row">
        <div class="col-md-3">
            <p>Time  (in minutes)</p>
        </div>
        <div class="col-md-4">
            <input placeholder="Time in minutes" @bind="@minutes" />
        </div>
    </div>
    <div>
        <button type="submit">Submit</button>
    </div>
</EditForm>

<div>
    <div>@finalresult</div>
</div>

@code {
    private List<ACTIVITY> ActivityList = new();
    private ACTIVITY submitActivity { get; set; } = new();

    private int weight;
    private int minutes;
    private string finalresult = string.Empty;
    private decimal hours;
    private string skip = "";
    protected override async Task OnInitializedAsync()
    {
        //Get the data from the employees.txt
        var root = await Http.GetFromJsonAsync<Rootobject>("sample-data/employees.txt");
        ActivityList = root.burned.ACTIVITY;
    }





    private void Submit()
    {
        //Debug - View the CD

        skip = "N";
        if (weight <= 21)
        {
            skip = "Y";
            finalresult = "Invalid Weight";
        }
        if (weight >= 150)
        {
            skip = "Y";

            finalresult = "Invalid Weight";
        }
        if (minutes <= 5)
        {
            skip = "Y";

            finalresult = "Invalid Time";
        }

        if (minutes >= 200)
        {
            skip = "Y";

            finalresult = "Invalid Time";
        }
        //use the CD to get the and display the description
        ACTIVITY model = ActivityList.FirstOrDefault(a => a.CD == submitActivity.CD);

        hours = (decimal)minutes / 60;
        if (skip == "N")
        { finalresult = (weight * model.METS * hours).ToString(); }


    }


    //View Model
    public class Rootobject
    {
        public Burned burned { get; set; }
    }

    public class Burned
    {
        public List<ACTIVITY> ACTIVITY { get; set; }
    }

    public class ACTIVITY
    {
        public string CD { get; set; }
        public decimal METS { get; set; }
        public string CATEGORY { get; set; }
        public string DESC { get; set; }
    }
}
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,551 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,381 questions
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 63,746 Reputation points
    2023-11-04T16:40:31.65+00:00

    When Blazor is hosted by a webserver the following:

    var root = await Http.GetFromJsonAsync<Rootobject>("sample-data/employees.txt");
    

    does a fetch from the hosting webserver.

    But when a mobile app, there is no default webserver, so you need to supply the full url of the webserver.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.