【发布时间】:2021-03-13 23:17:53
【问题描述】:
我正在从 API 获取数据并希望将其转换为对象列表
// Get the data
var searchResult = GetResults();
string[] data = (string[]) searchResult.Data;
string headers = searchResult.Headers;
// Combine the data into CSVish format
var sb = new StringBuilder();
sb.AppendLine(headers);
foreach(var recordString in data){
sb.AppendLine(recordString);
}
// Convert to a new a JsonFormat
/* This is where the issue is. I get the an exeption */
var convertedData = JsonConvert.DeserializeObject<List<ConvertedModel>>(sb.ToString());
数据返回示例:
{
"Headers": "Name, subject, score, prevScore"
"Data":[
"Jhon,Math,24.54, 30",
"Jhon,English,,28.23",
"Jhon,Art,13.53,12",
"Joe,Math,27.01,",
"Joe,English,,",
]
转换后的模型我要转换的示例:
public class ConvertedModel {
public string Name { get; set; }
public string Subject { get; set; }
public decimal Score { get; set; }
public decimal PrevScore { get; set; }
}
异常消息:
System.Reflection.TargetInvocationException: '异常已被 被调用的目标抛出。'
内在的例外:
ArgumentNullException:值不能为空。参数名称:值
【问题讨论】:
-
见minimal reproducible example。简而言之,我粘贴了您的代码,但它无法编译
-
“这就是问题所在” - 你忘了告诉我们
jsonString的定义和使用方式。也许jsonString是null? -
我猜他错过了
jsonString = sb.ToString(); -
在示例中我确实错过了 'jsonString = sb.ToString();'然而,原来的问题仍然存在。根据@TheGeneral 的要求对其进行了更新并使其更加完整
标签: c# json jsonconvert