[英]Deserializing and Serializing JSON File in C#
我正在嘗試將一個object(Item)序列化為一個文件,該文件每次調用該函數時都會附加更多的Item對象。
它一直給我這個錯誤:
07-12 15:43:27.931 I / MonoDroid(17468):Newtonsoft.Json.JsonSerializationException:無法將當前JSON對象(例如{“ name”:“ value”})反序列化為類型'System.Collections.Generic.List' 1 [WaterApp.Item]',因為該類型需要JSON數組(例如[1,2,3])才能正確反序列化。 07-12 15:43:27.931 I / MonoDroid(17468):要解決此錯誤,請將JSON更改為JSON數組(例如[1,2,3])或更改反序列化類型,使其成為普通的.NET可以從JSON對象反序列化的類型(例如,不是整數的原始類型,不是數組或列表的集合類型)。 還可以將JsonObjectAttribute添加到類型中,以強制其從JSON對象反序列化。
這是對象類:
using System;
using System.Collections;
using Android.Content;
using Android.Preferences;
using Java.IO;
namespace WaterApp
{
public class Item
{
public bool Type { get; set; }
public string ItemName { get; set; }
public DateTime ExpiryDate { get; set; }
public int ItemIconNumber { get; set; }
public Item(bool type, string itemName, DateTime expiryDate, int itemIconNumber)
{
Type = type;
ItemName = itemName;
ExpiryDate = expiryDate;
ItemIconNumber = itemIconNumber;
}
}
}
這是使用方法完成數據處理的類:
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using Console = System.Console;
using Environment = System.Environment;
using File = System.IO.File;
using JsonWriter = Newtonsoft.Json.JsonWriter;
namespace WaterApp.Droid
{
public abstract class DataHandler
{
private static string documentsPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal);
private static string filePath = Path.Combine(documentsPath, "HatchListData.json");
public static void SaveObjectToFile(Item item)
{
// read in the existing list
// IMPORTANT - if this is the first time and the file doesn't exist, then
// you need to initialise itemList = new List<Item>();
var itemList = new List<Item>();
File.WriteAllText(filePath, String.Empty);
string json = File.ReadAllText(filePath);
itemList = JsonConvert.DeserializeObject<List<Item>>(json);
// add a new item
itemList.Add(item);
// serialize the list and write it out again
json = JsonConvert.SerializeObject(itemList, Formatting.Indented);
File.WriteAllText(filePath, json);
Console.WriteLine(File.ReadAllText(filePath));
}
public static void LoadList(List<Item> listToBeLoaded)
{
string json = "";
if (File.Exists(filePath))
{
if (filePath.Length > 2)
{
Console.WriteLine("READING" + "READ!");
using (StreamReader streamReader = new StreamReader(filePath))
{
string json = streamReader.ReadToEnd();
listToBeLoaded = JsonConvert.DeserializeObject<List<Item>>(json);
}
Console.WriteLine("READING" + "loaded");
}
}
else
{
Console.WriteLine("READING" + "Doesn't exist, json file.");
}
}
}
}
請有人可以將我推向正確的方向或以任何方式幫助解決此錯誤嗎? 我不太確定如何解決錯誤問題。 我不明白該錯誤要求什么代碼。
您不能僅將兩個序列化的json對象附加在一起-不會產生有效的json。 相反,您需要讀入現有列表,向其中添加項目,然后再次將其序列化
// read in the existing list
// IMPORTANT - if this is the first time and the file doesn't exist, then
// you need to initialise itemList = new List<Item>();
json = File.ReadAllText(filePath);
var itemList = JsonConvert.DeserializeObject<List<Item>>(json);
// add a new item
itemList.Add(newItem);
// serialize the list and write it out again
string json = JsonConvert.SerializeObject(itemList, Formatting.Indented);
File.WriteAllText(path,json);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.