0

I've been trying for the past hour and can't get it. At this point my controller looks like

public ActionResult GenerateMasterLink (string assetsJSON) { ... } 

and I've confirmed that it is being passed a string like

[["Microsoft","Azure","roman_column.png"],["Microsoft","Azure","runphp.cmd"],["Microsoft","Azure","runphp.cmd"],["Microsoft","Azure","Picture1.png"],["Microsoft","Azure","vertical-align-scrnsht.png"],["Microsoft","Azure","vertical-align-scrnsht.png"]] 

The only question I have is how I get the damned stuff out of it!

I've tried creating a class

 public class ThreePartKey { public string organizationName { get; set; } public string categoryName { get; set; } public string fileName { get; set; } } 

and then done

ThreePartKey [] assetList = new JavaScriptSerializer().Deserialize<ThreePartKey []>(assetsJSON); 

which gives me

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

in my browser's console some of the time, and other times gives me

Additional information: Type 'AllyPortal.Controllers.SurfaceAssetsController+ThreePartKey' is not supported for deserialization of an array.

as a Visual Studio error.

I've experimented with a million things and can't get this right. All I want is to have the JSON in some C# data structure where I can actually use it in my controller. Any suggestions?

3
  • Did you try deserializing as an IEnumerable instead of an array?CommentedMay 18, 2015 at 22:35
  • @BradleyDotNET Supposing I can do that, how do I then get out the constituent strings for each subarray in the JavaScript array?CommentedMay 18, 2015 at 22:37
  • Ah, your JS data model doesn't match the C# one, that could be problematic as well. First try having each object only hold a string array (since that's what it is getting). You could try just accessing it, but 10:1 its not going to understand how to map the non-named strings in the sub array to the properties you defined.CommentedMay 18, 2015 at 22:39

2 Answers 2

3

You are trying to deserialize to incompatible model. You string input can be deserialized into string[][] variable but in order to allow deserializations into an ThreePartKey you need to name those values per property: [[organizationName: "Microsoft",...]] This will copy right value to your model

    2

    The problem is that your target datatype doesn't match the source datatype.

    If you are converting an array of array of strings, you must deserialize into another array of array of strings, only them, you'll be able to convert it into whatever you want:

    In short, replace

    ThreePartKey [] assetList = new JavaScriptSerializer().Deserialize<ThreePartKey []>(assetsJSON); 

    for

    ThreePartKey[] assetList = new JavaScriptSerializer().Deserialize<string[][]>(assetsJSON) .Select(el => new ThreePartKey() {organizationName = el[0], categoryName = el[1], fileName = el[2]}) .ToArray(); 

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.