Converting a JSON String from a datatable into a JObject c#
<p>I am looking for some help with a  datatable conversion into a JObject. I am using <code>C#</code> in visual studio along with the <code>Newtonsoft JSON .net</code> library. I have retrieved my datatable from my database. </p>

<p>From here I took the data table and processed it through this class:</p>

<pre><code>    public string DataTableToJSONString(DataTable table)
    {
        string JSONString = string.Empty;
        JSONString = JsonConvert.SerializeObject(table,Formatting.Indented);
        return JSONString; 
    }  
</code></pre>

<p>Now that the object has been converted to a <code>JSONString</code> successfully (It throws no errors at this point) via the <code>Newtonsoft JSON.net</code> Library, I am unable to parse it into a <code>JObject</code> with this code:</p>

<blockquote>
  <p>Note: "json" is the string variable I placed the returned value from the DatatabletoDataTableToJSONString() method into..</p>
</blockquote>

<p>JObject job = JObject.Parse(json);</p>

<p>I continuously get the following error...</p>

<pre><code>Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.
</code></pre>

<p>The reason I need it in the form of a <code>JObject</code> it to pass it into the following post method for an API:</p>

<pre><code>    public static async Task&lt;JObject&gt; Post(string url, JObject data)
    {
        // Create an HttpClient instance
        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Authorization = CreateBasicHeader(ClientContext.Username, ClientContext.Password);
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));   

        // Send a request asynchronously and continue when complete
        var requestbody = new StringContent(data.ToString(), Encoding.UTF8, "application/json");

        HttpResponseMessage response = await client.PostAsync(url, requestbody);
        dynamic content = await response.Content.ReadAsAsync&lt;JObject&gt;();

        // Check that response was successful or throw exception
        try
        {
            response.EnsureSuccessStatusCode();
        }
        catch (Exception ex)
        {
            throw new Exception("Status Code: " + response.StatusCode +    "\r\nMessage: " + content[0].ToString(), ex);
        }

        // Read response asynchronously as a JObject

        return content;
    }
</code></pre>

<p>I would appreciate any help on this as I have been researching for 2 weeks with no luck. This one is really killing me. Thanks in advance to all who take the time to examine this.</p>

<p>UPDATE: I ended up adding in the following bit of code as I realized I was returning an array when I pulled the data table.</p>

<p>string jsonresult = JsonConvert.SerializeObject(dt);</p>

<pre><code>        JArray aray = JArray.Parse(jsonresult);
</code></pre>

<p>//I then have code for the API login credentials here....</p>

<pre><code>            foreach (JObject item in aray)
            {
                ApiClient.Post(Url, item).Wait();
            }
</code></pre>

<p>This appears to pass in the JObjects just fine into the post method although I get the following error:"AggregateException was unhandled..." I set a break point and the flaw appears to happen at the:</p>

<pre><code>        try
        {
            response.EnsureSuccessStatusCode();

        }
</code></pre>

<p>After attempting the response.ensuresuccessstatusCode(); The program of course jumps to the catch....</p>

<pre><code>        catch (Exception ex)
        {
            throw new Exception("Status Code: " + response.StatusCode + "\r\nMessage: " + content.message.ToString(), ex);
        }
</code></pre>

<p>Again, any help would be appreciated. I am almost there with this. After it posts, I will be done.... Thanks again in advance to everyone. I apologize for being long winded.</p>