【发布时间】:2021-05-24 03:18:13
【问题描述】:
我正在尝试将我的领域对象序列化为 json 字符串,但我收到 Newtonsoft.Json.JsonSerializationException: 'Error getting value from 'Id' on 'Goodbuy.Models.product '。'这是序列化发生的方法:
async void OnItemSelected(product item)
{
if (item == null)
return;
var model = await ProductService.GetProduct(item.Id);
//Convert Object to string
string jsonStrObj = await Task.Run(() => JsonConvert.SerializeObject(model));
// This will push the ItemDetailPage onto the navigation stack
await Shell.Current.GoToAsync($"{nameof(ItemDetailPage)}?ProductModel={jsonStrObj}");
}
下面是我的领域对象类:
public class product :RealmObject
{
[PrimaryKey]
[MapTo("_id")]
public ObjectId? Id { get; set; }
[MapTo("brand")]
public string Brand { get; set; }
[MapTo("image_url")]
public string ImageUrl { get; set; }
[MapTo("name")]
public string Name { get; set; }
}
【问题讨论】:
-
id的值是多少?这是整数值还是字符串?
-
值是这样的,"_id":{"$oid":"600a87ccd2a5c05244d93f76"}
-
请改成字符串再试试
-
我无法将其更改为字符串,因为我正在使用 mondoDb 领域同步,所以它不会再同步我的数据了
标签: c# json xamarin.forms realm