0

This is what I want (json array): [{"location":"uk","keyword":"developer","specialization":"asp.net","lat":"28.5654"},"long":78.3265"]`

This is what I tried to get json array:

var list = new List<KeyValuePair<string, string>>();

        list.Add(new KeyValuePair<string, string>("Name", query.Name));
        list.Add(new KeyValuePair<string, string>("Specialization", query.Specialization));

        var json = JsonConvert.SerializeObject(list);

This is the result: [{"Key":"Name","Value":"Sam"},{"Key":"Specialization","Value":"ASP.Net"}]

But I want it like this: [{"Name":"Sam","Specialization":"ASP.Net"}]

dotnet_
  • 3
  • 2
  • 1
    Can you provide us your controller? That would be helpful. Need to know if your controller action method taking in an array or an object. – penleychan Nov 29 '18 at 21:43

2 Answers2

0

Hmmm, I think you really need a list of key value pair instead an array, try this:

var list = new List<KeyValuePair<string,string>>();
list.Add(new KeyValuePair<string,string>("location": mysearch.location);
list.Add(new KeyValuePair<string,string>("keyword": mysearch.keyword);
...

You can use this as your body request, but if you need an array you can do:

var array = list.ToArray();

For help how to make an http post request, you can consult this post: How to make HTTP POST web request

I hope be helpfull.

Lemon
  • 148
  • 10
0

Oh right, so I think the solution is use a Dictionary instead KeyValuePair:

var list = new Dictionary<string,string>();

list.Add("location", mysearch.location);
list.Add("keyword", mysearch.keyword);
...

var listSerialized= JsonConvert.Serialize(list);

If you need an array you can do this:

var dictionaryList = new List<Dictionary<string, string>>();

foreach(search in mySearchList)
{
    var item = new Dictionary<string,string>();
    item.Add("location", search.location);
    item.Add("keyword", search.keyword);
    ...

    dictionaryList.Add(item);
}
var serializedArray = JsonConvert.Serialize(jsonArray);
Lemon
  • 148
  • 10