0

I have a JSON string that I am sending to a c# server. It comprises an array of Event objects and an Array of relationship objects. The relationship objects describe the database table relationships.

However I'm having trouble getting data from the the JSON at the server. The object doesn't exist on the server to deserailize into and JSON.net throws parse errors when I try the following:

// Both throw parse errors
JObject o = JObject.Parse(Request.Form.ToString());
JsonConvert.DeserializeObject<MobileEvents>(Request.Form.ToString());

the JSON:

{
    "CreateEvents": {
        "Event": [
            {
                "Id": "1",
                "Subject": "Hire a Clown"
            }
        ],
        "Relationship": [
            {
                "Primary": "Table1",
                "Secondary": "Table2",
                "Field": [
                    {
                        "Table1Id": "1",
                        "Table2Id": [
                            "101"
                        ]
                    }
                ]
            },
            {
                "Primary": "Table1",
                "Secondary": "Table3",
                "Field": [
                    {
                        "Table1Id": "1",
                        "Table3Id": [
                            "200025"
                        ]
                    }
                ]
            },
            {
                "Primary": "Table1",
                "Secondary": "Table4",
                "Field": [
                    {
                        "Table1Id": "1",
                        "Table4Id": [
                            "3"
                        ]
                    }
                ]
            }
        ]
    }
}
Jon Wells
  • 4,191
  • 9
  • 40
  • 69
  • which exception? what does `The object doesnt exist to deserailize into` mean? usually you access a key within your `.Form` like `this.Request.Form["myHiddenFieldNAME"]` to get a value –  May 31 '12 at 09:25
  • Check this - http://stackoverflow.com/questions/10815439/deserializing-multiple-json-arrays-of-different-types-of-objects-in-c-sharp – Kapil Khandelwal May 31 '12 at 09:26
  • @Andreas The data structure is created client side, its doesn't exist on the server (does that make any more sense?) – Jon Wells May 31 '12 at 09:29
  • @Kapil that is similar to what I tried originally, unfortunately i'm using .net 3.5 and do not have dynamic objects – Jon Wells May 31 '12 at 09:30
  • @CrimsonChin ah - the model does not exist on the server ...? well, then you will need to use the dynamic part of json.net :) (your approach of using `JObject.Parse`...) –  May 31 '12 at 09:31

2 Answers2

3

Request.Form.ToString() would returns the result like "a=1&b=3", it's definitely not what you need.

If you're passing values as submiting a form, you can use Request.Form["your-key"] to get the value.

If you're passing values by the http body, you can use new StreamReader(Request.InputStream).ReadToEnd() to get the whole JSON string.

Jeffrey Zhao
  • 4,923
  • 4
  • 30
  • 52
  • stream reader approach worked for me - this turned into an excellent solution for parsing massive json objects in MVC, by not trying to pass them as parameters in the controller but instead reading directly from the form of the request. Genius thanks! – Adam Diament Apr 09 '14 at 10:19
2

I think you have an error within your getting ...

It's not

this.Request.Form.ToString(); // see http://stackoverflow.com/questions/7065979/why-is-the-return-value-of-request-form-tostring-different-from-the-result-of for output

Instead it should be

this.Request.Form["myInputNAME"].ToString();

Important - really use the name-attribute of your input/select/...-element

Anyways: I would like to encourage you, to use eg. <asp:HiddenField runat="server" ID="foo" />. When you have a server-control you can then access its value by simple doing this.foo.Value at server-side, whereas at client-side you can access the input field like document.getElementById('<%= this.foo.ClientID %>')

  • I'm wondering if the values should even be in the Form. The object is created on the client from form values AND THEN posted to the server so I'm not sure where the input/value pairing is maintained at this point. – Jon Wells May 31 '12 at 09:49
  • oh well, my mistake ... i assumed that you are transforming your json-object to a string an then save it to an `input`-element - if you are not actually doing it, you should so ... for transforming the json-object to string take eg `JSON.stringify` (https://github.com/douglascrockford/JSON-js) (demo of usage @ http://stackoverflow.com/a/912247/57508) –  May 31 '12 at 10:14