I'm new to C# and I'm wondering how would one go about casting a stringbuilder to a JSON structure? I'm using SQL's FOR JSON feature and it returns JSON in multiple rows. I add the rows together to get a string and cast it to JSON this way. I'm getting this error when I compile however this was the closest solution I found. It seems like C# is pretty strict with data types
Error Argument 1: cannot convert from 'System.Text.StringBuilder' to 'System.ReadOnlySpan' WebApiSwagger
public class AlgReadingChannel
{
/// <example>1</example>
[Required]
public int chanNo { get; set; }
}
public class AlgReadingInner
{
/// <example>2020-03-12T16:41:43.017</example>
public string Created { get; set; }
/// <example>2020-03-12T16:41:43.017</example>
public string Modified { get; set; }
/// <example>24867</example>
public int ReadingID { get; set; }
/// <example>2323</example>
public string Name { get; set; }
/// <example>Starting</example>
public string Reading { get; set; }
/// <example></example>
public string State { get; set; }
/// <example>X</example>
public string Site { get; set; }
/// <example>Main</example>
[Required]
public IList<AlgReadingChannel> v { get; set; }
}
/// <summary>
/// Represents AlgReading parent class
/// </summary>
public class AlgReadingOuter
{
/// <example>5137</example>
[Required]
public int AnalogID { get; set; }
/// <example>E360EC39-7B0F-42A1-B125-2298SHDE53</example>
[Required]
public string SiteID { get; set; }
/// <example>2020-03-12T09:41:43</example>
[Required]
public string EntryDateTime { get; set; }
[Required]
public IList<AlgReadingInner> t { get; set; }
}
var stringBuilder = new StringBuilder();
var docs = new List<AlgReadingOuter>();
stringBuilder.Append([{
"AnalogID": 5137,
"SiteID": "E360EC39-7B0F-42A1-B125-22C1F6C5DE53",
"EntryDateTime": "2020-03-12T09:41:43",
"t": [{
"Created": "2020-03-12T16:41:43.017",
"Modified": "2020-03-12T16:41:43.017",
"ReadingID": 24867,
"Name": "ABC",
"Reading": "Starting",
"State": "X",
"Site": "Main",
"v": [{
"chanNo": 1
}]
},
{
"Created": "2020-03-12T16:41:43.017",
"Modified": "2020-03-12T16:41:43.017",
"ReadingID": 24868,
"Name": "bar",
"Reading": "Starting",
"State": "X",
"Site": "Main",
"v": [{
"chanNo": 2
}]
},
{
"Created": "2020-03-12T16:41:43.017",
"Modified": "2020-03-12T16:41:43.017",
"ReadingID": 24869,
"Name": "foo",
"Reading": "Starting",
"State": "X",
"Site": "Main",
"v": [{
"chanNo": 3
}]
},
{
"Created": "2020-03-12T16:41:43.017",
"Modified": "2020-03-12T16:41:43.017",
"ReadingID": 24870,
"Name": "Test",
"Reading": "Starting",
"State": "X",
"Site": "Aux",
"v": [{
"chanNo": 4
}]
},
{
"v": [{
"chanNo": 30
},
{
"chanNo": 32
},
{
"chanNo": 39
},
{
"chanNo": 56
},
{
"chanNo": 69
},
{
"chanNo": 71
},
{
"chanNo": 79
},
{
"chanNo": 82
},
{
"chanNo": 87
}]
}]
}]);
var algReading = JsonSerializer.Deserialize<AlgReadingOuter>(stringBuilder);
docs.Add(algReading);
That is because StringBuilder
is not a string.
You should call ToString()
to actually get what you want.
So, it should really be:
algReading= JsonSerializer.Deserialize<AlgReadingOuter>(jsonResult.ToString());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.