How to pass values to string array in post method using Fiddler? - c#

When I'm passing the values in Fiddler like {"rajehs","ramesh","ramsgkfhh"}, I'm getting all the values in [0] location. I've also tried by using = {"rajehs","ramesh","ramsgkfhh"}.
I declared the method like :
public void Post([FromBody]string[] value)
{
}

You are passing a JSON object in our body, instead of an array. Use [] to pass a JSON array:
["rajehs","ramesh","ramsgkfhh"]

Related

c# error - Can not convert Array to byte array

I am trying cast a Json data to byte array and then save it in SQL image field, code is below
public string Post([FromBody] dynamic data)
{
foreach (var item in data)
{
imgPhoto1 = (byte[])item["Photo1"];
}
}
But getting error Can not convert Array to byte array
byte[] imgPhoto1 = (byte[])item["Photo1"];
Values in field item["Photo1"] is look like below
[255,216,255,224]
any help will be appreciated
If your parameter data is JToken, then you need convert it to desired type.
Try to use:
var obj = item["Photo1"].ToObject<byte[]>();
And will be better explicit declare your parameter data as JToken.

NewtonSoft Json.NET and Single Element Arrays

I have some JSon that I am converting to an object using the ToObject method.
A part of this Json has a repeated element which is correctly represented as an array in the Json text. When I convert this it correctly is mapped to the C# object
public IList<FooData> Foo { get; set; }
But when I only have 1 element I get an error saying that the Json that I am trying to Parse into an object is not an array because it does not have [] around it.
Does Json.NET support single element arrays?
But when I only have 1 element I get an error saying that the Json
that I am trying to Parse into an object is not an array because it
does not have [] around it.
If a JSON text has no [] around, then it's not a single-element array: actually it's an object (for example: { "text": "hello world" }).
Try using JsonConvert.DeserializeObject method:
jsonText = jsonText.Trim();
// If your JSON string starts with [, it's an array...
if(jsonText.StartsWith("["))
{
var array = JsonConvert.DeserializeObject<IEnumerable<string>>(jsonText);
}
else // Otherwise, it's an object...
{
var someObject = JsonConvert.DeserializeObject<YourClass>(jsonText);
}
It can also happen that JSON text contains a literal value like 1 or "hello world"... but I believe that these are very edge cases...
For the above edge cases just deserialize them with JsonConvert.DeserializeObject<string>(jsonText) for example (replace string with int or whatever...).
Make sure you are enclosing your JSON single item array is still specified as an array using array notation []

How to read params object[] items passed from C# in Javascript?

I am doing a simple program in Silverlight to inkove javascript function in silverlight.
The silverlight function is as under
void InvokeJS(params object[] items)
{
object result = System.Windows.Browser.HtmlPage.Window.Invoke("JSFunction", items);
}
Pasing value to this function is happening as under
InvokeJS((object)new object[]{ (object)"10", (object)"20"})
And the JS function is as under
function JSFunction(params) {
alert(params);
}
Now how to read the params value in javascript?
The params variable is just the first of many arguments being passed in. You can access the other arguments using the following syntax:
alert(this.arguments[0]);
alert(this.arguments[1]);
alert(this.arguments[2]);
If you're passing all the arguments in a single variable, it will be an array so use:
alert(params[0]);
alert(params[1]);
alert(params[2]);
To the called function, the params array is just that, an array.
In this case you will have an array that looks like this:
[ [ "10", "20" ] ]
I got it
alert(params[0]); alert(params[1]);

Pass array as individual values to params

I am calling a function which takes the same form as string.format where the first parameter is a string and the remainder are the replacement values. I have the string in a variable and the replacement values in an array, how can I call this function given any number of objects in the array? Simply passing in the array as the last argument does not work.
Use the params keyword:
public string MyMethod(string value, params object[] args)
{
// as an example
return string.Format(value, args);
}
Then you can call it either with individual values
MyMethod("Test", "value1", "value2");
Or with an array
MyMethod("Test", new [] { "value1", "value2" });
you need to use the params keyword
The function I was calling had the signature
public static IQueryable Where(this IQueryable source, string predicate, params object[] values)
Thhe issue was that I was passing in an array of ints as the last parameter. When I createed a new array of objects poplulated from that initial array and passed it in it worked. Thanks for the answers

Why am I getting System.char[] printed out in this case?

I'm trying to figure out what I'm doing wrong here, but I can't seem to. I have this method that takes in a string and reverses it. However, when I print out the reversed string from the caller method, I just get "System.Char []" instead of the actual reversed string.
static string reverseString(string toReverse)
{
char[] reversedString = toReverse.ToCharArray();
Array.Reverse(reversedString);
return reversedString.ToString();
}
Calling ToString on a T array in .NET will always return "T[]". You want to use this instead: new string(reversedString).
By calling ToString you just get the default implementation that every class inherits from object. .NET can't provide a special implementation just for an array of char; the override would have to apply to all types of array.
Instead, you can pass the array to String's constructor, return new String(reversedString).

Categories

Resources