5

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?

2
  • Did you print value on the server to check if the syntax is correct? My Guess is, you are getting a json array and not a json object Commented Dec 11, 2012 at 15:35
  • yes it seems to be array. So there is no way to convert this to a JObject right? How can i convert this array e.g. to a DataTable? Commented Dec 11, 2012 at 15:41

2 Answers 2

12

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

Sign up to request clarification or add additional context in comments.

Comments

0

Just glancing over this, I'd suggest changing

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

to

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

...since jQuery expects either an object to serialize, or a valid query string, and then listening for that posted variable. I don't think you can just POST a bunch of data without assigning it as the value of a name/value pair.

Comments

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.