[英]C# JSON Serialize/Deserialize - INPUT problem
I have problem with serialization and deserialization in JSON I've made 2 tasks to read from JSON file which looks like this:我在 JSON 中遇到序列化和反序列化问题我做了 2 个任务来读取 JSON 文件,如下所示:
[
{
"ID": 1,
"OIB": 123456789,
"ime": "name",
"prezime": "surname",
"grad": "city"
}
]
Now I have to add another client with ID 2, with new user informations.现在我必须添加另一个 ID 为 2 的客户端,以及新的用户信息。 I can read this JSON file with no problems, but I am stuck on writing into the same file.我可以毫无问题地读取这个 JSON 文件,但我一直坚持写入同一个文件。
public struct Klijent
{
public int ID { get; set; }
public long OIB { get; set; }
public string ime { get; set; }
public string prezime { get; set; }
public string grad { get; set; }
}
"FetchClient" from JSON来自 JSON 的“FetchClient”
public static List<Klijent> DohvatiKlijente()
{
List<Klijent> lKlijent = new List<Klijent>();
StreamReader klijent = new StreamReader("x");
string sJson = "";
using (klijent)
{
sJson = klijent.ReadToEnd();
lKlijent = JsonConvert.DeserializeObject<List<Klijent>>(sJson);
}
return lKlijent;
}
"AddClient" to JSON “AddClient”到 JSON
OIB -> personal identificator
ID -> should go +1 with every entry of client
grad -> city
ime -> name
prezime -> surname
public static void DodavanjeKlijenata()
{
Console.Write("Unesite OIB klijenta: ");
string pOIB = Console.ReadLine();
long nullOIB = 0;
long.TryParse(pOIB, out nullOIB);
int id = 0;
Console.Write("Unesite ime klijenta: ");
string ime1 = Console.ReadLine();
Console.Write("Unesite prezime klijenta: ");
string prezime1 = Console.ReadLine();
Console.Write("Unesite grad klijenta: ");
string grad1 = Console.ReadLine();
List<Klijent> lKlijent = DohvatiKlijente();
foreach (var Klijent in lKlijent)
{
id = Klijent.ID + 1;
}
Klijent dKlijent = new Klijent()
{
ID = id,
OIB = nullOIB,
ime = ime1,
prezime = prezime1,
grad = grad1
};
var serializer = new JsonSerializer();
using (var sw = new StreamWriter("x"))
using (JsonWriter writer = new JsonTextWriter(sw))
{
serializer.Serialize(writer, dKlijent);
}
}
This code does work, but it seems to delete every time my JSON file and it's format is in one line only, I would like to have it in multiple lines.这段代码确实有效,但似乎每次我的 JSON 文件都会被删除,而且它的格式只有一行,我想把它分成多行。
Thank you :)谢谢 :)
There are two things that you need to do here这里有两件事你需要做
Ensure new Client is appended to existing list确保将新客户附加到现有列表中
For this you can add the new client to the List为此,您可以将新客户端添加到列表中
lKlijent.Add(dKlijent);
Now you need to serialize the List, instead of lKlijent
现在你需要序列化 List,而不是lKlijent
using (JsonWriter writer = new JsonTextWriter(sw))
{
serializer.Serialize(writer, lKlijent);
}
Formatting格式化
For formatting you can use Formatting Settings.对于格式化,您可以使用格式化设置。 For example,例如,
var serializer = new JsonSerializer() { Formatting = Formatting.Indented} ;
Additional Comments附加评论
1. Calculation of ID 1. ID的计算
Instead of calculating the new ID using the following loop,不是使用以下循环计算新 ID,
foreach (var Klijent in lKlijent)
{
id = Klijent.ID + 1;
}
You could use Enumerable.Last()
to get the last client in the list.您可以使用Enumerable.Last()
获取列表中的最后一个客户端。 For example,例如,
var id = lKlijent?.Any()!=true? 0:lKlijent.Last().ID;
2. Rewriting DohvatiKlijente method 2. 重写 DohvatiKlijente 方法
The DohvatiKlijente
method could rewritten as DohvatiKlijente
方法可以重写为
public static List<Klijent> DohvatiKlijente()
{
return JsonConvert.DeserializeObject<List<Klijent>>(File.ReadAllText("C:\\Users\\Hrvoje\\Desktop\\Polica Osiguranja MAIN\\Polica Osiguranja\\klijent.json"));
}
Similarly, writing back to file can be simplified as同样,写回文件可以简化为
var jsonString = JsonConvert.SerializeObject(lKlijent,Newtonsoft.Json.Formatting.Indented);
File.WriteAllText(outputFilePath,jsonString);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.