0

I have a javascript library that is expecting this from the server but the example is in PHP

<?php
/* http://www.example.com/json.php */
$array['E'] =  'Letter E';
 $array['F'] =  'Letter F';
 $array['G'] =  'Letter G';
 $array['selected'] =  'F';
 print json_encode($array);
 ?>

so I trying to find how to do the above in c# asp.net-mvc given that C# arrays don't take string keys . .

 public JsonResult MyAction()
 {
     return Json(...);
  } 

2 Answers 2

1

What does the resulting JSON look like from that PHP code?

If its an object.. you could just return an anonymous object:

return Json(new {
    E = "Letter E",
    F = "Letter F",
    // etc...
});

If its a key-value pair, you could use a Dictionary:

return Json(new Dictionary<string, string>() {
    { "E", "Letter E" },
    { "F", "Letter F" },
    // etc...
});
Sign up to request clarification or add additional context in comments.

Comments

1

Try this using anonymous types:

public JsonResult MyAction()
{
    return Json(
        new
        {
            E = "Letter E",
            F = "Letter F",
            G = "Letter G",
            Selected = "F",
        });
} 

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.