JSON.NET Error reading JObject

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 get on the server a value that I want to parse using JSON.NET:

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

This throws an exception:

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

Why? It seems to me that this value is for me?

+4
source share
2 answers

json.stringify will create the following json line:

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

This is jsonArray, not JsonObject. Therefore, on the server side, you will need to read it using JArray a = JArray.Parse(@value);

+11
source

Just by looking at it, I suggest changing

 data: "="+JSON.stringify( data ), 

to

 data: "myJSON="+JSON.stringify( data ), 

... since jQuery expects either a serialization object or a valid query string, and then listens for this published variable. I don’t think you can simply POST to collect a bunch of data without assigning it as the value of a name / value pair.

0
source

All Articles