I'm having a JSON string, it has Key in the form of Camel-case but I need to convert the Key to Pascal-case.
Actual JSON String
string jsonString = "{\"personName\":{\"firstName\":\"Emma\",\"lastName\":\"Watson\"}}";
Expected JSON String : Needs to convert from the above JSON string.
string jsonString = "{\"PersonName\":{\"FirstName\":\"Emma\",\"LastName\":\"Watson\"}}";
Kindly assist me how to convert this using C#.
Because I can't sleep.
If you define the following static class of extension methods...
public static class JsonExtensions
{
public static void Capitalize(this JArray jArr)
{
foreach(var x in jArr.Cast<JToken>().ToList())
{
var childObj = x as JObject;
if(childObj != null)
{
childObj.Capitalize();
continue;
}
var childArr = x as JArray;
if(childArr != null)
{
childArr.Capitalize();
continue;
}
}
}
public static void Capitalize(this JObject jObj)
{
foreach(var kvp in jObj.Cast<KeyValuePair<string,JToken>>().ToList())
{
jObj.Remove(kvp.Key);
var newKey = kvp.Key.Capitalize();
var childObj = kvp.Value as JObject;
if(childObj != null)
{
childObj.Capitalize();
jObj.Add(newKey, childObj);
return;
}
var childArr = kvp.Value as JArray;
if(childArr != null)
{
childArr.Capitalize();
jObj.Add(newKey, childArr);
return;
}
jObj.Add(newKey, kvp.Value);
}
}
public static string Capitalize(this string str)
{
if (string.IsNullOrEmpty(str))
{
throw new ArgumentException("empty string");
}
char[] arr = str.ToCharArray();
arr[0] = char.ToUpper(arr[0]);
return new string(arr);
}
}
You can:
void Main()
{
string jsonString =
"{\"personName\":{\"firstName\":\"Emma\",\"lastName\":\"Watson\"}}";
var jObj = JObject.Parse(jsonString);
jObj.Capitalize();
Console.WriteLine(jObj.ToString()); //yay!
}
Related
public static string RESTToJsonConverter(string incoming_data){
string data = "[";
int i = 0;
Debug.Log("incoming_data"+incoming_data);
data += "]";
string JSONToParse = "{\"values\":" + data + "}";
return JSONToParse;
}
Below is my result when I run that code. My Question is how can I access/get all the data without taking "M4qRmfIqhKdy643Ujye" key (auto-generate)?
If using JavaScript, I can use object.values but since I'm using C#, I don't know how to get the data.
{
"-M4qRmfIqhKdy643Ujye": {
"assetName": "avatar",
"id": "-M4qRmfnFya7bC43Ujye",
"imageName": "icon_avatar",
"name": "Bob",
"objName": "Bobby",
"point": "-M4vZRY9vhKs65n5L_Gk",
"versionNumber": "3"
},
"-M4qRmfIqhKdy643Ujye": {
"assetName": "avatar",
"id": "-M4qRmfnFya7bC43Ujye",
"imageName": "icon_avatar",
"name": "Bobfds",
"objName": "Bobbydsf",
"point": "-M4vZRY9vhKs65n5L_Gk",
"versionNumber": "3"
},
"-M4qRmfIqhKdy643Ujye": {
"assetName": "avatar",
"id": "-M4qRmfnFya7bC43Ujye",
"imageName": "icon_avatar",
"name": "Bobfdsa",
"objName": "Bobbyfc",
"point": "-M4vZRY9vhKs65n5L_Gk",
"versionNumber": "3"
}
}
UPDATE
This is one of function in my Downloader Class
IEnumerator DownloadData(string dataPath, Action<string> callback){
Debug.Log("dataPath=>"+dataPath);
var token = LocalData.getAuth();
Auth data = JsonUtility.FromJson<Auth>(token);
var request = new
UnityWebRequest("https://test123.firebaseio.com/"+dataPath+".json?
auth="+data.idToken, "GET");
request.downloadHandler = (DownloadHandler) new
DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
yield return request.SendWebRequest();
if (request.isHttpError || request.isNetworkError)
{
Debug.Log(request.error);
NotificationHelper.showOnePopup("Error \n"+request.error);
callback(null);
}
else
{
//Debug.Log(request.downloadHandler.text);
string json =
FirebaseSetup.RESTToJsonConverter(request.downloadHandler.text);
callback(json);
}
Below is my FirebaseSetup Class
public static string FirebaseToJsonConverter(DataSnapshot snapshot){
string data = "[";
int i = 0;
foreach(DataSnapshot s in snapshot.Children){
data += s.GetRawJsonValue();
i++;
if(i != snapshot.ChildrenCount)
data += ",";
}
data += "]";
string JSONToParse = "{\"values\":" + data + "}";
return JSONToParse;
}
public static string RESTToJsonConverter(string incoming_data)
{
Debug.Log($"incoming_data:/n{incoming_data}");
// remove everything before the SECOND occurrence of '{'
// remove last occurrence of '}'
var startIndex = incoming_data.IndexOf('{', incoming_data.IndexOf('{') + 1);
var endIndex = incoming_data.LastIndexOf('}') - 1;
var json = incoming_data.Substring(startIndex, endIndex - startIndex);
// then remove leading or trailing whitespace
json = json.Trim();
Debug.Log($"json:/n{json}");
var data = JsonUtility.FromJson<string>(json);
return data;
}
After I try #derHugo code, I get new error.
This is my new error
For your specific case I would go the other way round:
The root field name usually doesn't matter so if you remove the trailing } and start the string from the second { you would have
{
"assetName": "avatar",
"id": "-M4qRmfnFya7bC43Ujye",
"imageName": "icon_avatar",
"name": "Bob",
"objName": "Bobby",
"point": "-M4vZRY9vhKs65n5L_Gk",
"versionNumber": "3"
}
which you can simply create a c# class for
[Serializable]
public class Data
{
public string assetName;
public string id;
public string imageName;
public string name;
public string objName;
public string point;
public string versionNumber;
}
and then you could use JsonUtility
public static Data RESTToJsonConverter(string incoming_data)
{
Debug.Log($"incoming_data:/n{incoming_data}");
// remove everything before the SECOND occurrence of '{'
// remove last occurrence of '}'
var startIndex = incoming_data.IndexOf('{', incoming_data.IndexOf('{') + 1);
var endIndex = incoming_data.LastIndexOf('}') - 1;
var json = incoming_data.Substring(startIndex, endIndex - startIndex);
// then remove leading or trailing whitespace
json = json.Trim();
Debug.Log($"json:/n{json}");
var data = JsonUtility.FromJson<Data>(json);
return data;
}
Update
You now updated your question content so now the data comes as a Dictionary of data objects.
In this case you could use Newtonsoft Json.NET which directly supports (de)serialization of Dictionary like e.g.
[Serializable]
public class Data
{
public string assetName;
public string id;
public string imageName;
public string name;
public string objName;
public string point;
public string versionNumber;
}
and then do something like
public static Dictionary<string, Data> RESTToJsonConverter(string incoming_data)
{
Debug.Log($"incoming_data:/n{incoming_data}");
var data = JsonConvert.DeserializeObject<Dictionary<string, Data>(json);
return data;
}
then you can do e.g.
var datas = RESTToJsonConverter(receivedRawData);
foreach(var data in data.Values)
{
Debug.Log(data.id);
}
I suppose, your question is about the deserialization of the JSON with non-unique keys.
If it is, take to look over here: How to deserialize JSON with duplicate property names in the same object
In your case, the solution should contain the following:
declaring your contract:
public class Data
{
public string assetName{get;set;}
public string id{get;set;}
public string imageName{get;set;}
public string name{get;set;}
public string objName{get;set;}
public string point{get;set;}
public string versionNumber{get;set;}
}
your "custom deserializer" :)
public static JToken DeserializeAndCombineDuplicates(JsonTextReader reader)
{
if (reader.TokenType == JsonToken.None)
{
reader.Read();
}
if (reader.TokenType == JsonToken.StartObject)
{
reader.Read();
JObject obj = new JObject();
while (reader.TokenType != JsonToken.EndObject)
{
string propName = (string)reader.Value;
reader.Read();
JToken newValue = DeserializeAndCombineDuplicates(reader);
JToken existingValue = obj[propName];
if (existingValue == null)
{
obj.Add(new JProperty(propName, newValue));
}
else if (existingValue.Type == JTokenType.Array)
{
CombineWithArray((JArray)existingValue, newValue);
}
else // Convert existing non-array property value to an array
{
JProperty prop = (JProperty)existingValue.Parent;
JArray array = new JArray();
prop.Value = array;
array.Add(existingValue);
CombineWithArray(array, newValue);
}
reader.Read();
}
return obj;
}
if (reader.TokenType == JsonToken.StartArray)
{
reader.Read();
JArray array = new JArray();
while (reader.TokenType != JsonToken.EndArray)
{
array.Add(DeserializeAndCombineDuplicates(reader));
reader.Read();
}
return array;
}
return new JValue(reader.Value);
}
private static void CombineWithArray(JArray array, JToken value)
{
if (value.Type == JTokenType.Array)
{
foreach (JToken child in value.Children())
array.Add(child);
}
else
{
array.Add(value);
}
}
some code to get a Dictionary<string, Data[]> as a result type
using (StringReader sr = new StringReader(json))
using (JsonTextReader reader = new JsonTextReader(sr))
{
var parsed = DeserializeAndCombineDuplicates(reader).ToObject<Dictionary<string, Data[]>>();
if(parsed!=null)
{
parsed
.ToList()
.ForEach(x=>Console.WriteLine("\r\nkey={0}\r\nvalues:\r\n{1}"
, x.Key
, string.Join("\r\n", x.Value
.Select(z=>string.Join("\t\t", z.name, z.id, z.objName))
.ToArray())));
} else Console.WriteLine("No way, dude!");
}
The full solution is placed here: https://dotnetfiddle.net/lYBytk
I want to consume an ASP.NET Core Web API method that includes [FromQuery] parameters.
Since the format is somewhat unusual, I figured there would exist a library function that would take a complex type and generate the query string formatted text - however, I can't find one.
IOW, given a controller method defined like this:
[HttpGet("testing")]
public bool Testing([FromQuery]X x)
{
return (x?.Ys[1]?.Zs[1]?.Bs[3] == 3 && x?.Ys[1]?.Zs[0]?.A == 4);
}
And an X defined like this:
public class X
{
public Y[] Ys { get; set; }
}
public class Y
{
public Z[] Zs { get; set; }
}
public class Z
{
public int A { get; set; }
public int[] Bs { get; set; }
}
First of all, what's an example of what ASP.NET [FromQuery] is expecting to encounter in the query string in order to return true?
Secondly, is there a function somewhere that can serialize an object appropriately into whatever ASP.NET is expecting, or do I need to write one?
You can use the following "serializer"
public class QueryStringSerializer
{
private static bool IsPrimitive(object obj)
{
return obj.GetType().IsPrimitive || obj is string || obj is Guid;
}
private static bool IsEnumerable(object obj)
{
return obj is IEnumerable && !IsPrimitive(obj);
}
private static bool IsComplex(object obj)
{
return !(obj is IEnumerable) && !IsPrimitive(obj);
}
private static StringBuilder ToQueryStringInternal(object obj, string prop = null)
{
StringBuilder queryString = new StringBuilder();
//skip null values
if (obj == null)
return queryString;
Type type = obj.GetType();
PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
if (IsEnumerable(obj))
{
int i = 0;
foreach (object item in obj as IEnumerable)
{
string query = ToQueryStringInternal(item, $"{prop}[{i}]").ToString();
queryString.Append(query);
i++;
}
}
else if (IsComplex(obj))
{
foreach (PropertyInfo property in properties)
{
string propName = property.Name;
object value = property.GetValue(obj);
string name = prop == null ? propName : $"{prop}.{propName}";
string query = ToQueryStringInternal(value, name).ToString();
queryString.Append(query);
}
}
else
{
string encoded = HttpUtility.UrlEncode(Convert.ToString(obj));
queryString.Append($"{prop}={encoded}&");
}
return queryString;
}
public static string ToQueryString(object obj, string propertyName = null)
{
StringBuilder queryString = ToQueryStringInternal(obj, propertyName);
queryString.Length--;
return queryString.ToString();
}
}
Usage
var x = new X
{
Ys = new Y[] {
new Y {
Zs = new Z[] { new Z { } }
},
new Y {
Zs = new Z[] {
new Z { },
new Z {
A = 1,
Bs = new int[] { 0, 1, 2, 3 }
}
}
}
}
};
string query = QueryStringSerializer.ToQueryString(x);
Result
Ys[0].Zs[0].A=0&Ys[1].Zs[0].A=0&Ys[1].Zs[1].A=1&Ys[1].Zs[1].Bs[0]=0&Ys[1].Zs[1].Bs[1]=1&Ys[1].Zs[1].Bs[2]=2&Ys[1].Zs[1].Bs[3]=3
Caution
The serializer may still contain various bags. Also, do not create array with "gaps" such as
var q = new X[] {
new X { },
null, //a gap
new X { }
};
The result will be technically correct but ASP.NET model binder will bind only the first element properly.
I have a json string like,
{"objectType" : "Subscriber", "objectList":[{"firstName":"name1","email":"email#example.com","address":"exampleAddress"},{"firstName":"name2","email":"email2#example.com","address":"exampleAddress2"}]}
I need to parse it in my C# code. I have tried,
JavaScriptSerializer json_serializer = new JavaScriptSerializer();
object routes_list = json_serializer.DeserializeObject(myjson here);
But i cant loop through the "objectList" array. How it can be done?
var jsonObj = new JavaScriptSerializer().Deserialize<RootObj>(json);
foreach (var obj in jsonObj.objectList)
{
Console.WriteLine(obj.address);
}
public class ObjectList
{
public string firstName { get; set; }
public string email { get; set; }
public string address { get; set; }
}
public class RootObj
{
public string objectType { get; set; }
public List<ObjectList> objectList { get; set; }
}
Hint: You can use this site to convert your json string to c# classes
EDIT
using Json.Net
dynamic jsonObj = JsonConvert.DeserializeObject(json);
foreach (var obj in jsonObj.objectList)
{
Console.WriteLine(obj.address);
}
var routes_list = (Dictionary<string, object>)json_serializer.DeserializeObject(myjson);
foreach (var record in routes_list)
{
Console.WriteLine(record);
}
This worked for me, converts to JSON to YAML essentially
string JSONDeserialized {get; set;}
public int indentLevel;
private bool JSONDictionarytoYAML(Dictionary<string, object> dict)
{
bool bSuccess = false;
indentLevel++;
foreach (string strKey in dict.Keys)
{
string strOutput = "".PadLeft(indentLevel * 3) + strKey + ":";
JSONDeserialized+="\r\n" + strOutput;
object o = dict[strKey];
if (o is Dictionary<string, object>)
{
JSONDictionarytoYAML((Dictionary<string, object>)o);
}
else if (o is ArrayList)
{
foreach (object oChild in ((ArrayList)o))
{
if (oChild is string)
{
strOutput = ((string)oChild);
JSONDeserialized += strOutput + ",";
}
else if (oChild is Dictionary<string, object>)
{
JSONDictionarytoYAML((Dictionary<string, object>)oChild);
JSONDeserialized += "\r\n";
}
}
}
else
{
strOutput = o.ToString();
JSONDeserialized += strOutput;
}
}
indentLevel--;
return bSuccess;
}
usage
Dictionary<string, object> JSONDic = new Dictionary<string, object>();
JavaScriptSerializer js = new JavaScriptSerializer();
try {
JSONDic = js.Deserialize<Dictionary<string, object>>(inString);
JSONDeserialized = "";
indentLevel = 0;
DisplayDictionary(JSONDic);
return JSONDeserialized;
}
catch (Exception)
{
return "Could not parse input JSON string";
}
What is the best way to convert a JSON object into querystrings to append to a GET Url? The POST is straight forward and gets read by my Web API backend.
{Name: 'Mike' } = ?Name=Mike
private static string MakeRequest(HttpWebRequest req, string data)
{
try
{
if (req.Method == Verbs.POST.ToString() || req.Method == Verbs.PUT.ToString() || req.Method == Verbs.DELETE.ToString())
{
var encodedData = Encoding.UTF8.GetBytes(data);
req.ContentLength = encodedData.Length;
req.ContentType = "application/json";
req.GetRequestStream().Write(encodedData, 0, encodedData.Length);
}
using (var response = req.GetResponse() as HttpWebResponse)
using (var reader = new StreamReader(response.GetResponseStream()))
{
return reader.ReadToEnd();
}
}
catch (WebException we)
{
if(we.Response == null)
{
return JsonConvert.SerializeObject(new { Errors = new List<ApiError> { new ApiError(11, "API is currently unavailable") }});
}
using (var response = we.Response as HttpWebResponse)
using (var reader = new StreamReader(response.GetResponseStream()))
{
return reader.ReadToEnd();
}
}
}
If the json object is flat as in your example, then
string json = #"{
""name"": ""charlie"",
""num"": 123
}";
var jObj = (JObject)JsonConvert.DeserializeObject(json);
var query = String.Join("&",
jObj.Children().Cast<JProperty>()
.Select(jp=>jp.Name + "=" + HttpUtility.UrlEncode(jp.Value.ToString())));
query would be name=charlie&num=123
I make this code to run in .Net Core:
public static string JsonToQuery(this string jsonQuery)
{
string str = "?";
str += jsonQuery.Replace(":", "=").Replace("{","").
Replace("}", "").Replace(",","&").
Replace("\"", "");
return str;
}
Example:
var _baseURI = "http://www.example.com/";
var endPoint = "myendpoint";
ExampleObjectModel requestModel = new ExampleObjectModel();
var requestModelJson = JsonConvert.SerializeObject(requestModel);
var url = string.Format("{0}{1}{2}", _baseURI, endPoint, requestModelJson.JsonToQuery());
Try this, work all object, in deep
public static class ExtensionMethods
{
public static string GetQueryString(this object obj, string prefix = "")
{
var query = "";
try
{
var vQueryString = (JsonConvert.SerializeObject(obj));
var jObj = (JObject)JsonConvert.DeserializeObject(vQueryString);
query = String.Join("&",
jObj.Children().Cast<JProperty>()
.Select(jp =>
{
if (jp.Value.Type == JTokenType.Array)
{
var count = 0;
var arrValue = String.Join("&", jp.Value.ToList().Select<JToken, string>(p =>
{
var tmp = JsonConvert.DeserializeObject(p.ToString()).GetQueryString(jp.Name + HttpUtility.UrlEncode("[") + count++ + HttpUtility.UrlEncode("]"));
return tmp;
}));
return arrValue;
}
else
return (prefix.Length > 0 ? prefix + HttpUtility.UrlEncode("[") + jp.Name + HttpUtility.UrlEncode("]") : jp.Name) + "=" + HttpUtility.UrlEncode(jp.Value.ToString());
}
)) ?? "";
}
catch (Exception ex)
{
}
return query;
}
}
To use: SomeObject.GetQueryString();
if your object(Entity) have a Children like this Entity :
public class Parent
{
public Child childs { get; set; } = new Child();
public int PageIndex { get; set; }
public int? PageSize { get; set; }
}
public class Child
{
public int Id { get; set; }
public string Name { get; set; }
}
Your Can Use This Code For Build Query:
First Convert You Enrity Model To JObject
And Call This Method :
public static string GetQueryString(this JObject jObj)
{
return String.Join("&",
jObj.Children().Cast<JProperty>()
.Select(jp =>
{
if (jp.Value.Type == JTokenType.Object)
{
var arrValue = String.Join("&",
jObj.Values().Children().Cast<JProperty>()
.Select(jp => jp.Path + "=" + HttpUtility.UrlEncode(jp.Value.ToString())));
return arrValue;
}
else
{
var arrValue = String.Join("&", jp.Name + "=" + HttpUtility.UrlEncode(jp.Value.ToString()));
return arrValue;
}
}
)) ?? "";
}
Your Can Get Like This QueryString :
childs.Id=1&childs.Name="Test"&PageIndex=1&PageSize=1
I found a really nice action filter that converts a comma-separated parameter to a generic type list: http://stevescodingblog.co.uk/fun-with-action-filters/
I would like to use it but it will not work for an ApiController, it completely ignore it. Can someone help convert this for Web API use?
[AttributeUsage(AttributeTargets.Method)]
public class SplitStringAttribute : ActionFilterAttribute
{
public string Parameter { get; set; }
public string Delimiter { get; set; }
public SplitStringAttribute()
{
Delimiter = ",";
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.ActionParameters.ContainsKey(this.Parameter))
{
string value = null;
var request = filterContext.RequestContext.HttpContext.Request;
if (filterContext.RouteData.Values.ContainsKey(this.Parameter)
&& filterContext.RouteData.Values[this.Parameter] is string)
{
value = (string)filterContext.RouteData.Values[this.Parameter];
}
else if (request[this.Parameter] is string)
{
value = request[this.Parameter] as string;
}
var listArgType = GetParameterEnumerableType(filterContext);
if (listArgType != null && !string.IsNullOrWhiteSpace(value))
{
string[] values = value.Split(Delimiter.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
var listType = typeof(List<>).MakeGenericType(listArgType);
dynamic list = Activator.CreateInstance(listType);
foreach (var item in values)
{
try
{
dynamic convertedValue = TypeDescriptor.GetConverter(listArgType).ConvertFromInvariantString(item);
list.Add(convertedValue);
}
catch (Exception ex)
{
throw new ApplicationException(string.Format("Could not convert split string value to '{0}'", listArgType.FullName), ex);
}
}
filterContext.ActionParameters[this.Parameter] = list;
}
}
base.OnActionExecuting(filterContext);
}
private Type GetParameterEnumerableType(ActionExecutingContext filterContext)
{
var param = filterContext.ActionParameters[this.Parameter];
var paramType = param.GetType();
var interfaceType = paramType.GetInterface(typeof(IEnumerable<>).FullName);
Type listArgType = null;
if (interfaceType != null)
{
var genericParams = interfaceType.GetGenericArguments();
if (genericParams.Length == 1)
{
listArgType = genericParams[0];
}
}
return listArgType;
}
}
What namespace are you using for ActionFilterAttribute? For Web API you need to be using System.Web.Http.Filters namespace and not System.Web.Mvc.
EDIT
Here's a rough conversion, not fully tested.
SplitStringAttribute
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
namespace StackOverflowSplitStringAttribute.Infrastructure.Attributes
{
[AttributeUsage(AttributeTargets.Method)]
public class SplitStringAttribute : ActionFilterAttribute
{
public string Parameter { get; set; }
public string Delimiter { get; set; }
public SplitStringAttribute()
{
Delimiter = ",";
}
public override void OnActionExecuting(HttpActionContext filterContext)
{
if (filterContext.ActionArguments.ContainsKey(Parameter))
{
var qs = filterContext.Request.RequestUri.ParseQueryString();
if (qs.HasKeys())
{
var value = qs[Parameter];
var listArgType = GetParameterEnumerableType(filterContext);
if (listArgType != null && !string.IsNullOrWhiteSpace(value))
{
string[] values = value.Split(Delimiter.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
var listType = typeof(List<>).MakeGenericType(listArgType);
dynamic list = Activator.CreateInstance(listType);
foreach (var item in values)
{
try
{
dynamic convertedValue = TypeDescriptor.GetConverter(listArgType).ConvertFromInvariantString(item);
list.Add(convertedValue);
}
catch (Exception ex)
{
throw new ApplicationException(string.Format("Could not convert split string value to '{0}'", listArgType.FullName), ex);
}
}
filterContext.ActionArguments[Parameter] = list;
}
}
}
base.OnActionExecuting(filterContext);
}
private Type GetParameterEnumerableType(HttpActionContext filterContext)
{
var param = filterContext.ActionArguments[Parameter];
var paramType = param.GetType();
var interfaceType = paramType.GetInterface(typeof(IEnumerable<>).FullName);
Type listArgType = null;
if (interfaceType != null)
{
var genericParams = interfaceType.GetGenericArguments();
if (genericParams.Length == 1)
{
listArgType = genericParams[0];
}
}
return listArgType;
}
}
}
CsvController
using System.Web.Http;
using System.Collections.Generic;
using StackOverflowSplitStringAttribute.Infrastructure.Attributes;
namespace StackOverflowSplitStringAttribute.Controllers
{
public class CsvController : ApiController
{
// GET /api/values
[SplitString(Parameter = "data")]
public IEnumerable<string> Get(IEnumerable<string> data)
{
return data;
}
}
}
Example request
http://localhost:52595/api/csv?data=this,is,a,test,joe
Here is another way:
public class ConvertCommaDelimitedList<T> : CollectionModelBinder<T>
{
public override bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
var _queryName = HttpUtility.ParseQueryString(actionContext.Request.RequestUri.Query)[bindingContext.ModelName];
List<string> _model = new List<string>();
if (!String.IsNullOrEmpty(_queryName))
_model = _queryName.Split(',').ToList();
var converter = TypeDescriptor.GetConverter(typeof(T));
if (converter != null)
bindingContext.Model = _model.ConvertAll(m => (T)converter.ConvertFromString(m));
else
bindingContext.Model = _model;
return true;
}
}
And list your param in the ApiController ActionMethod:
[ModelBinder(typeof(ConvertCommaDelimitedList<decimal>))] List<decimal> StudentIds = null)
Where StudentIds is the querystring param (&StudentIds=1,2,4)