[英]How can i translate this PHP code to C# asp.net-mvc?
I have a javascript library that is expecting this from the server but the example is in PHP 我有一个javascript库,期望从服务器这个,但示例是在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 . 所以我试图找到如何在c#asp.net-mvc中执行上述操作,因为C#数组不接受字符串键。 . 。
public JsonResult MyAction()
{
return Json(...);
}
Try this using anonymous types: 使用匿名类型尝试此操作:
public JsonResult MyAction()
{
return Json(
new
{
E = "Letter E",
F = "Letter F",
G = "Letter G",
Selected = "F",
});
}
What does the resulting JSON look like from that PHP code? 从那个PHP代码得到的JSON是什么样的?
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...
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.