在 mvc 中创建 json 对象并从控制器返回

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28438209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:44:35  来源:igfitidea点击:

Creating json object in mvc and returning from controller

jsonasp.net-mvc

提问by Zoinky

I need to create the following in a loop, my has "name" and "id" where name will be used for the value property of the json object and id will be used for the "data" and query will be some string I can set. I tried using keypair but could not figure out how to do this property. Any help would be appreciated.

我需要在循环中创建以下内容,我有“name”和“id”,其中 name 将用于 json 对象的 value 属性,id 将用于“data”,查询将是一些字符串我可以放。我尝试使用密钥对,但无法弄清楚如何执行此属性。任何帮助,将不胜感激。

{

    "query": "Unit",
    "suggestions": [
        { "value": "United Arab Emirates", "data": "AE" },
        { "value": "United Kingdom",       "data": "UK" },
        { "value": "United States",        "data": "US" }
    ]
}

I am trying to return results for this autocomplete widget https://www.devbridge.com/sourcery/components/jquery-autocomplete/

我正在尝试返回此自动完成小部件的结果 https://www.devbridge.com/sourcery/components/jquery-autocomplete/

回答by Matthew Jones

I hate to go full blown on this, but maybe create your own classes?

我不想在这方面全力以赴,但也许可以创建自己的课程?

public class DataValuePair
{
    public string Data {get;set;}
    public string Value {get;set;}
}

public class SearchResult
{
    public string Query {get;set;}
    public List<DataValuePair> Suggestions {get;set;}
}

And now you can return a JSON Result

现在你可以返回一个 JSON 结果

return Json(mySearchResult);

回答by Matthew Jones

You can just create an anonymous object. To return the JSON as indicated in your question, it would be

您可以只创建一个匿名对象。要返回您的问题中指示的 JSON,它将是

public JsonResult GetCities(string query)
{
  var data = new
  {
    query = "Unit",
    suggestions = new[]
    {
      new { value = "United Arab Emirates", data = "AE" },
      new { value = "United Kingdom", data = "UK" },
      new { value = "United States", data = "US" }
    }
  };
  return Json(data, JsonRequestBehavior.AllowGet);
}

Side note: Unsure of the purpose of the method parameter?

旁注:不确定方法参数的用途?

回答by Liam

Answer from OP:

来自 OP 的回答:

Figured it out, below is the code

想通了,下面是代码

public ActionResult GetCities(string query)
    {
        var obj = new CitySuggestion();
        obj.suggestions.Add(new Suggestion { value = "test1", data = "test1" });
        obj.suggestions.Add(new Suggestion { value = "test2", data = "test2" });
        obj.suggestions.Add(new Suggestion { value = "test3", data = "test3" });

       return  Content(JsonConvert.SerializeObject(obj), "application/json");
    }
    public class CitySuggestion
    {
        public CitySuggestion()
        {
            suggestions = new List<Suggestion>();
        }
        public List<Suggestion> suggestions
        {
            get;
            set;
        }
    }
    public class Suggestion
    {
        public string value { get; set; }
        public string data { get; set; }
    }