简体   繁体   English

Json 导入失败 - C#

[英]Json Import fails - C#

I have the following class structure我有以下 class 结构

public class AreaFields
{
    public List<Fields> Fields { set; get; }
}

public class Fields
{
    public string Name { set; get; }
    public string Value {set; get; }
}

When I export to Json using Jayrock.Json.Conversion.JsonConvert.ExportToString(List<AreaField> obj) , everything works fine.当我使用Jayrock.Json.Conversion.JsonConvert.ExportToString(List<AreaField> obj)导出到 Json 时,一切正常。 The problem is when I attempt to import it back to a list of AreaField, the native import fails.问题是当我尝试将其导入到 AreaField 列表时,本机导入失败。 What I am trying to is我想要的是

Jayrock.Json.Conversion.JsonConvert.Import(strJson)

Is there a way to maybe override the import method?有没有办法覆盖导入方法?

EDIT: Yes, jayrock knows the type of the object.编辑:是的,jayrock 知道 object 的类型。 My guess it has to do something with me serializing a list.我猜这与我序列化列表有关。

Export -
List<AreaField> list = GetAListOfAreaFields();
string sJson = Jayrock.Json.Conversion.JsonConvert.ExportToString(list)

Import -
List<AreaField> list = (AreaField)JsonConvert.Import(typeof(AreaField, sJson);

Exception - Cannot import AreaField from a JSON Array value.例外 - 无法从 JSON 数组值导入 AreaField。

It looks like you're exporting a List<AreaField> but attempting to import an AreaField (singular).看起来您正在导出List<AreaField>但尝试导入AreaField (单数)。 Try:尝试:

List<AreaField> list = (List<AreaField>)JsonConvert.Import(typeof(List<AreaField>, sJson);

List<> won't works, but array works. List<> 不起作用,但数组起作用。 Like this:像这样:

AreaField[] list = JsonConvert.Import<AreaField[]>(sJson);

"It doesn't work" is not a good start. “它不起作用”不是一个好的开始。 Exception?例外? Wrong data?数据错误? Does Jayrock know the type of the object? Jayrock 知道 object 的类型吗?

AreaFields af = (AreaFields)Jayrock.Json.Conversion.JsonConvert.Import(typeof(AreaFields), strJson);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM