C# Error HttpClient API that returns an integer "JsonReader item is not an object: integer.

Gerry Carpinetti 0 Reputation points
2023-09-13T14:19:33.0166667+00:00

The API program I am currently working on does work just fine. When the api returns an integer value it produces a JsonReader warning but will still output the integer to the richtextbox as in integer value. I am trying to fix this issue so it will handle the integer being returned - Error reading JObject from JsonReader. Current JsonReader item is not an object: Integer.

The ResponseTextBox_Textchanged trys and parses Json String for formatting the incoming json response into the richtextbox example below. 

try
    {
    //Try and parse Json string
    JObject jObject = JObject.Parse(input);
    string formattedJson = JsonConvert.SerializeObject(jObject, Formatting.Indented);
    ResponseTextBox.Clear();
    FormatAndColorCodeJson(formattedJson);
    }
catch (JsonException)
    {
    string correctedJson = TryToCorrectJson(input);
    try
        {
        JObject correctedJObject = JObject.Parse(correctedJson);
        string formattedJson = JsonConvert.SerializeObject(correctedJson, Formatting.Indented);
        ResponseTextBox.Clear();
        FormatAndColorCodeJson(formattedJson);
        }
    catch (JsonException je)
        {
        MessageBox.Show(je.Message);
        }

Here is part of the SendButton_Click that handles the httpclient but uses a 'switch' & case for GET, POST, PUT and DELETE. I am only sharing a subsection of that code:

HttpResponseMessage Response = await httpClient.GetAsync(url);
try
    {
    switch (selectedMethod)
        {
        case "GET":
            Response = await httpClient.GetAsync(apiVariable);
            break;
        }
    if (Response.IsSuccessStatusCode)
        {
        string responseBody = await Response.Content.ReadAsStringAsync();
        ResponseTextBox.Text = responseBody;
        }
    else
        {
        MessageBox.Show($"Error: {Response.StatusCode.ToString()}");
        }
    }
catch (HttpRequestException ex)
    {
    MessageBox.Show(ex.Message);
    }
 I have tried the following code:
if (response != null && response.IsSuccessfulStatusCode)
{
string content = await response.Content.ReadAsString();
if (int.TryParse(content, out int result))
{
// some code here
}
else
{
// some more code for error handling
}
/// THIS NEXT BLOCK I TRIED JToken.Parse
try 
{
JToken jsonToken = JToken.Parse(content);
respondBodyTextBox.AppendText(jsonToken.ToString(Formatted.Indented));
}
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,873 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,649 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Gerry Carpinetti 0 Reputation points
    2023-09-13T17:17:29.6566667+00:00

    Yes, it helped in that part, but I identified. That the errors origin is coming from this section of code:

    private void ResponseTextBox_TextChanged(object sender, EventArgs e)
        {
        ResponseTextBox.TextChanged -= ResponseTextBox_TextChanged;
        string input = ResponseTextBox.Text;
        try
            {
            //Try and parse Json string
            JObject jObject = JObject.Parse(input);
            string formattedJson = JsonConvert.SerializeObject(jObject, Formatting.Indented);
            ResponseTextBox.Clear();
            FormatAndColorCodeJson(formattedJson);
            }
        catch (JsonException)
            {
            string correctedJson = TryToCorrectJson(input);
            try
                {
                JObject correctedJObject = JObject.Parse(correctedJson);
                string formattedJson = JsonConvert.SerializeObject(correctedJson, Formatting.Indented);                   
                FormatAndColorCodeJson(formattedJson);
                }
            catch (JsonException je)
                {
               // MessageBox.Show(je.Message);
                }
            }
        //readd event handler
        ResponseTextBox.TextChanged += ResponseTextBox_TextChanged;
        }