//if you want to use a list and add it to a file in json do this
using System.IO;
using System.Text.Json;
List<Data> data = new List<Data>();
public void SaveData()
{
using (StreamWriter sw = File.CreateText("data.json"))
{
sw.Write(JsonSerializer.Serialize(data));
}
}
using System.Text.Json;
using System.Text.Json.Serialization;
List<data> _data = new List<data>();
_data.Add(new data()
{
Id = 1,
SSN = 2,
Message = "A Message"
});
using FileStream fileStream = File.Create(@"D:path.json");
await JsonSerializer.SerializeAsync(createStream, _data);
using System.Text.Json;
using System.Text.Json.Serialization;
List<data> _data = new List<data>();
_data.Add(new data()
{
Id = 1,
SSN = 2,
Message = "A Message"
});
string json = JsonSerializer.Serialize(_data);
File.WriteAllText(@"D:path.json", json);
# write json file
import json
d = {
"firstName": "Tom",
"lastName": "Jack",
"gender": "male",
"age": 35,
"address": {
"streetAddress": "126",
"city": "San Jone",
"state": "CA",
"postalCode": "95150"
},
"phoneNumbers": [
{ "type": "home", "number": "4083627627" }
]
}
filename = "/content/sample_data/sample1.json"
with open(filename, 'w') as f:
json.dump(d, f)
//open file stream
using (StreamWriter file = File.CreateText(@"D:path.txt"))
{
JsonSerializer serializer = new JsonSerializer();
//serialize object directly into file stream
serializer.Serialize(file, _data);
}
File.Create(@"details.json");