为什么我的 JsonConvert 没有正确转换
Why is my JsonConvert not converting correctly
我正在从 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());
数据示例return:
{
"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,,",
]
我要转换为的转换模型 I 示例:
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: 'Exception has been
thrown by the target of an invocation.'
内部异常:
ArgumentNullException: Value cannot be null. Parameter name: value
由于您已经完成了将结果转换为类似 CSV 格式的工作,所以我将继续沿着这条路线前进。请注意,我包括了 nuget 包 Newtonsoft.Json 和 FileHelpers:
using System;
using System.Text;
using System.Collections.Generic;
using Newtonsoft.Json;
using FileHelpers;
public class Program
{
public static void Main()
{
/*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);
}*/
var sb = new StringBuilder();
sb.Append(@"Name, subject, score, prevScore
Jhon,Math,24.54, 30
Jhon,English,,28.23
Jhon,Art,13.53,12
Joe,Math,27.01,
Joe,English,,");
// 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());
var engine = new FileHelperEngine<ConvertedModel>();
var convertedData = engine.ReadString(sb.ToString());
}
[DelimitedRecord(",")]
[IgnoreFirst(1)]
public class ConvertedModel {
public string Name { get; set; }
public string Subject { get; set; }
[FieldOptional]
public decimal? Score { get; set; }
[FieldOptional]
public decimal? PrevScore { get; set; }
}
}
我正在从 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());
数据示例return:
{
"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,,",
]
我要转换为的转换模型 I 示例:
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: 'Exception has been thrown by the target of an invocation.'
内部异常:
ArgumentNullException: Value cannot be null. Parameter name: value
由于您已经完成了将结果转换为类似 CSV 格式的工作,所以我将继续沿着这条路线前进。请注意,我包括了 nuget 包 Newtonsoft.Json 和 FileHelpers:
using System;
using System.Text;
using System.Collections.Generic;
using Newtonsoft.Json;
using FileHelpers;
public class Program
{
public static void Main()
{
/*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);
}*/
var sb = new StringBuilder();
sb.Append(@"Name, subject, score, prevScore
Jhon,Math,24.54, 30
Jhon,English,,28.23
Jhon,Art,13.53,12
Joe,Math,27.01,
Joe,English,,");
// 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());
var engine = new FileHelperEngine<ConvertedModel>();
var convertedData = engine.ReadString(sb.ToString());
}
[DelimitedRecord(",")]
[IgnoreFirst(1)]
public class ConvertedModel {
public string Name { get; set; }
public string Subject { get; set; }
[FieldOptional]
public decimal? Score { get; set; }
[FieldOptional]
public decimal? PrevScore { get; set; }
}
}