简体   繁体   English

我怎样才能将这个PHP代码翻译成C#asp.net-mvc?

[英]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.

相关问题 如何在asp.net-mvc项目之外移动此代码 - How can i move this code outside of my asp.net-mvc project 使用ASP.Net-mvc 4从C#转换为VB - Conversion from C# to VB with ASP.Net-mvc 4 C#在asp.net-mvc中的linq表达式中编辑对象 - C# Edit objects in linq expression in asp.net-mvc 如何将int值从Web输入传递到c#asp.net-mvc 3中的域int? - How to pass int value from web input to domain int in c# asp.net-mvc 3? 如何在asp.net-mvc 5视图中访问元组项c#7.2 - how can access to tuple item in asp.net-mvc 5 view c#7.2 我如何在现有的asp.net-mvc网站上添加新路由而无需进行大量重写? - How can I add a new route on my existing asp.net-mvc site without a large rewrite? 在asp.net-mvc中,如何在控制器中建立URL - in asp.net-mvc, how can i build up URLs in controller 在 controller 在 asp.net-mvc 中,我如何获取有关用户浏览器的信息? - in a controller in asp.net-mvc how can i get information about the users browser? 如何在centos(linux)中解决这种情况以运行ASP.net-mvc? - how can i solve this situation in centos(linux) to run ASP.net-mvc? 在 asp.net-mvc 中,如何在不降低用户体验的情况下运行昂贵的操作? - In asp.net-mvc, how can I run an expensive operation without slowing down user experience?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM