함수가 호출 될 때마다 더 많은 항목 개체가 추가되는 파일에 개체 (항목)을 직렬화하려고합니다.
이 오류가 계속 발생합니다.
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])로 변경하거나 deserialized 유형을 일반 .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);
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다