JSON.NET Error reading JObject

21,099

json.stringify will create the following json string:

[
  ["fdsfsd", "Kifdsfa", "fsdfsa", "fadsf", "fasdfsd", "fadsf", "fasdfsd"],
  ["2008", "-5", "11", "12", "13"],
  ["2009", "20", "-11", "14", "13"],
  ["2010", "30", "15", "-12", "readOnly"]
]

Which is a jsonArray and not a JsonObject. So on server side you'll have to read it using JArray a = JArray.Parse(@value);

Share:
21,099
daniel
Author by

daniel

Always searching for contracts as a freelancer. My Blog https://zuidinga.de Newest Project: Cloud based FEA/FEM Simulation Moreover i am developing a site for engineers where they can find machine parts from manufacturers: http://www.engineeringonline.de (I am mechanical engineer besides my coding life) I am self employed. I prefer c#/.NET over Java at the moment and static typed languages compared to dynamic scripting languages.

Updated on December 12, 2020

Comments

  • daniel
    daniel over 3 years

    I am sending a JSON object via AJAX and Web Api to my Server:

    var data = [
      ["fdsfsd", "Kifdsfa", "fsdfsa", "fadsf", "fasdfsd", "fadsf", "fasdfsd"],
      ["2008", "-5", "11", "12", "13"],
      ["2009", "20", "-11", "14", "13"],
      ["2010", "30", "15", "-12", "readOnly"]
    ];
    
    $.ajax({
            url: '../webapi/Products',
            type: 'POST',
            dataType: "text", 
            data: "="+JSON.stringify( data ),
            success: function (test) {
                    alert(test);
                },
                error: function (test) {
                    alert("Error");
                }
    

    so i am getting on server the value which i want to parse with JSON.NET:

    public void Post([FromBody]string value )
    {
          JObject o = JObject.Parse(@value);
    }
    

    This throws the exception:

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

    Why? The value seems to be right to me?