I have an API endpoint which is receiving a data string which looks like the following:
"[[0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0],[[],[],[],[[17.637329000012972,17.637329000012972]],[],[],[],[],[]]]"
I would now like to convert this json object into an array which I can query, but I'm not sure where to start.
I have tried converting to a jagged array using
var result = JsonConvert.DeserializeObject<SomeType[][]>(json); where SomeType has been int, string, double etc etc
The problem seems to lie with each array being anonymous so it's proving difficult to model. To further complicate things some of the arrays found in the base array are also jagged arrays. As it happens I'm only really interested in the first array so if anyone can offer an easy way of extracting this I would be very grateful. Thanks in advance
Related
When I pass a PHP indexed array:
$myVar = new COM('MyClass');
$myVar->DubbleArray([1, 2]);
...it comes through to C# or F# as an System.Object[], and I can deal with it accordingly, e.g.:
[<ComVisible(true)>]
type LettersClass() =
member this.DubbleArray(array: obj[]) =
// do stuff with the values in the array
But when I pass an associative array:
$myVar->DubbleArray(array('first'=>'1st','second'=>'2nd'));
...it comes through as DBNull.
Is there any way to pass an associative array to a .NET function via COM?
Using a Dictionary in .NET allows you to deal with named keys. You could always encode the data in JSON format then decode in .NET as well... :)
(Note: this answer is based off of not seeing any .net in your question)
I suppose it really falls on how marshalling of an associative array is handled on the PHP side. The actual answer very well might be "not at all".
Unless you can figure that out, I would suggest a workaround - my best bet would be to convert the associative array to a multidimensional array storing keys and values in separate columns, and passing that through as an obj[,].
I have a string variable like so, which I am receiving from a third party API:
string strArray = "[[1,\"56353657\",\"300\",\"test\",\"<img src=\\\"../Images/Edit.gif\\\" id=\\\"Edit\\\" />\",\"<img src=\\\"../Images/Delete.gif\\\" id=\\\"Delete\\\" />\"],[2,\"56353657\",\"400\",\"test\",\"<img src=\\\"../Images/Edit.gif\\\" id=\\\"Edit\\\" />\",\"<img src=\\\"../Images/Delete.gif\\\" id=\\\"Delete\\\" />\"]]";
I would like to be able to loop through this and retrieve the first 3 items in each of the arrays.
Could someone please advise me as to how to achieve this using c#?
Because values in your array of arrays are not of the same type you can try this way:
String[][] table = JsonConvert.DeserializeObject<String[][]>(strArray);
Then you can loop through this and, if you need, convert values to the desired type.
So I've looked at Dictionaries and various arrays for this, and I'm sure I'm missing an elegant solution.
Currently, I have a configuration dictionary that has information about what data needs to be retrieved.
Then I create a string[,] array where the first string is the item number and the second is the configuration value for a given item, then the value is the value for that configuration item. Something like this:
ret[0,0] = "12345678"
ret[0,1] = "\\localhost\images"
ret[0,2] = "\test.img"
ret[1,0] = "23231231"
ret[1,1] = "\\localhost\images"
ret[1,2] = "\here.img"
There are more values, but that's the gist of it.
Now I need to also to grab each of those .img files (which are concatenated TIFF files) and extract images into byte[] values. Some of the additional values are an offset and length in the file for that item number's image, so extracting the images is easy. For some reason, however, I'm having a hard time finding a smart way to index the byte[] arrays for a given image (there's a front and a rear image for each) with the index value of the ret[,] array. Neither Dictionaries or Lists seem like they'd work. If I could have a jagged array with mixed values, that would work, but I don't really see how to do that.
Please let me know if I'm not making sense regarding what I'm looking for. I may need to draw it out lol
Thanks!
You need something like:
Dictionary<int,Dictionary<int,<string>> myVar = new Dictionary<int,Dictionary<int,string>>();
myVar.add(0,new Dictionary<int,string>(0,'string'));
Console.WriteLine(myVar[0][0]);
You might also want to check the DataTable class.
You can simply define your own class for the image.
It stores the number, and all the other strings and the byte array. Then you implement a List of this class.
I'm deserialising JSON using JSON.Net along the same lines as the accepted solution to this question: Deserialize json object into dynamic object using Json.net. Essentially:
dynamic d = JObject.Parse("{number:1000, str:'string', array: [1,2,3,4,5,6]}");
Console.WriteLine(d.number);
Console.WriteLine(d.str);
Console.WriteLine(d.array.Count);
Difference being my array has strings rather than numbers. The above methods work fine. However, what I want to do is test if the array contains a particular value. If it was a typed array of strings I could use myArray.IndexOf("ValueToFind") and if it returns a value > -1 then it's in there. However, this isn't working and I think it's because it's actually an array of JValues rather than strings.
I can iterate through the array, cast each one to a string and then test (i.e. a foreach loop with an if statement inside) but I was hoping for a more succinct single line test. Can anyone advise if there is a simpler way to do the test?
Thanks
JSON.NET can seamlessly convert JArray to List<string>, so you can use
Console.WriteLine(d.array.ToObject<List<string>>().IndexOf("1"));
This works even with JSON integer array.
I got a slightly messier answer by first converting your dynamic array to a known type:
IEnumerable<JToken> d2 = d.array;
Then you can use Any as an extension method.
if (d2.Any(p => p.ToString() == "1")) //etc.
I am trying to create a jagged array but due to the dynamic-ness of the data I am working with I do not want to waste resources creating a a large jagged array.
I am currently doing:
int[][][] data = new data[Int16.MaxValue][][];
I do not how big the data set is, or is there a better way than doing it via Lists?
Yes, you should use List<T>.
In this case, you would use List<List<List<int>>>.
Your array:
int[][][] data = new data[Int16.MaxValue][Int16.MaxValue][Int16.MaxValue];
will take up (2^16)^3 = 2^48 = way more storage space than you have,
not to mention that that declaration is not valid C#.
If you don't know how much space you need when you initialize, then it would be best to use a dynamically resizing list.
Use a variable similar to this:
List<List<List<int>>> data = new List<List<List<int>>>();
This variable allows you to add List<List<int>>'s to it, and those lists contain List<int>'s, which of course contain int's
If you absolutely do not want to use Lists, you can always replicate what Lists do under the hood: Create your array with a small number of elements, and when you reach the maximum, create a new array that is double the original's size, copy your existing array into it, and dispose of your original. Continue this pattern until you are done. I recommend using Lists instead, but this is how you would get around it if, for some reason, you just don't want to use Lists.
In fact, you can create jagged arrays without defining second and further dimensions.
int[][][] jagged = new int[256][][];
But at large datasets it is more effective to use streaming data - i.e., combinations of IEnumerable<T>.