c# error - Can not convert Array to byte array - c#

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.

Related

How can I parse a string representation of an array of array of doubles?

I'm passing a JSON object in C#. The original object is an array of arrays of doubles, e.g.
var arrayObject = [[1.2,3,1,0],[2.3,1,0,9],[3,6.7,9,1]]
To pass between JavaScript and C# this has been converted to a JSON representation of an array of arrays:
string json = "[[1.2,3,1,0],[2.3,1,0,9],[3,6.7,9,1]]"
How can I parse this? I want to do something like JavaScriptSerializer().Deserialize<List<myObject>>(json), but this gives me an error that "Type 'myObject' is not supported for deserialization of an array."
There's not much structure here in terms of a JSON object...and all I really need to do is parse this out into a set of arrays.
I've read a little about Json.NET, but I don't want to add unnecessary class libraries. Is there a simple way to parse this string?
First, deserialize it into something the serializer does understand - a double[][] - then you can convert it however you want:
string json = "[[1.2,3,1,0],[2.3,1,0,9],[3,6.7,9,1]]";
double[][] arrayOfArrays = new JavaScriptSerializer().Deserialize<double[][]>(json);
Assuming your myObject class looks something like:
class myObject {
public double[] Arr { get;set; }
}
Then you could use LINQ to convert it to what you're looking for:
List<myObject> list = arrayOfArrays.Select(x => new myObject { Arr = x }).ToList();

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 []

Reading Binary/Byte from SQL on C# ASP

Im trying to read the Binary Data of the Document Stored on SQL.
But when im suppose to store it in my Template Class i can't.
presentation.presentationDocBinData = byte.Parse(dr["presentationDocBinData"].ToString());
public byte[] presentationDocBinData
{
get;
set;
}
So im reading it from a DataRow. I tried byte[], Byte, byte, Byte[]. Im pretty lost on what to do.. any enlightment would be much appreciated.
You can simply cast object to byte array:
var result = (Byte[])dr["presentationDocBigData"];
Try to convert it in byte[](byte array)
presentation.presentationDocBinData = (byte[])dr["presentationDocBinData"];
public byte[] presentationDocBinData
{
get;
set;
}
byte.Parse() converts only single value into byte. for example if you have a value like "A" and you want to convert it into byte then you can use byte.Parse() method to convert value into byte.

Getting type from list of icomparable

I have a dictionary of <string, IComparable> which means the value could Pretty much contain any value type.
Now when I update the value I want to convert a string input to the original type. I have tried all manner of converts but I don't seem to be having any success.
Example
Dictionary val = new Dictionary ();
Val.Add ("test", 1);
Val ["test"]="44"; //this is where I tried convert. But it's not always an int.
So if pre update the value was an int then it will be stored as an int. If it is a byte then it will be stored as an byte.
Can anyone point me in the right direction
Thanks
string id = "type";
val.Add(id, 1);
var type = val[id].getType();
val[id] = System.Convert.ChangeType("44", type);
Assuming that you know all the data types that will be used (int, byte etc), you could use the typeof operator to check for the type before, and then cast to that type from your data.
if (typeof(Val["test]) == int)
{
Val["test"] = int.parse("44");
}

How do I check if an object contains a byte array?

I'm having an issue with the following code.
byte[] array = data as byte[]; // compile error - unable to use built-in conversion
if (array != null) { ...
I only want to assign the data to array variable if the data is actually a byte array.
How about this:
byte[] array = new byte[arrayLength];
if (array is byte[])
{
// Your code
}
Try
if(data.GetType().Name == "Byte[]")
{
// assign to array
}
As soon as I asked this I realised that the type of data was not object.
Making it of type object (its coming in via a type converter in Silverlight) and it worked.

Categories

Resources