将文件与 JSON 一起使用后关闭文件 [英] Close a file after using it with JSON

查看:77
本文介绍了将文件与 JSON 一起使用后关闭文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现该函数可以正确用于 JSON 解析:

I've found that function that correctly work for the JSON parse:

var objs = JObject.Load(new JsonTextReader(new StreamReader(JsonPath)))
                  .SelectTokens("*")
                  .Select(t => JsonConvert.DeserializeObject<Cars>(t.ToString()));

但它在使用后没有释放文件(所以我不能将它用于其他功能)......我尝试使用功能关闭,但似乎无法在对象上工作!

but it doesn't release the file after use it (so I can not use it for other function) ... I've tried to use the function Close, but seems not working over the object!

附言我检查了 previous open-one question,releated到这个话题,但没有人谈论如何访问对象文件,如果我们计算我有 1 个以上的对象,似乎问题会有所不同......猜猜有人可以更好地解释它是如何工作的......

P.s. i've checked the previous open-one question, releated to this topic, but nobody talk about how to access to the object fileds, and seems the qeustion are different if we count that i have more than 1 object ... guess someone can explain better how it works ...

推荐答案

我建议您异步读取 json 数据,因为此方法具有将流作为输入的重载,因此您可以像这样读取两串代码中的数据:

I recommend you to read your json data async because this method have overload that takes stream as an input, so you can read data in two strings of code like this:

ProjFolder\Program.cs

using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
using StackOverflow.Models;

namespace StackOverflow
{
    class Program
    {
        static async Task Main(string[] args)
        {
            using var openStream = File.OpenRead(@"D:\Code\test.json");

            var result = await JsonSerializer.DeserializeAsync<Root>(openStream);

            // You need to get car2 Costo
            var costo = result.Car2.Costo;
        }
    }    
}

ProjFolder\Models\Root.cs

using System.Text.Json.Serialization;

namespace StackOverflow.Models
{
    public class Root
    {
        [JsonPropertyName("car1")]
        public Car Car1 { get; set; }

        [JsonPropertyName("car2")]
        public Car Car2 { get; set; }
    }
}

ProjFolder\Models\Car.cs

namespace StackOverflow.Models
{
    public class Car
    {
        public string CasaAutomobilistica { get; set; }
        public string Modello { get; set; }
        public string AnnoImmatricolazione { get; set; }
        public string Targa { get; set; }
        public int KM { get; set; }
        public bool Finanziamento { get; set; }
        public int Porte { get; set; }
        public int Costo { get; set; }
    }
}

{
    "car1": 
    {
        "CasaAutomobilistica": "Fiat",
        "Modello": "500",
        "AnnoImmatricolazione": "2017",
        "Targa": "AZ978AG",
        "KM": 120000,
        "Finanziamento" : true,
        "Porte" : 5,
        "Costo" : 6000
    },
    "car2":
    {
        "CasaAutomobilistica": "BMW",
        "Modello": "Serie 1",
        "AnnoImmatricolazione": "2019",
        "Targa": "BC978AG",
        "KM": 150000,
        "Finanziamento" : false,
        "Porte" : 3,
        "Costo" : 12000
    }
}

这篇关于将文件与 JSON 一起使用后关闭文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆