问题
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!
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 ...
回答1:
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
}
}
回答2:
From the JSON.NET Site Read JSON from a File :
// read JSON directly from a file
using (StreamReader file = File.OpenText(@"c:\videogames.json"))
using (JsonTextReader reader = new JsonTextReader(file))
{
JObject o2 = (JObject)JToken.ReadFrom(reader);
}
The question's code does something unnecessary though - it tries to serialize the already deserialized JObject back into JSON, then deserialize it again as a Car
. Why not deserialize the original file just once?
The docs describe this too in Deserialize JSON from a file:
// deserialize JSON directly from a file
using (StreamReader file = File.OpenText(@"c:\movie.json"))
{
JsonSerializer serializer = new JsonSerializer();
Movie movie2 = (Movie)serializer.Deserialize(file, typeof(Movie));
}
JSON documents can contain only a single object. If the file contents look like this :
{
"Movies":[{
"Title":"abc"
}, {
"Title":"abc"
}]
}
The root object should contain a Movies
property that holds a Movie[]
array :
class RootObjedt
{
public Movie[] Movies{get;set;}
}
...
using (StreamReader file = File.OpenText(@"c:\movie.json"))
{
JsonSerializer serializer = new JsonSerializer();
var root = (Movie)serializer.Deserialize(file, typeof(RootObject));
var movies=root.Movies;
}
回答3:
First, read the file with surrounded by using block and do JSON operation Better place it in a separate method as below
private string readFile()
{
string buffer = null;
using (System.IO.FileStream stm = new
System.IO.FileStream("c:\\YourFile.txt",System.IO.FileMode.Open,
System.IO.FileAccess.Read, System.IO.FileShare.None))
{
using (System.IO.StreamReader rdr = new System.IO.StreamReader (stm))
{
buffer=rdr.ReadToEnd();
}
}
return buffer
}
private Object Deserialize(string content)
{
//do conversion here
}
来源:https://stackoverflow.com/questions/65706382/close-a-file-after-using-it-with-json