1

My MVC3/.Net service is receiving some arguments as a JSONified Javascript array, and I'd like to unpack them into a C# array of strings.

Is there an existing method for doing this, or do I have to write my own?

(currently the data comes in to my controller as a single string, and looks something like this:

"[\"string1\", \"string2\", \"string3\"]"
4
  • Can you show us your Json data Commented Mar 23, 2012 at 19:58
  • Check for JSON.Net that helps serializing to and from the json format Commented Mar 23, 2012 at 20:01
  • Why don't you create a model so that when it comes into the controller the default binder will give you an object with its properties set based upon the json data? Commented Mar 23, 2012 at 20:03
  • We need to see the action method signature as well.. Commented Mar 23, 2012 at 20:11

2 Answers 2

1

Yes, this should happen via built in binding but without your code to see if there's an issue, can't comment more.

See for example: ASP.Net MVC 3 - JSON Model binding to array You shouldn't require JSON.Net

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

1 Comment

The jQuery $.post method, used with the syntax in the linked question, jsonifies the object in a way that the built in binding seems to like and the rest is cake.
0

I originally left a comment but thought I'd jump in with an answer.

If you create a model with the properties marked as public the default binder should return you an object in your controller based upon the incoming JSON data.

public MyModel
{
  public string Value1 {get;set;}
  public string Value2 {get;set;}
  public string Value3 {get;set;}
}

public ActionResult MyActionMethod(MyModel model)
{ 
   //oooh model is populated here
}

However it looks as though your JSON data is not key/value pairs

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.