2

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);

1 Answer 1

2

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());
Sign up to request clarification or add additional context in comments.

5 Comments

I think you're correct however this leads to another error about JSON not matching the swagger model. "The JSON value could not be converted to WebApiSwagger.Api.v2.Models.AlgReadingOuter. Path: $ | LineNumber: 0 | BytePositionInLine: 1.". Do you know of an easy way to return the JSON other than that way I'm trying above? All I'm doing is a SQL query and trying to return the results.
Ensure your json is correct. I suggest streaming from a text file.
I'm sure it's correct. I think it's having issues being serialized to the nested list constructor. I even used a generator such as json2csharp.com and it kicked back as unable to convert. pastebin.com/8wBcwYq8
I pasted your json into json2csharp and it works. Don't know if it counts, but you could mark the date properties as DateTime instead of string.
So it was throwing errors coercing to the class structure as it was hard coded. So I changed the deserialization syntax to not have a structure to use as a template. I'm still unsure why that did the fix because the structure looked 1:1. That fixed it though! return Ok(JsonConvert.DeserializeObject(jsonResult.ToString()));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.