使用Newtonsoft将数据从List写入JSON

rryofs0p  于 2023-03-24  发布在  其他
关注(0)|答案(1)|浏览(94)

目前,我正尝试将列表中的数据写入JSOn文件。我已经阅读了一篇与此问题非常相似的帖子,只是出于某种原因,它似乎对我不起作用。
这是我的模型:

namespace ERP.Classes
{
    public class User_Role
    {
        public string RoleId { get; init; }
        public string RoleName { get; set; }
        public string RoleDescription { get; set; }
        public List<User> Users { get; set; }

        public User_Role(string roleName, string roleDescription)
        {
            RoleId = Guid.NewGuid().ToString("N");
            RoleName = roleName;
            RoleDescription = roleDescription;
            Users = new List<User>();
        }
    }
}

这是我的剃刀页面,我想在这里创建这个列表并保存JSON文件:

@page "/user_roles"
@using ERP.Classes
@using Microsoft.AspNetCore.Components.Forms

<PageTitle>User Roles</PageTitle>

<h1>User Roles</h1>

<EditForm Model="@_userRoles" OnValidSubmit="@createRoleName"> 
    <InputText id="roleName" @bind-Value="_roleName"/>
    <InputText id="roleDescription" @bind-Value="_roleDescription"/>
    <button type="submit">Create user role</button>
</EditForm>
<p>@_confirmation</p>
<table>
    <tr>
        <th><h5>Role Id</h5></th>
        <th><h5>Role Name</h5></th>
        <th><h5>Role Description</h5></th>
    </tr>
    @foreach(User_Role list in _userRoles)
    {
        <tr>
            <td>@list.RoleId</td>
            <td>@list.RoleName</td>
            <td>@list.RoleDescription</td>
        </tr>
    }
</table>
// Button to call the method
<button onclick="@saveData">Save</button>

@code {
    private string _confirmation = "";
    private string _roleName = "";
    private string _roleDescription = "";
    private List<User_Role> _userRoles = new List<User_Role>();

    private void createRoleName()
    {
        if(roleExists(_userRoles,_roleName))
        {
            _confirmation = "Role already exists";
        }
        else
        {
            User_Role _userRole = new User_Role(_roleName,_roleDescription);
            _userRoles.Add(_userRole);
            _confirmation = "Role created";
        }        
    }

    private List<User_Role> loadRoles()
    {
        return _userRoles;
    }
    // Calling the SaveData class to send over the data
    private void saveData()
    {                
        SaveData<User_Role> _data = new SaveData<User_Role>();
        _data.SavetoJson("userRoles.txt",_userRoles);
    }

    private bool roleExists(List<User_Role> user_Roles, string currentRoleName)
    {
        bool hasRole = false;
        foreach(var item in user_Roles)
        {
            if(item==null)
            {
                hasRole = false;
            }          
            else if(item.RoleName == currentRoleName)
            {
                hasRole = true;
            }
        }
        return hasRole;
    }
}

这是具有保存功能的类:

using System.Collections.Generic;
using Newtonsoft.Json;

namespace ERP.Classes
{
    public class SaveData <T>
    {
        public void SavetoJson(string filename, List<T> dataList)
        {
            using (StreamWriter file = File.CreateText(@"C:\Data\" + filename))
            {
                JsonSerializer serializer = new JsonSerializer();
                serializer.Serialize(file, dataList);
            }
        }
    }
}

我的SaveData类之所以是通用的,是为了我可以将其用于我所有的其他模型。为了防止重复编码。但由于某种原因,它不会创建JSON文件。我已经尝试了“.txt”和“.json”。我对编程很陌生,所以如果这个问题已经得到了回答,请原谅我。但我无法找到它。

piv4azn7

piv4azn71#

试试这个

public void SavetoJson(string filename, List<T> dataList)
 {
   var json = JsonConvert.SerializeObject(dataList);
  
  File.WriteAllText(fileName, json);
}

相关问题