3

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.

{

    "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/

1
  • 1
    Please don't pout answers into questions. I've removed and will add as a wiki answer. If you want to answer your own question let me know and I'll delete the wiki Commented Mar 17, 2017 at 11:53

3 Answers 3

6

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

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?

Sign up to request clarification or add additional context in comments.

Comments

4

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

return Json(mySearchResult);

1 Comment

you beat me to it, but figure it out so i will mark urs as an answer since its close to what I had to do
1

Answer from 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; }
    }

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.