简体   繁体   English

使用虚拟 object 反序列化 C# EF Core 中的 JSON 作为字符串列表

[英]Deserialize JSON in C# EF Core with dummy object for list of string

I am currently attempting to build an ASP.NET Core API.我目前正在尝试构建一个 ASP.NET Core API。

On startup it checks if there is anything in its EF Core powered DB.在启动时,它会检查其 EF Core 驱动的数据库中是否有任何内容。 If not it reads a few JSON files and fills it up.如果不是,它会读取一些 JSON 文件并将其填满。 To do that it needs to Deserialize them.为此,它需要反序列化它们。 One of the properties is a List of strings and EF Core doesn't like that.其中一个属性是字符串列表,而 EF Core 不喜欢这样。 To get around it I made a dummy Model.为了解决这个问题,我做了一个虚拟的 Model。

public class StringModel
{
    public int id { get; set; }
    public string str { get; set; }
}

And the other Model.和另一个 Model。

public class Book
{
    public int id { get; set; }
    public virtual List<StringModel> chapters { get; set; }
}

This is not how my models look.这不是我的模型的样子。 This is a minimum reproducible example.这是一个最小的可重现示例。

JSON JSON

[{
  chapters: ["a","b"]
}, {
  chapters: ["foo","bar"]
}]

And this is where it throws这就是它抛出的地方

string json = GetJSONFromFile();
var objs = JsonConvert.DeserializeObject<List<Book>>(json);

Newtonsoft.Json.JsonSerializationException: 'Error converting value "a" to type 'Project.Models.Primitives.StringModel'. Newtonsoft.Json.JsonSerializationException:“将值“a”转换为类型“Project.Models.Primitives.StringModel”时出错。 Path '[0].chapters[0]'路径 '[0].chapters[0]'

Any ideas?有任何想法吗?

for your json you need this class对于您的 json,您需要这个 class

     public class Book
    {
        public int id { get; set; }
        public List<string> chapters { get; set; }
    }

and after this you can use之后你可以使用

var objs = JsonConvert.DeserializeObject<List<Book>>(json);

if you want to use StringModel instead of string, your json should be如果你想使用 StringModel 而不是字符串,你的 json 应该是

[
{"id":5,"chapters":[{"id":1,"str":"a"},{"id":2,"str":"b"}]},
{"id":6,"chapters":[{"id":3,"str":"bar"},{"id":4,"str":"foo"}]}
]

反序列化 json 到 List<object> 在 C#<div id="text_translate"><p> 我有以下 JSON 字符串</p><pre> { "data": [ { "symbol": "1COV.GE", "exposure": "0", "makerExposure": "-2028", "takerExposure": "2028", "makerPnl": "447.6688", "takerPnl": "-447.6688", "makerPositions": [ { "name": "IB_001", "position": "-2028", "vwap": "47.41", "pnl": "447.6688" } ], "takerPositions": [ { "name": "MT5_1", "position": "2028", "vwap": "47.41", "pnl": "-447.6688" } ] }, { "symbol": "A", "exposure": "0", "makerExposure": "-10", "takerExposure": "10", "makerPnl": "-4.6", "takerPnl": "4.6", "makerPositions": [ { "name": "IB_002", "position": "-10", "vwap": "136.78", "pnl": "-4.6" } ], "takerPositions": [ { "name": "MT5_1", "position": "10", "vwap": "136.78", "pnl": "4.6" } ], "total": 2 } }</pre><p> 我的目标是将它从节点“数据”序列化为 object 的列表:我有 map 数据节点字段的类:</p><pre> public class Positions { public string name { get; set; } public string position { get; set; } public string vwap { get; set; } public string pnl { get; set; } } public class ExPositions { public string symbol { get; set; } public string exposure { get; set; } public string makerExposure { get; set; } public string takerExposure { get; set; } public string makerPnl { get; set; } public string takerPnl { get; set; } public OZPositions makerPositions { get; set; } public OZPositions takerPositions { get; set; } }</pre><p> 您是否知道如何将节点“数据”转换为“Expositions”对象列表,例如。 列表</p><p>我已经这样做了,但到目前为止它会引发错误</p><pre>var positions = JsonSerializer.Deserialize<ExPositions>(json_string);</pre></div></object> - Deserialize json to List<object> in C#

暂无
暂无

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

相关问题 将列表的 JSON 字符串反序列化为 c# object - Deserialize JSON string for list to c# object 如何反序列化JSON字符串不列出 <object> 在C#中 - How to deserialize json string NOT to List<object> in c# 如何在 C# dot 中将 json 字符串反序列化为对象列表 - How to deserialize json string to object list in c# dot 在C#中用牛顿反序列化json对象列表 - Deserialize json object list with newton in c# C#将JSON反序列化为对象列表 - c# deserialize json to object list C#反序列化对象错误的JSON列表 - c# Deserialize JSON list of object error 反序列化 json 到 List<object> 在 C#<div id="text_translate"><p> 我有以下 JSON 字符串</p><pre> { "data": [ { "symbol": "1COV.GE", "exposure": "0", "makerExposure": "-2028", "takerExposure": "2028", "makerPnl": "447.6688", "takerPnl": "-447.6688", "makerPositions": [ { "name": "IB_001", "position": "-2028", "vwap": "47.41", "pnl": "447.6688" } ], "takerPositions": [ { "name": "MT5_1", "position": "2028", "vwap": "47.41", "pnl": "-447.6688" } ] }, { "symbol": "A", "exposure": "0", "makerExposure": "-10", "takerExposure": "10", "makerPnl": "-4.6", "takerPnl": "4.6", "makerPositions": [ { "name": "IB_002", "position": "-10", "vwap": "136.78", "pnl": "-4.6" } ], "takerPositions": [ { "name": "MT5_1", "position": "10", "vwap": "136.78", "pnl": "4.6" } ], "total": 2 } }</pre><p> 我的目标是将它从节点“数据”序列化为 object 的列表:我有 map 数据节点字段的类:</p><pre> public class Positions { public string name { get; set; } public string position { get; set; } public string vwap { get; set; } public string pnl { get; set; } } public class ExPositions { public string symbol { get; set; } public string exposure { get; set; } public string makerExposure { get; set; } public string takerExposure { get; set; } public string makerPnl { get; set; } public string takerPnl { get; set; } public OZPositions makerPositions { get; set; } public OZPositions takerPositions { get; set; } }</pre><p> 您是否知道如何将节点“数据”转换为“Expositions”对象列表,例如。 列表</p><p>我已经这样做了,但到目前为止它会引发错误</p><pre>var positions = JsonSerializer.Deserialize<ExPositions>(json_string);</pre></div></object> - Deserialize json to List<object> in C# 将json字符串反序列化为C#中的对象变量 - Deserialize a json string to an object variable in C# 如何将JSON字符串反序列化为C#对象? - How to deserialize JSON string into a C# object? 将JSON字符串反序列化为c#对象 - Deserialize JSON string to c# object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM