Xamarin.forms 使用JSON将值传递给intent

Xamarin.forms 使用JSON将值传递给intent,xamarin.forms,Xamarin.forms,我正在尝试使用Newtonsoft.Json将值从共享项目传递到droid项目。因此,在我的共享项目中,我将对象序列化如下,并获得一定的结果,如下所示。然后,我将该结果带到droid项目,并尝试反序列化该值,但我得到的异常也显示在下面 我的项目 public void PushDictionary(List<Word> allWordsOfUserForAutomat) { var second = new Intent(MainActivi

我正在尝试使用Newtonsoft.Json将值从共享项目传递到droid项目。因此,在我的共享项目中,我将对象序列化如下,并获得一定的结果,如下所示。然后,我将该结果带到droid项目,并尝试反序列化该值,但我得到的异常也显示在下面

我的项目

 public void PushDictionary(List<Word> allWordsOfUserForAutomat)
        {
            var second = new Intent(MainActivity.Instance, typeof(LockScreenDictionary));
           
            second.PutExtra("MyData", JsonConvert.SerializeObject(allWordsOfUserForAutomat));
            var result = JsonConvert.SerializeObject(allWordsOfUserForAutomat);

            Console.WriteLine(result);
            MainActivity.Instance.StartActivity(second);
        }
然而,我得到了这个例外,有人能告诉我吗

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'AT.Model.Word' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Newtonsoft.Json.JsonSerializationException:'无法将当前Json数组(例如[1,2,3])反序列化为类型'AT.Model.Word',因为该类型需要一个Json对象(例如{“name”:“value”})才能正确反序列化。
要修复此错误,请将JSON更改为JSON对象(例如{“name”:“value”}),或将反序列化类型更改为数组或实现可从JSON数组反序列化的集合接口(例如ICollection、IList)类似列表的类型。还可以将JsonArrayAttribute添加到类型中,以强制它从JSON数组反序列化。

您正在序列化单词列表

var result = JsonConvert.SerializeObject(allWordsOfUserForAutomat);
然后您尝试反序列化单个单词

var allWordsOfUserForAutomat = JsonConvert.DeserializeObject<Word>(Intent.GetStringExtra("MyData"));
var allWordsOfUserForAutomat=JsonConvert.DeserializeObject(Intent.GetStringExtra(“MyData”);
相反,反序列化列表

var allWordsOfUserForAutomat = JsonConvert.DeserializeObject<List<Word>>(Intent.GetStringExtra("MyData"));
var allWordsOfUserForAutomat=JsonConvert.DeserializeObject(Intent.GetStringExtra(“MyData”);
请注意,异常会确切地告诉您如何解决此问题

要修复此错误,请将JSON更改为JSON对象(例如。 {“name”:“value”})或将反序列化类型更改为数组或 实现集合接口的类型(例如ICollection、IList) 类似于可以从JSON数组反序列化的列表。

var allWordsOfUserForAutomat = JsonConvert.DeserializeObject<List<Word>>(Intent.GetStringExtra("MyData"));