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"
                        ]
                    }
                ]
            }
        ]
    }
}
5
  • 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 Commented May 31, 2012 at 9:25
  • Check this - stackoverflow.com/questions/10815439/… Commented May 31, 2012 at 9:26
  • @Andreas The data structure is created client side, its doesn't exist on the server (does that make any more sense?) Commented May 31, 2012 at 9:29
  • @Kapil that is similar to what I tried originally, unfortunately i'm using .net 3.5 and do not have dynamic objects Commented May 31, 2012 at 9: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...) Commented May 31, 2012 at 9:31

2 Answers 2

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.

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

1 Comment

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!
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 %>')

2 Comments

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.
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 (github.com/douglascrockford/JSON-js) (demo of usage @ stackoverflow.com/a/912247/57508)

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.